kopia lustrzana https://github.com/glidernet/ogn-python
Added ognutils
rodzic
e0145de086
commit
c3b33ccde4
|
@ -1,51 +1,18 @@
|
|||
from urllib.request import urlopen
|
||||
|
||||
from .db import session
|
||||
from .model import AddressOrigin, Flarm
|
||||
from ogn.db import session
|
||||
from ogn.model import Flarm
|
||||
from ogn.ognutils import get_devices_from_ddb
|
||||
|
||||
|
||||
def get_devices_from_ddb():
|
||||
session.query(Flarm.address_origin == AddressOrigin.ogn_ddb).delete()
|
||||
|
||||
response = urlopen("http://ddb.glidernet.org/download")
|
||||
lines = response.readlines()
|
||||
for line in lines:
|
||||
if (line.decode()[0] == "#"):
|
||||
continue
|
||||
|
||||
flarm = Flarm()
|
||||
flarm.parse_ogn(line.decode())
|
||||
session.add(flarm)
|
||||
|
||||
def put_into_db(beacon):
|
||||
session.add(beacon)
|
||||
session.commit()
|
||||
|
||||
|
||||
def get_devices_from_flarmnet():
|
||||
session.query(Flarm.address_origin == AddressOrigin.flarmnet).delete()
|
||||
|
||||
response = urlopen("http://flarmnet.org/files/data.fln")
|
||||
lines = response.readlines()
|
||||
for line in lines:
|
||||
if (len(line) != Flarm.FLARMNET_LINE_LENGTH):
|
||||
continue
|
||||
|
||||
flarm = Flarm()
|
||||
flarm.parse_flarmnet(line.decode())
|
||||
session.add(flarm)
|
||||
|
||||
def fill_flarm_db():
|
||||
session.query(Flarm).delete()
|
||||
flarms = get_devices_from_ddb()
|
||||
session.bulk_save_objects(flarms)
|
||||
session.commit()
|
||||
|
||||
|
||||
def put_position_into_db(position):
|
||||
session.add(position)
|
||||
session.commit()
|
||||
|
||||
|
||||
def put_receiver_into_db(receiver):
|
||||
session.add(receiver)
|
||||
session.commit()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
get_devices_from_ddb()
|
||||
get_devices_from_flarmnet()
|
||||
fill_flarm_db()
|
||||
|
|
|
@ -4,7 +4,6 @@ from time import time
|
|||
from ogn import db_utils
|
||||
from ogn import settings
|
||||
from ogn.aprs_parser import parse_aprs
|
||||
from ogn.model import Position, Receiver
|
||||
|
||||
|
||||
def proceed():
|
||||
|
@ -53,10 +52,8 @@ def proceed_line(line):
|
|||
print('Reason: %s' % e)
|
||||
return
|
||||
|
||||
if isinstance(result, Position):
|
||||
db_utils.put_position_into_db(result)
|
||||
elif isinstance(result, Receiver):
|
||||
db_utils.put_receiver_into_db(result)
|
||||
if result is not None:
|
||||
db_utils.put_into_db(result)
|
||||
|
||||
if __name__ == '__main__':
|
||||
while True:
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
from urllib.request import urlopen
|
||||
|
||||
from ogn.model import Flarm
|
||||
|
||||
from geopy.geocoders import Nominatim
|
||||
|
||||
|
||||
def get_devices_from_ddb():
|
||||
devices = list()
|
||||
|
||||
response = urlopen("http://ddb.glidernet.org/download")
|
||||
lines = response.readlines()
|
||||
for line in lines:
|
||||
if (line.decode()[0] == "#"):
|
||||
continue
|
||||
|
||||
flarm = Flarm()
|
||||
flarm.parse_ogn(line.decode())
|
||||
devices.append(flarm)
|
||||
|
||||
return devices
|
||||
|
||||
|
||||
def get_country_code(latitude, longitude):
|
||||
geolocator = Nominatim()
|
||||
location = geolocator.reverse("%f, %f" % (latitude, longitude))
|
||||
country_code = location.raw["address"]["country_code"]
|
||||
return country_code
|
|
@ -0,0 +1,14 @@
|
|||
import unittest
|
||||
from ogn.ognutils import get_devices_from_ddb, get_country_code
|
||||
|
||||
|
||||
class TestStringMethods(unittest.TestCase):
|
||||
def test_get_devices_from_ddb(self):
|
||||
devices = get_devices_from_ddb()
|
||||
self.assertGreater(len(devices), 1000)
|
||||
|
||||
def test_get_country_code(self):
|
||||
latitude = 48.0
|
||||
longitude = 11.0
|
||||
country_code = get_country_code(latitude, longitude)
|
||||
self.assertEquals(country_code, 'de')
|
Ładowanie…
Reference in New Issue