-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
82 lines (72 loc) · 2.81 KB
/
main.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
#!/use/bin/python
import argparse
from contextlib import closing
import csv
from dateutil import rrule
from datetime import date, datetime, timezone
import requests
import sys
import time
SAMDI_URL = "http://www.marinedebris.engr.uga.edu/mdtapp/getLoggedData.php"
SAMDI_LIST = 22
def make_requests(ini_date, end_date, file_name):
dates = list(rrule.rrule(rrule.MONTHLY, dtstart=ini_date, until=end_date))
dates.append(end_date)
# see form details at http://www.marinedebris.engr.uga.edu/newmap/
form_data = {
'lists[]': SAMDI_LIST,
'from': None,
'to': None,
# unused fields
'username': None,
'password': None,
'keywords': None,
}
keys = {
"altitude",
"description",
"id",
"item_id",
"iteminstance_description",
"itemlist_id",
"itemname",
"latitude",
"longitude",
"listname",
"location",
"material_id",
"quantity",
"radius",
"timestamp",
"date"
}
with open(file_name, 'w', newline='', encoding="utf-8") as dest_file:
writer = csv.DictWriter(dest_file, keys, delimiter=';', quotechar='"', restval='')
writer.writeheader()
for start, end in zip(dates, dates[1:]):
form_data['from'] = int(start.timestamp()*1000)
form_data['to'] = int(end.timestamp()*1000)
with closing(requests.post(SAMDI_URL, data=form_data, stream=True)) as r:
if r.status_code != 200:
raise Exception("Problems requesting the data.")
data_dict = r.json()['data']
for d in data_dict:
if d['timestamp'] is not None:
d['date'] = d['timestamp'][:4] + '-' + d['timestamp'][4:6] + '-' + d['timestamp'][6:8] + ' ' + d['timestamp'][8:10] + ':' + d['timestamp'][10:12] + ':' + d['timestamp'][12:14]
else:
d['date'] = ''
writer.writerows(data_dict)
time.sleep(3)
print("Request finished from %s to %s" % (form_data['from'], form_data['to']))
def main():
parser = argparse.ArgumentParser()
parser.add_argument('ini_date', type=lambda s: datetime.strptime(s, '%m-%d-%Y'))
parser.add_argument('end_date', type=lambda s: datetime.strptime(s, '%m-%d-%Y'))
parser.add_argument("file_name", type=str)
args = parser.parse_args()
args.ini_date = args.ini_date.replace(hour=0, minute=0, second=0, microsecond=0, tzinfo=timezone.utc)
args.end_date = args.end_date.replace(hour=23, minute=59, second=59, microsecond=999999, tzinfo=timezone.utc)
print("Pulling records from %s to %s.." % (args.ini_date, args.end_date))
make_requests(args.ini_date, args.end_date, args.file_name)
if __name__ == "__main__":
main()