-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
76 lines (56 loc) · 1.88 KB
/
Copy pathparser.py
File metadata and controls
76 lines (56 loc) · 1.88 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
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
import csv, os, requests
from collections import Counter
from progress.bar import Bar
os.system('clear')
csv_data_filename = 'location_data.csv'
data_output_filename = 'parsed_data.txt'
data_collection_frequency = 300
API_KEY = ''
locations = []
if os.path.exists(data_output_filename):
os.remove(data_output_filename)
f = open(data_output_filename, "x")
line_count = len(open(csv_data_filename).readlines( ))
bar = Bar('Processing location data ', max=line_count)
with open(csv_data_filename) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for row in csv_reader:
try:
location = requests.get(f'http://api.positionstack.com/v1/reverse?access_key={API_KEY}&query={row[3]},{row[4]}').json()
location = location['data'][0]['name']
os.system('clear')
bar.next()
print('\n')
print(f'Found location near {row[3]},{row[4]} : {location}')
locations.append(location)
except:
pass
bar.finish()
results = [item for items, c in Counter(locations).most_common()
for item in [items] * c]
locations.clear()
for location in results:
locations.append(f'{location},{results.count(location)}')
results = [i for n, i in enumerate(locations) if i not in locations[:n]]
os.system('clear')
if os.path.exists(data_output_filename):
pass
else:
f = open(data_output_filename, "x")
print('Locations in dataset, sorted by frequency:\n\n')
for location in results:
instance_count = int(location.split(",")[-1])
time = instance_count * data_collection_frequency
day = time // (24 * 3600)
time = time % (24 * 3600)
hour = time // 3600
time %= 3600
minute = time // 60
if day == 0 and hour == 0 and minute < 45:
pass
else:
data = f'{(location.split(",")[0])}: in dataset {instance_count} times ({int(day)} days, {int(hour)} hours, {int(minute)} minutes)\n'
print(data)
f = open(data_output_filename, "a")
f.write(data)
f.close()