from data_collection.management.commands import BaseShpStationsShpDistrictsImporter

"""
Lichfield publish their data on data.gov.uk as zipped shp files

I've uploaded the data to Amazon S3 for import purposes

Additionally there's a hashes only scraper at
https://morph.io/wdiv-scrapers/DC-PollingStations-Lichfield
polling the URLs to look for changes.
"""

class Command(BaseShpStationsShpDistrictsImporter):
    srid = 27700
    council_id = 'E07000194'
    districts_name = 'local.staffordshire.2017-05-04/Lichfield District Council Polling Districts Shapefile/Lichfield_District_Council_Polling_Districts'
    stations_name = 'local.staffordshire.2017-05-04/LDC_Polling_Stations_Shapefile/Lichfield_District_Council_Polling_Station_Locations.shp'
    elections = ['local.staffordshire.2017-05-04']

    def district_record_to_dict(self, record):
        return {
            'internal_council_id': str(record[4]).strip(),
            'name': str(record[4]).strip(),
            'polling_station_id': str(record[4]).strip(),
        }

    def station_record_to_dict(self, record):
        address = "\n".join([
            str(record[1]).strip(),
            str(record[4]).strip(),
        ])
        postcode = str(record[5]).strip()
        codes = [record[9].strip(), record[10].strip(), record[11].strip()]

        stations = []
        for code in codes:
            if code != b'':
                stations.append({
                    'internal_council_id': str(code),
                    'postcode'           : postcode,
                    'address'            : address,
                })
        return stations

from data_collection.management.commands import BaseApiShpZipStationsShpZipDistrictsImporter

class Command(BaseApiShpZipStationsShpZipDistrictsImporter):
    srid = 27700
    districts_srid = 27700
    council_id = 'E07000194'
    districts_url = 'https://www.lichfielddc.gov.uk/Inspire-data-sets/Lichfield%20District%20Council%20Polling%20Districts/Lichfield%20District%20Council%20Polling%20Districts%20Shapefile.zip'
    stations_url = 'https://www.lichfielddc.gov.uk/Inspire-data-sets/Lichfield%20District%20Council%20Polling%20Stations/LDC_Polling_Stations_Shapefile.zip'
    elections = ['local.staffordshire.2017-05-04']

    def district_record_to_dict(self, record):
        return {
            'internal_council_id': str(record[4]).strip(),
            'name': str(record[4]).strip(),
            'polling_station_id': str(record[4]).strip(),
        }

    def station_record_to_dict(self, record):
        address = "\n".join([
            str(record[1]).strip(),
            str(record[4]).strip(),
        ])
        postcode = str(record[5]).strip()
        codes = [record[9].strip(), record[10].strip(), record[11].strip()]

        stations = []
        for code in codes:
            if code != b'':
                stations.append({
                    'internal_council_id': str(code),
                    'postcode'           : postcode,
                    'address'            : address,
                })
        return stations

convert Lichfield import script to fetch data from web instead of S3