-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrun.py
86 lines (75 loc) · 2.46 KB
/
run.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python
# coding: utf-8
""" Run the scraper against BRÅ
"""
from bra_scraper.interface import Interface
from bra_scraper.BRA import BRA
def main():
""" Entry point when run from command line """
# pylint: disable=C0103
cmd_args = [{
'short': "-t", "long": "--topic",
'dest': "topic",
'type': str,
'help': """name of the topic to be scraped (from http://statistik.bra.se/solwebb/action/start?menykatalogid=1)""",
'default': 'Månads- och kvartalsvis - Kommun och storstädernas stadsdelar 1996-',
}, {
'short': "-r", "long": "--regions",
'dest': "regions",
'type': str,
'help': """a comma seprated list of regions to query by""",
}, {
'short': "-o", "long": "--outfile",
'dest': "outfile",
'type': str,
'help': """store result in this csv file""",
'required': True,
}, {
'short': "-n", "long": "--notes",
'dest': "notes",
'type': str,
'help': """store notes in this csv file"""
}, {
'short': "-ps", "long": "--period_start",
'dest': "period_start",
'default': "1900-01-01",
'type': str,
'help': """start date (for example 2016-09-01)"""
}, {
'short': "-pe", "long": "--period_end",
'dest': "period_end",
'default': "2999-01-01",
'type': str,
'help': """end date (for example 2016-09-01)"""
}]
ui = Interface("Run scraper",
"Fetch data from command line",
commandline_args=cmd_args)
# Init
scraper = BRA(logger=ui)
topic_name = unicode(ui.args.topic, "utf-8")
topic = scraper.topic(topic_name)
# Make query
if not ui.args.regions:
regions = "*"
else:
regions = ui.args.regions.decode("utf-8").split(",")
result = topic.query(
period_start=ui.args.period_start,
period_end=ui.args.period_end,
regions=regions,
)
# Store data
data_file_path = ui.args.outfile
result.data.to_csv(data_file_path)
ui.info(u"Writing to {}".format(unicode(data_file_path,"utf-8")))
#
if not ui.args.notes:
# If note path not defined, write to "mydata_notes.csv" (if "mydata.csv" is data path)
notes_file = data_file_path.replace(".csv", "_notes.csv")
else:
notes_file = ui.args.notes
if result.notes:
result.notes.to_csv(notes_file)
if __name__ == '__main__':
main()