forked from UESTC-Ingress/IFSolver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownloadUtils.py
More file actions
54 lines (48 loc) · 1.69 KB
/
DownloadUtils.py
File metadata and controls
54 lines (48 loc) · 1.69 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
import re
import requests
import os
from collections import OrderedDict
from multiprocessing.pool import ThreadPool
def fetch_url(entry):
ret = ""
path = 'data/' + str(entry["id"]) + ".jpg"
if not os.path.exists(path):
r = requests.get(entry["Image"], stream=True)
if r.status_code == 200:
with open(path, 'wb') as f:
for chunk in r:
f.write(chunk)
ret = "Downloaded image id " + str(entry["id"])
return ret
def getPortals(portals_file):
portal_list = []
cnt = 0
with open(portals_file, encoding="utf-8", newline='', errors="replace") as csvfile:
for line in csvfile:
tmp = OrderedDict()
match = re.match(r'^\"([\S\s]+)\"\,(\d+\.\d+)\,(\d+.\d+)\,\"(.*)\"$', line)
if match:
if match.group(4) is not '':
if ',' in match.group(1):
s = re.sub(r'\,', '%u002c', match.group(1))
else:
s = match.group(1)
s = re.sub(r'[\\/:\*\?\"<>\|]', '', s)
tmp['Name'] = s
tmp['Latitude'] = match.group(2)
tmp['Longitude'] = match.group(3)
tmp['Image'] = match.group(4)
tmp['id'] = cnt
portal_list.append(tmp)
cnt = cnt + 1
return portal_list
def main():
if not os.path.exists('data'):
os.makedirs('data')
portal_list = getPortals("Portal_Export.csv")
run = ThreadPool(12).imap_unordered(fetch_url, portal_list)
for res in run:
if res != "":
print(res)
if __name__ == "__main__":
main()