-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathptr_batch_submit.py
More file actions
executable file
·42 lines (33 loc) · 1.24 KB
/
ptr_batch_submit.py
File metadata and controls
executable file
·42 lines (33 loc) · 1.24 KB
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
#!/usr/bin/python
#curl -d "@IXMaps_20170315T04_59_43Z_494943.txt" -X POST https://www.ixmaps.ca/application/controller/geoloc_ptr.php
import os
import sys
import string
import requests
import json
def json_validator(data):
try:
json.loads(data)
return True
except ValueError as error:
print("invalid json: %s" % error)
return False
outfile = open('ptr_submission_returns.out', 'r+')
# delete contents of file
outfile.seek(0)
outfile.truncate()
num_files = len([name for name in os.listdir('.')])
for count, filename in enumerate(os.listdir('.'), start=1):
# for index, filename in os.listdir('.'):
if filename.endswith(".txt"):
print(str(100 * float(count)/float(num_files))+'% complete')
route = open(filename, 'r+')
r = requests.post("https://www.ixmaps.ca/application/controller/geoloc_ptr.php", data=route)
print(r.status_code, r.reason)
if r.status_code == 201 and json_validator(r.text):
returned_json = json.loads(r.text)
# unicode
print(returned_json[u'request_id'])
outfile.write(str(returned_json[u'request_id'])+', '+str(r.status_code)+', '+str(r.reason)+'\n')
else:
outfile.write('Problem with file '+str(filename)+'. Returned '+str(r.status_code)+', '+str(r.reason)+'\n')