Skip to content

Commit fd63d88

Browse files
committed
Finished Week 6 (JSON and the REST Architecture)
1 parent 5ad056c commit fd63d88

File tree

3 files changed

+263
-0
lines changed

3 files changed

+263
-0
lines changed
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
{
2+
"note":"This file contains the sample data for testing",
3+
"comments":[
4+
{
5+
"name":"Leven",
6+
"count":100
7+
},
8+
{
9+
"name":"Mahdiya",
10+
"count":97
11+
},
12+
{
13+
"name":"Ajayraj",
14+
"count":87
15+
},
16+
{
17+
"name":"Lillian",
18+
"count":86
19+
},
20+
{
21+
"name":"Aon",
22+
"count":86
23+
},
24+
{
25+
"name":"Ruaraidh",
26+
"count":78
27+
},
28+
{
29+
"name":"Gursees",
30+
"count":75
31+
},
32+
{
33+
"name":"Emmanuel",
34+
"count":74
35+
},
36+
{
37+
"name":"Christy",
38+
"count":72
39+
},
40+
{
41+
"name":"Annoushka",
42+
"count":72
43+
},
44+
{
45+
"name":"Inara",
46+
"count":72
47+
},
48+
{
49+
"name":"Caite",
50+
"count":70
51+
},
52+
{
53+
"name":"Rosangel",
54+
"count":70
55+
},
56+
{
57+
"name":"Iana",
58+
"count":66
59+
},
60+
{
61+
"name":"Anise",
62+
"count":66
63+
},
64+
{
65+
"name":"Jaosha",
66+
"count":65
67+
},
68+
{
69+
"name":"Cadyn",
70+
"count":65
71+
},
72+
{
73+
"name":"Edward",
74+
"count":63
75+
},
76+
{
77+
"name":"Charlotte",
78+
"count":61
79+
},
80+
{
81+
"name":"Sammy",
82+
"count":60
83+
},
84+
{
85+
"name":"Zarran",
86+
"count":60
87+
},
88+
{
89+
"name":"Rowen",
90+
"count":59
91+
},
92+
{
93+
"name":"Stanislaw",
94+
"count":59
95+
},
96+
{
97+
"name":"Maighdlin",
98+
"count":57
99+
},
100+
{
101+
"name":"Connan",
102+
"count":56
103+
},
104+
{
105+
"name":"Warrick",
106+
"count":54
107+
},
108+
{
109+
"name":"Diya",
110+
"count":52
111+
},
112+
{
113+
"name":"Lawson",
114+
"count":52
115+
},
116+
{
117+
"name":"Wu",
118+
"count":51
119+
},
120+
{
121+
"name":"Irmak",
122+
"count":47
123+
},
124+
{
125+
"name":"Emilija",
126+
"count":47
127+
},
128+
{
129+
"name":"Kayda",
130+
"count":41
131+
},
132+
{
133+
"name":"Ellenor",
134+
"count":41
135+
},
136+
{
137+
"name":"Kyra",
138+
"count":41
139+
},
140+
{
141+
"name":"Nikita",
142+
"count":38
143+
},
144+
{
145+
"name":"Kaelah",
146+
"count":35
147+
},
148+
{
149+
"name":"Meko",
150+
"count":32
151+
},
152+
{
153+
"name":"Marissa",
154+
"count":31
155+
},
156+
{
157+
"name":"Ayat",
158+
"count":24
159+
},
160+
{
161+
"name":"Sali",
162+
"count":19
163+
},
164+
{
165+
"name":"Hashem",
166+
"count":19
167+
},
168+
{
169+
"name":"Tygan",
170+
"count":18
171+
},
172+
{
173+
"name":"Rioden",
174+
"count":17
175+
},
176+
{
177+
"name":"Cruiz",
178+
"count":16
179+
},
180+
{
181+
"name":"Caoilfinn",
182+
"count":13
183+
},
184+
{
185+
"name":"Ewen",
186+
"count":8
187+
},
188+
{
189+
"name":"Baighley",
190+
"count":7
191+
},
192+
{
193+
"name":"Ramone",
194+
"count":1
195+
},
196+
{
197+
"name":"Kyran",
198+
"count":1
199+
},
200+
{
201+
"name":"Noelani",
202+
"count":1
203+
}
204+
]
205+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import urllib.request as ur
2+
import urllib.parse as up
3+
import json
4+
5+
serviceurl = "http://python-data.dr-chuck.net/geojson?"
6+
# This API only accepts the university in a list of its accepted ones.
7+
# This API uses the same parameters (sensor and address) as the Google API.
8+
# This API also has no rate limit so you can test as often as you like.
9+
# If you visit the URL with no parameters, you get a list of all of the address values which can be used with this API.
10+
11+
address_input = input("Enter location: ")
12+
params = {"sensor": "false", "address": address_input}
13+
url = serviceurl + up.urlencode(params)
14+
print("Retrieving ", url)
15+
data = ur.urlopen(url).read().decode('utf-8')
16+
print('Retrieved', len(data), 'characters')
17+
json_obj = json.loads(data)
18+
19+
place_id = json_obj["results"][0]["place_id"]
20+
print("Place id", place_id)
21+
22+
# Sample Execution
23+
24+
# $ python solution.py
25+
# Enter location: South Federal University
26+
# Retrieving http://...
27+
# Retrieved 2101 characters
28+
# Place id ChIJJ8oO7_B_bIcR2AlhC8nKlok
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import urllib.request as ur
2+
import json
3+
4+
# json_url = 'http://python-data.dr-chuck.net/comments_42.json'
5+
6+
json_url = input("Enter location: ")
7+
print("Retrieving ", json_url)
8+
data = ur.urlopen(json_url).read().decode('utf-8')
9+
print('Retrieved', len(data), 'characters')
10+
json_obj = json.loads(data)
11+
12+
sum = 0
13+
total_number = 0
14+
15+
for comment in json_obj["comments"]:
16+
sum += int(comment["count"])
17+
total_number += 1
18+
19+
print('Count:', total_number)
20+
print('Sum:', sum)
21+
22+
23+
# Sample Execution
24+
#
25+
# $ python solution.py
26+
# Enter location: http://python-data.dr-chuck.net/comments_42.json
27+
# Retrieving http://python-data.dr-chuck.net/comments_42.json
28+
# Retrieved 2733 characters
29+
# Count: 50
30+
# Sum: 2482

0 commit comments

Comments
 (0)