-
Notifications
You must be signed in to change notification settings - Fork 3
Wi p pollingstations refactor #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
GeoWill
wants to merge
11
commits into
master
Choose a base branch
from
WiP-pollingstations-refactor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
68e0cd3
Add deps and empty pollingstations app
GeoWill 39ff861
PollingStationsList and PollingDistrictsList models
GeoWill 1fbd821
Add command and base scrapers
GeoWill 55e2d9e
Arcgis scraper
GeoWill e8d89c0
XML scrapers
GeoWill ff7eba7
GeoJsonScraper and example
GeoWill c21b372
fixup! Arcgis scraper
GeoWill 9e77233
fixup! Add command and base scrapers
GeoWill 73ccad0
fixup! Arcgis scraper
GeoWill e4a0166
Scrapers for AGB and BRT
GeoWill 60c43df
RandomIdGeoJsonScraper and example (CAT)
GeoWill File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| from rich.progress import Progress | ||
|
|
||
| from lgsf.commands.base import PerCouncilCommandBase | ||
| from lgsf.path_utils import load_scraper, load_council_info | ||
|
|
||
|
|
||
| class Command(PerCouncilCommandBase): | ||
| command_name = "polling_stations" | ||
|
|
||
| def add_arguments(self, parser): | ||
| parser.add_argument( | ||
| "--check-only", | ||
| action="store_true", | ||
| help="Just check for updated pages, don't scrape anything", | ||
| ) | ||
| parser.add_argument( | ||
| "--list-missing", | ||
| action="store_true", | ||
| help="Print missing councils", | ||
| ) | ||
| parser.add_argument( | ||
| "--list-disabled", | ||
| action="store_true", | ||
| help="Print disabled councils", | ||
| ) | ||
|
|
||
| def _run_single(self, scraper, progress, summary): | ||
| try: | ||
| progress.console.print(scraper.options["council"]) | ||
| scraper.run() | ||
| summary["completed"] += 1 | ||
| except KeyboardInterrupt: | ||
| raise | ||
| except: | ||
| if self.options.get("verbose"): | ||
| raise | ||
| summary["failed"] += 1 | ||
| progress.console.print( | ||
| "Error running asdasd {}, see {} for more".format( | ||
| self.options["council"], scraper._error_file_name() | ||
| ), | ||
| style="red", | ||
| ) | ||
|
|
||
| def handle(self, options): | ||
| self.options = options | ||
| if options["list_missing"]: | ||
| self.output_missing() | ||
|
|
||
| if options["list_disabled"]: | ||
| self.output_disabled() | ||
|
|
||
| self.output_status() | ||
|
|
||
| self.normalise_codes() | ||
| to_run = self.councils_to_run() | ||
| summary = { | ||
| "completed": 0, | ||
| "missing scraper": 0, | ||
| "failed": 0, | ||
| "skipped": 0, | ||
| } | ||
| with Progress() as progress: | ||
| tasks = { | ||
| "total": progress.add_task(description=f"Total", total=len(to_run)), | ||
| } | ||
|
|
||
| while not progress.finished: | ||
| for council in to_run: | ||
| self.options["council"] = council | ||
| self.options["council_info"] = load_council_info(council) | ||
| scraper_cls = load_scraper(council, self.command_name) | ||
| if not scraper_cls: | ||
| summary["missing scraper"] += 1 | ||
| continue | ||
| with scraper_cls((self.options), progress.console) as scraper: | ||
| should_run = True | ||
| if scraper.disabled: | ||
| should_run = False | ||
|
|
||
| if should_run and options["refresh"]: | ||
| if scraper.run_since(): | ||
| should_run = False | ||
|
|
||
| if should_run and options["tags"]: | ||
| required_tags = set(options["tags"].split(",")) | ||
| scraper_tags = set(scraper.get_tags) | ||
| if not required_tags.issubset(scraper_tags): | ||
| should_run = False | ||
|
|
||
| if should_run: | ||
| self._run_single(scraper, progress, summary) | ||
| else: | ||
| summary["skipped"] += 1 | ||
|
|
||
| progress.update(tasks["total"], advance=1) | ||
| self.console.print(summary) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import json | ||
|
|
||
|
|
||
| class PollingStationsList: | ||
| def __init__(self, stations): | ||
| self.stations = stations | ||
|
|
||
| def as_file_name(self): | ||
| return "stations" | ||
|
|
||
| def as_json(self): | ||
| return json.dumps(self.stations, indent=4) | ||
|
|
||
|
|
||
| class PollingDistrictsList: | ||
| def __init__(self, districts): | ||
| self.districts = districts | ||
|
|
||
| def as_file_name(self): | ||
| return "districts" | ||
|
|
||
| def as_json(self): | ||
| return json.dumps(self.districts, indent=4) |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import json | ||
| from arcgis2geojson import arcgis2geojson | ||
|
|
||
|
|
||
| from lgsf.polling_stations.scrapers.common import PollingStationScraperBase | ||
|
|
||
|
|
||
| class ArcGisScraper(PollingStationScraperBase): | ||
| encoding = "utf-8" | ||
| key = "OBJECTID" | ||
|
|
||
| def make_geometry(self, feature): | ||
| return json.dumps(arcgis2geojson(feature), sort_keys=True) | ||
|
|
||
| def get_data(self, url): # pragma: no cover | ||
| response = self.get(url) | ||
| data_str = response.content | ||
| data = json.loads(data_str.decode(self.encoding)) | ||
| return (data_str, data) | ||
|
|
||
| def process_feature(self, feature, fields=None): | ||
| # assemble record | ||
| record = { | ||
| "council_id": self.council_id, | ||
| "geometry": self.make_geometry(feature), | ||
| } | ||
| for field in fields: | ||
| value = feature["attributes"][field["name"]] | ||
| if isinstance(value, str): | ||
| record[field["name"]] = value.strip() | ||
| else: | ||
| record[field["name"]] = value | ||
| return record | ||
|
|
||
| def scrape(self, url, type="features"): | ||
| # load json | ||
| data_str, data = self.get_data(url) | ||
| print(f"found {len(data['features'])} {type}") | ||
|
|
||
| # grab field names | ||
| fields = data["fields"] | ||
| features = data["features"] | ||
|
|
||
| return self.process_features(features, fields) | ||
|
|
||
| # print summary | ||
| # summarise(self.table) | ||
|
|
||
| # self.store_history(data_str, self.council_id) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feels like a large chunk of this file could be refactored in to a common file - it's largely the same as the councillors one, and it would be good to keep the outputs and interfaces as similar as we can I think.
This can be an issue of course, not needed for this PR