Exemplo n.º 1
0
    def handle_file(self, open_file):
        self.route = None
        self.trip = None
        self.stop_times = []
        self.notes = []

        # detect encoding
        detector = UniversalDetector()
        for line in open_file:
            detector.feed(line)
            if detector.done:
                break
        detector.close()
        encoding = detector.result['encoding']

        open_file.seek(0)

        # stops
        stops = {}
        for line in open_file:
            identity = line[:2]
            if identity == b'QL' or identity == b'QB':
                stop_code = line[3:15].decode().strip()
                if identity == b'QL':
                    name = line[15:63]
                    if name and stop_code not in stops:
                        name = name.decode(encoding).strip()
                        stops[stop_code] = StopPoint(atco_code=stop_code,
                                                     common_name=name,
                                                     active=True)
                else:
                    easting = line[15:23].strip()
                    northing = line[23:31].strip()
                    if easting:
                        stops[stop_code].latlong = Point(
                            int(easting),
                            int(northing),
                            srid=29902  # Irish Grid
                        )
        existing_stops = StopPoint.objects.in_bulk(stops.keys())
        stops_to_update = []
        new_stops = []
        for stop_code in stops:
            if stop_code in existing_stops:
                stops_to_update.append(stops[stop_code])
            else:
                new_stops.append(stops[stop_code])
        StopPoint.objects.bulk_update(stops_to_update,
                                      fields=['common_name', 'latlong'])
        StopPoint.objects.bulk_create(new_stops)

        open_file.seek(0)

        # everything else
        previous_line = None
        for line in open_file:
            self.handle_line(line, previous_line, encoding)
            previous_line = line
Exemplo n.º 2
0
 def setUpTestData(cls):
     Region.objects.create(id='EA', name='East Anglia')
     DataSource.objects.create(name='Arriva')
     Operator.objects.create(region_id='EA', id='ANWE')
     StopPoint.objects.bulk_create([
         StopPoint(pk='069000023592', active=True, latlong=Point(0, 0)),
         StopPoint(pk='0690WNA02877', active=True, latlong=Point(0, 0)),
         StopPoint(pk='0690WNA02861', active=True, latlong=Point(0, 0)),
     ])
Exemplo n.º 3
0
def do_stops(archive):
    stops = {}
    admin_areas = {}
    stops_not_created = {}
    for line in read_file(archive, 'stops.txt'):
        stop_id = get_stop_id(line['stop_id'])
        if stop_id[0] in '78' and len(stop_id) <= 16:
            stops[stop_id] = StopPoint(atco_code=stop_id,
                                       latlong=Point(float(line['stop_lon']),
                                                     float(line['stop_lat'])),
                                       common_name=line['stop_name'][:48],
                                       locality_centre=False,
                                       active=True)
        else:
            stops_not_created[stop_id] = line['stop_name'][:48]
    existing_stops = StopPoint.objects.in_bulk(stops)
    stops_to_create = [
        stop for stop in stops.values() if stop.atco_code not in existing_stops
    ]

    for stop in stops_to_create:
        admin_area_id = stop.atco_code[:3]
        if admin_area_id not in admin_areas:
            admin_areas[admin_area_id] = AdminArea.objects.filter(
                id=admin_area_id).exists()
        if admin_areas[admin_area_id]:
            stop.admin_area_id = admin_area_id

    StopPoint.objects.bulk_create(stops_to_create)
    return StopPoint.objects.in_bulk(stops), stops_not_created
