FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
code-guild/code-guild/python/api-examples/NPRmain.py at master · grantholly/code-guild · GitHub
grantholly
code-guild
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
code-guild
/
code-guild
/
python
/
api-examples
/
NPRmain.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
37 lines (30 loc) · 955 Bytes
Breadcrumbs
code-guild
/
code-guild
/
python
/
api-examples
/
NPRmain.py
Copy path
File metadata and controls
37 lines (30 loc) · 955 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
__author__
=
'grant'
from
urllib2
import
urlopen
from
json
import
load
key
=
"API_KEY"
def
build_api_call
(
key
):
zip_code
=
raw_input
(
"Enter your zip code:"
)
url
=
'http://api.npr.org/stations?apiKey='
+
key
url
+=
'&format=json'
url
+=
"&zip="
+
zip_code
return
url
def
call_station_api
(
url
):
response
=
urlopen
(
url
)
j
=
load
(
response
)
return
j
def
parse_station_json
(
json_obj
):
for
station
in
json_obj
[
'station'
]:
print
(
station
[
'callLetters'
][
'$text'
]
+
": "
+
station
[
'marketCity'
][
'$text'
]
+
", "
+
station
[
'state'
][
'$text'
])
print
"Frequency:"
+
station
[
'frequency'
][
'$text'
]
+
station
[
'band'
][
'$text'
]
if
'url'
in
station
:
print
"MP3 streams"
if
'url'
in
station
:
for
link
in
station
[
"url"
]:
if
link
[
"type"
]
==
"Audio MP3 Stream"
:
print
"
\t
"
,
link
[
"title"
]
+
" - "
+
link
[
"$text"
]
url
=
build_api_call
(
key
)
print
"URL: "
,
url
json_obj
=
call_station_api
(
url
)
parse_station_json
(
json_obj
)
Back
|
FazBrowse Home
|
New Git URL