-
Notifications
You must be signed in to change notification settings - Fork 0
/
readjsonfromurl.py
46 lines (36 loc) · 1.42 KB
/
readjsonfromurl.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
"""""
Author: Ai-Linh Alten
Date created: 8/2/2018
Date last modified: 8/10/2018
Python Version: 2.7.15
"""
import requests, zipfile, io, json
remote_links = ['https://github.com/aalten77/WildfiresPilot/raw/master/data/Tubbs/Fountaingrove/Fountaingrove.json.zip',
'https://github.com/aalten77/WildfiresPilot/raw/master/data/Tubbs/Fountaingrove/Fountaingrove_v2.json.zip',
'https://github.com/aalten77/WildfiresPilot/raw/master/data/Tubbs/Fountaingrove/Fountaingrove_v3.json.zip']#,
#'https://github.com/aalten77/WildfiresPilot/raw/master/data/Tubbs/Fountaingrove/fake.json.zip']
def get_remote_links():
return remote_links
def get_json_remote(link):
"""
Fetch json from URL.
:param link: remote https link to pull json. Can be .json or .json.zip extension.
:return: loaded json
"""
r = requests.get(link)
if r.status_code == 404: #if bad request, exit
return -1
#if the link is a zip, then unzip and load json. Else, just load the json.
if link.find('.zip') != -1:
z = zipfile.ZipFile(io.BytesIO(r.content))
print z.printdir()
file = z.open(link.split('/')[-1].replace('.zip',''))
js = json.load(file)
else:
js = json.loads(r.content)
return js
def fetch_jsons():
js_list = []
for link in remote_links:
js_list.append(get_json_remote(link))
return js_list