-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from elaguerta/data-cli
#46 Add CLI to run data scrapers
- Loading branch information
Showing
4 changed files
with
76 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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,14 @@ | ||
from typing import Dict, Any | ||
import data_scrapers.alameda_county as alameda_county | ||
|
||
scrapers: Dict[str, Any] = { | ||
'alameda': alameda_county | ||
# 'contra_costa': None, | ||
# 'marin': None, | ||
# 'napa': None, | ||
# 'san_francisco': san_francisco_county, | ||
# 'san_mateo': None, | ||
# 'santa_clara': None, | ||
# 'solano': None, | ||
# 'sonoma': None, | ||
} |
This file contains 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
This file contains 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,2 @@ | ||
source env/bin/activate; | ||
python3 scraper_data.py $@; |
This file contains 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,37 @@ | ||
#!/usr/bin/env python3 | ||
import click | ||
import json | ||
import data_scrapers | ||
from typing import Tuple | ||
from pathlib import Path | ||
|
||
|
||
COUNTY_NAMES : Tuple[str,...]= tuple(data_scrapers.scrapers.keys()) | ||
|
||
|
||
@click.command(help='Create a .json with data for one or more counties. Supported ' | ||
f'counties: {", ".join(COUNTY_NAMES)}.') | ||
@click.argument('counties', metavar='[COUNTY]...', nargs=-1, | ||
type=click.Choice(COUNTY_NAMES, case_sensitive=False)) | ||
@click.option('--output', help='write output file to this directory') | ||
|
||
def main(counties: Tuple[str,...], output:str) -> None: | ||
out = dict() | ||
if len(counties) == 0: | ||
counties = COUNTY_NAMES | ||
|
||
# Run each scraper's get_county() method. Assign the output to out[county] | ||
for county in counties: | ||
out[county] = data_scrapers.scrapers[county].get_county() | ||
|
||
if output: | ||
parent = Path(output) | ||
parent.mkdir(exist_ok = True) # if output directory does not exist, create it | ||
with parent.joinpath('data.json').open('w', encoding='utf-8') as f: | ||
json.dump(out, f, ensure_ascii=False, indent=2) | ||
|
||
else: | ||
print(json.dumps(out,indent=2)) | ||
|
||
if __name__ == '__main__': | ||
main() |