2014-10-16 07:37:35 -06:00
|
|
|
from optparse import make_option
|
2014-10-21 02:27:39 -06:00
|
|
|
|
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
2019-09-23 05:08:11 -06:00
|
|
|
from satellite_tle import fetch_tle_from_celestrak
|
2014-10-21 02:27:39 -06:00
|
|
|
|
2014-12-19 06:06:58 -07:00
|
|
|
from network.base.models import Satellite
|
2014-10-16 07:37:35 -06:00
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
option_list = BaseCommand.option_list + (
|
|
|
|
make_option('--delete',
|
|
|
|
action='store_true',
|
|
|
|
dest='delete',
|
|
|
|
default=False,
|
2014-10-21 02:27:39 -06:00
|
|
|
help='Delete Satellite'),
|
2014-10-16 07:37:35 -06:00
|
|
|
)
|
2014-10-21 02:27:39 -06:00
|
|
|
args = '<Satellite Identifiers>'
|
2014-10-16 07:37:35 -06:00
|
|
|
help = 'Updates/Inserts TLEs for certain Satellites'
|
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
for item in args:
|
|
|
|
if options['delete']:
|
|
|
|
try:
|
|
|
|
Satellite.objects.get(norad_cat_id=item).delete()
|
2014-10-21 02:27:39 -06:00
|
|
|
self.stdout.write('Satellite {}: deleted'.format(item))
|
2014-10-16 07:37:35 -06:00
|
|
|
continue
|
2017-11-17 07:31:21 -07:00
|
|
|
except Satellite.DoesNotExist:
|
2014-10-16 07:37:35 -06:00
|
|
|
raise CommandError('Satellite with Identifier {} does not exist'.format(item))
|
|
|
|
|
2014-10-21 02:27:39 -06:00
|
|
|
try:
|
2019-09-15 12:53:58 -06:00
|
|
|
tle = fetch_tle_from_celestrak(item)
|
|
|
|
except LookupError:
|
2014-10-21 02:27:39 -06:00
|
|
|
raise CommandError('Satellite with Identifier {} does not exist'.format(item))
|
|
|
|
|
2014-10-16 07:37:35 -06:00
|
|
|
try:
|
|
|
|
obj = Satellite.objects.get(norad_cat_id=item)
|
2017-11-17 07:31:21 -07:00
|
|
|
except Satellite.DoesNotExist:
|
2014-10-16 07:37:35 -06:00
|
|
|
obj = Satellite(norad_cat_id=item)
|
|
|
|
|
2019-09-15 12:53:58 -06:00
|
|
|
obj.name = tle[0]
|
2014-10-25 15:47:11 -06:00
|
|
|
obj.tle0 = tle[0]
|
|
|
|
obj.tle1 = tle[1]
|
|
|
|
obj.tle2 = tle[2]
|
2014-10-16 07:37:35 -06:00
|
|
|
obj.save()
|
|
|
|
|
|
|
|
self.stdout.write('fetched data for {}: {}'.format(obj.norad_cat_id, obj.name))
|