-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraper.py
69 lines (50 loc) · 1.76 KB
/
scraper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Copyright (c) 2020 Jonathan Hildenbrand
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
import click
import tqdm
from data_scraper import factory
from data_scraper import database as db
from data_scraper import openfda
db_options = click.Choice(['pgsql', 'sqlite'])
source_options = click.Choice(
['recalls', 'adverseevents', 'inspectionclassifications', 'inspectioncitations', 'importrefusals']
)
@click.group()
def cli():
pass
@cli.command(help='Initialize a PostgreSQL or SQLite database.')
@click.option('--database', '-db', type=db_options)
def init(database):
session = None
if database == 'pgsql':
db._pgsql_env_file()
username, password = db._pgsql_get_vars()
pgsql_conn = db._pgsql_conn(username, password)
session = db.create_session(pgsql_conn, create_all=True)
click.echo('PostgreSQL database successfully created.')
if database == 'sqlite':
sqlite_conn = db._sqlite_conn()
session = db.create_session(sqlite_conn)
return session
@cli.command(help='Retrieve data from API sources.')
@click.option('--source', '-s', type=source_options, multiple=True)
@click.option('--database', '-db', type=db_options)
@click.option('--filepath', '-f', type=str)
def scrape(source, database, filepath):
# Create a new session to the chosen database.
session = db.create_session(database=database, path=filepath)
if 'recalls' in source:
recall = factory.create('OPENFDA_RECALL')
recall.get_metadata()
recall.get_events()
recall.to_db(session)
@cli.command(help='Run machine learning models.')
def model():
pass
@cli.command(help='Start the recalls dashboard.')
def start():
pass
if __name__ == '__main__':
cli()