Exemplo n.º 4
0
 def test_uk_train(self):
     stop = StopPoint(atco_code='9100FLKSTNC')
     with vcr.use_cassette('data/vcr/uk_train.yaml'):
         departures, max_age = live.get_departures(stop, ())
         departures = departures['departures'].get_departures()
     self.assertEqual(30, max_age)
     self.assertEqual(departures[0]['live'], 'Cancelled')
     self.assertEqual(departures[2]['live'], 'Cancelled')
    def setUpTestData(cls):
        cls.ea = Region.objects.create(pk='EA', name='East Anglia')
        cls.w = Region.objects.create(pk='W', name='Wales')
        cls.gb = Region.objects.create(pk='GB', name='Großbritannien')
        cls.sc = Region.objects.create(pk='S', name='Scotland')
        Region.objects.bulk_create([
            Region(pk='NE', name='North East'),
            Region(pk='NW', name='North West'),
            Region(pk='IM', name='Isle of Man')
        ])

        cls.fecs = Operator.objects.create(pk='FECS',
                                           region_id='EA',
                                           name='First in Norfolk & Suffolk')
        Operator.objects.create(id='bus-vannin',
                                region_id='EA',
                                name='Bus Vannin')
        cls.megabus = Operator.objects.create(pk='MEGA',
                                              region_id='GB',
                                              name='Megabus')
        cls.fabd = Operator.objects.create(pk='FABD',
                                           region_id='S',
                                           name='First Aberdeen')

        nocs = DataSource.objects.create(name='National Operator Codes')
        OperatorCode.objects.create(operator=cls.megabus,
                                    source=nocs,
                                    code='MEGA')
        OperatorCode.objects.create(operator=cls.fabd,
                                    source=nocs,
                                    code='FABD')

        source = DataSource.objects.create(
            name='EA', url='https://www.example.co.uk/open-data')
        OperatorCode.objects.create(operator=cls.fecs,
                                    source=source,
                                    code='FECS')

        StopPoint.objects.bulk_create(
            StopPoint(atco_code=atco_code,
                      locality_centre=False,
                      active=True,
                      common_name=common_name,
                      indicator=indicator,
                      latlong=Point(lng, lat, srid=4326))
            for atco_code, common_name, indicator, lat, lng in (
                ('639004572', 'Bulls Head', 'adj', -2.5042125060,
                 53.7423055225),
                ('639004562', 'Markham Road', 'by"', -2.5083672338,
                 53.7398252112),
                ('639004554', 'Witton Park', 'opp', -2.5108434749,
                 53.7389877672),
                ('639004552', 'The Griffin', 'adj', -2.4989239373,
                 53.7425523688),
                ('049004705400', 'Kingston District Centre', 'o/s', 0, 0),
                ('1000DDDV4248', 'Dinting Value Works', '', 0, 0),
                ('2900A181', '', '', 0, 0),
            ))
    def setUpTestData(cls):
        cls.ni = Region.objects.create(pk='NI', name='Northern Ireland')
        cls.gle = Operator.objects.create(pk='GLE',
                                          name='Goldline Express',
                                          region=cls.ni)

        StopPoint.objects.bulk_create(
            StopPoint(atco_code, latlong=Point(0, 0), active=True)
            for atco_code in ('700000015363', '700000015687', '700000004923',
                              '700000005645'))
    def setUpTestData(cls):
        cls.ea = Region.objects.create(pk='EA', name='East Anglia')

        StopPoint.objects.bulk_create(
            StopPoint(atco_code, latlong=Point(0, 0), active=True)
            for atco_code in ('1100DEB10368', '1100DEC10085', '1100DEC10720',
                              '1100DEB10354', '2900A181', '2900S367',
                              '2900N12106', '0500HSTIV002'))

        clean_up()
Exemplo n.º 8
0
 def test_dublin(self):
     stop = StopPoint(atco_code='8220DB07602')
     with vcr.use_cassette('data/vcr/dublin.yaml'):
         departures, max_age = live.get_departures(stop, ())
     self.assertEqual(max_age, 60)
     self.assertEqual(len(departures['departures']), 12)
     self.assertEqual(
         departures['departures'][4], {
             'time': datetime(2017, 6, 5, 12, 43),
             'live': datetime(2017, 6, 5, 12, 35, 58),
             'destination': 'Dublin Airport',
             'service': '16'
         })
Exemplo n.º 9
0
    def do_stops(transxchange_stops):
        stops = StopPoint.objects.in_bulk(transxchange_stops.keys())
        stops_to_create = {
            atco_code: StopPoint(atco_code=atco_code,
                                 common_name=str(stop)[:48],
                                 active=True)
            for atco_code, stop in transxchange_stops.items()
            if atco_code not in stops
        }
        if stops_to_create:
            StopPoint.objects.bulk_create(stops_to_create.values())
            stops = {**stops, **stops_to_create}

        return stops
Exemplo n.º 10
0
    def do_stops(transxchange_stops):
        stops = StopPoint.objects.in_bulk(transxchange_stops.keys())
        stops_to_create = {}
        for atco_code, stop in transxchange_stops.items():
            if atco_code not in stops:
                if atco_code.startswith('000'):
                    stops[atco_code] = str(stop)[:255]
                else:
                    stops_to_create[atco_code] = StopPoint(
                        atco_code=atco_code,
                        common_name=str(stop)[:48],
                        active=True)

        if stops_to_create:
            StopPoint.objects.bulk_create(stops_to_create.values())
            stops = {**stops, **stops_to_create}

        return stops
Exemplo n.º 11
0
 def test_translink_metro_no_services_running(self):
     with vcr.use_cassette('data/vcr/translink_metro.yaml',
                           match_on=['body']):
         departures = live.AcisHorizonDepartures(
             StopPoint(pk='700000000748'), ())
         self.assertEqual([], departures.get_departures())
