Skip to content

Commit

Permalink
Use ImportCommand as base class
Browse files Browse the repository at this point in the history
  • Loading branch information
elnappo committed Mar 28, 2019
1 parent 7d366c3 commit 0945a92
Showing 1 changed file with 26 additions and 31 deletions.
57 changes: 26 additions & 31 deletions project_novis/callsign/management/commands/import_repeatermap.py
Original file line number Diff line number Diff line change
@@ -1,68 +1,63 @@
import requests
from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand
from django.contrib.gis.geos import Point

from ...models import CallSign, Repeater, Transmitter
from ...utils import extract_callsign
from . import ImportCommand
from ...models import Repeater, Transmitter


class Command(BaseCommand):
class Command(ImportCommand):
help = 'Import repeater from repeatermap.de'
source = "repeatermap.de"
task = "repeater_import_repeatermap"

def add_arguments(self, parser):
parser.add_argument('url', nargs='?', type=str, default="https://repeatermap.de/api.php")

def handle(self, *args, **options):
counter = 0
new_callsign_counter = 0
update_callsign_counter = 0
error = 0

self.stdout.write("Import repeater from repeatermap.de")
r = requests.get(options['url'], stream=False)
if r.ok:
repeaters = r.json()
for repeater in repeaters["relais"]:
callsign = extract_callsign(repeater["call"].split("-")[0])
if not callsign:
self.stdout.write(f"Invalid callsign { repeater['call'].split('-')[0] }")
try:
call_sign_instance, new_call_sign = self._handle_callsign(repeater["call"].split("-")[0])
except (ValueError,) as e:
self._warning(f"Invalid callsign {repeater} - {e}")
continue

counter += 1
call_sign_instance, new_call_sign = CallSign.objects.get_or_create(name=callsign,
defaults={"name": callsign,
"created_by": get_user_model().objects.get(id=1),
"type": "repeater"})
if not call_sign_instance.type:
call_sign_instance.type = "repeater"
call_sign_instance.save()

# TODO handle updates
if new_call_sign:
call_sign_instance.set_default_meta_data()
if "lat" in repeater and "lon" in repeater:
try:
call_sign_instance.location = Point(repeater["lon"], repeater["lat"])
call_sign_instance.save()
except Exception:
print(repeater)
call_sign_instance.save()
new_callsign_counter += 1
self._warning(f"Invalid location {repeater}")

# TODO handle updates
if "lat" in repeater and "lon" in repeater:
try:
repeater_instance, new_repeater = Repeater.objects.get_or_create(callsign=call_sign_instance,
defaults={"callsign": call_sign_instance,
"website": repeater["url"],
"location": Point(repeater["lon"], repeater["lat"]),
"created_by": get_user_model().objects.get(id=1),})
defaults={"callsign": call_sign_instance,
"website": repeater["url"],
"location": Point(repeater["lon"], repeater["lat"]),
"created_by": get_user_model().objects.get(id=1),
"source": self.source})
transmitter_instance, new_transmitter = Transmitter.objects.get_or_create(
repeater=repeater_instance, transmit_frequency=repeater["tx"],
defaults={"repeater": repeater_instance,
"transmit_frequency": repeater["tx"],
"offset": repeater["rx"] - repeater["tx"],
"mode": repeater["mode"],
"description": repeater["remarks"],
"created_by": get_user_model().objects.get(id=1),})

"created_by": get_user_model().objects.get(id=1),
"source": self.source})
except:
self.stderr.write(repeater)


self.stdout.write(self.style.SUCCESS('call sings: %d new call sings: %d updated call sings: %d errors: %d source: %s' % (
counter, new_callsign_counter, update_callsign_counter, error, options['url'])))
self._warning(f"Invalid repeater or transmitter data in {repeater}")
self._finish()

0 comments on commit 0945a92

Please sign in to comment.