Exemplo n.º 12
0
 def setUpTestData(cls):
     region = Region.objects.create(id='NW', name='North West')
     operator = Operator.objects.create(region=region,
                                        id='HATT',
                                        name='Hattons of Huyton')
     service = Service.objects.create(line_name='156',
                                      service_code='156',
                                      date='2020-01-01',
                                      current=True)
     service.operator.add(operator)
     StopPoint.objects.bulk_create([
         StopPoint(atco_code='1800EB00151', active=True),
         StopPoint(atco_code='1800EB06881', active=True),
         StopPoint(atco_code='1800EB13721', active=True),
         StopPoint(atco_code='1800NF28251', active=True),
         StopPoint(atco_code='1800NF28261', active=True),
         StopPoint(atco_code='1800NF28271', active=True),
         StopPoint(atco_code='1800NF28281', active=True),
         StopPoint(atco_code='1800NF28291', active=True),
         StopPoint(atco_code='1800NF28301', active=True),
         StopPoint(atco_code='1800NF28541', active=True),
         StopPoint(atco_code='1800NF28551', active=True),
         StopPoint(atco_code='1800NF28781', active=True),
         StopPoint(atco_code='1800NF28791', active=True),
         StopPoint(atco_code='1800NF28801', active=True),
         StopPoint(atco_code='1800NF28811', active=True),
         StopPoint(atco_code='1800NF28821', active=True),
         StopPoint(atco_code='1800NF28831', active=True),
         StopPoint(atco_code='1800NF28841', active=True),
         StopPoint(atco_code='1800NF28851', active=True),
         StopPoint(atco_code='1800NF28861', active=True),
         StopPoint(atco_code='1800NF28931', active=True),
         StopPoint(atco_code='1800NF28941', active=True),
         StopPoint(atco_code='1800NF28951', active=True),
         StopPoint(atco_code='1800NF28961', active=True),
         StopPoint(atco_code='1800NF28971', active=True),
         StopPoint(atco_code='1800NF28981', active=True),
         StopPoint(atco_code='1800SB02041', active=True),
         StopPoint(atco_code='1800SB05841', active=True),
         StopPoint(atco_code='1800SB12051', active=True),
         StopPoint(atco_code='1800SB12491', active=True),
         StopPoint(atco_code='1800SB14291', active=True),
         StopPoint(atco_code='1800SB14301', active=True),
         StopPoint(atco_code='1800SB15431', active=True),
         StopPoint(atco_code='1800SB33551', active=True),
         StopPoint(atco_code='1800SB33701', active=True),
         StopPoint(atco_code='1800SB33721', active=True),
         StopPoint(atco_code='1800SB33731', active=True),
         StopPoint(atco_code='1800SB33741', active=True),
         StopPoint(atco_code='1800SB33751', active=True),
         StopPoint(atco_code='1800SB33781', active=True),
         StopPoint(atco_code='1800SB33791', active=True),
         StopPoint(atco_code='1800SB33801', active=True),
         StopPoint(atco_code='1800SB33811', active=True),
         StopPoint(atco_code='1800SB33821', active=True),
         StopPoint(atco_code='1800SB33841', active=True),
         StopPoint(atco_code='1800SB33851', active=True),
         StopPoint(atco_code='2800S11031B', active=True),
         StopPoint(atco_code='2800S11050A', active=True),
         StopPoint(atco_code='2800S11051B', active=True),
         StopPoint(atco_code='2800S11052A', active=True),
         StopPoint(atco_code='2800S11053A', active=True),
         StopPoint(atco_code='2800S11085A', active=True),
         StopPoint(atco_code='2800S46043C', active=True),
         StopPoint(atco_code='2800S46075A', active=True),
         StopPoint(atco_code='2800S46075B', active=True),
         StopPoint(atco_code='2800S61011A', active=True),
         StopPoint(atco_code='2800S61012B', active=True),
         StopPoint(atco_code='2800S61012C', active=True),
         StopPoint(atco_code='2800S61013A', active=True),
         StopPoint(atco_code='2800S61013B', active=True),
     ])
     StopUsage.objects.create(service=service,
                              stop_id='2800S11053A',
                              order=69)
     DataSource.objects.create(name='Transport for the North',
                               settings={
                                   'app_id': 'hen hom',
                                   'app_key': 'roger poultry'
                               })
Exemplo n.º 13
0
 def handle_location(cls, line):
     atco_code = line[3:15]
     if atco_code in cls.deferred_stop_codes:
         common_name = line[15:].strip()
         cls.deferred_stops[atco_code] = StopPoint(atco_code=atco_code,
                                                   common_name=common_name)