This forum is now read-only. Please use our new forums! Go to forums
banner
Close banner
Can't get any of MP3 streaming links printed out.
Hi all
Everything works fine, but I simply can’t get any of MP3 streaming links printed out using every zip-code example given in section “Print Station Audio Streams”.
Here is my code:
from urllib2 import urlopen
from json import load
key = "API_KEY"
def build_api_call(key):
url = "http://api.npr.org/stations?apiKey="
url += key + '&format=json'
zip_code = raw_input("Enter your zip code:")
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: "
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)
I have tried all zip-codes provided in the task instructions, but only this (code below) is printed out to the console:
Enter your zip code: 10001
URL: http://api.npr.org/stations?apiKey=API_KEY&format=json&zip=10001
WNYC: New York, NY
Frequency:93.9FM
WFUV: New York, NY
Frequency:90.7FM
WBGO: Newark, NJ
Frequency:88.3FM
WNYE: Brooklyn, NY
Frequency:91.5FM
WQXR: Newark, NJ
Frequency:105.9FM
WNYC: New York, NY
Frequency:820AM
WBAI: New York, NY
Frequency:99.5FM
WFUV: New York, NY
Frequency:90.7FM
WNPR: Meriden, CT
Frequency:90.5FM
WSHU: Fairfield, CT
Frequency:91.1FM
WSTC: Stamford, CT
Frequency:1400AM
WNJP: Sussex, NJ
Frequency:88.5FM
WSHU: Westport, CT
Frequency:1260AM
WYBC: New Haven, CT
Frequency:1340AM
WNLK: Norwalk, CT
Frequency:1350AM
WEDW: Stamford, CT
Frequency:88.5FM
None
All your remarks will be appreciated:)
Answer 53d134298c1ccc65bd0001b7
1 vote
In your function parse_station_json at the if url in station, url shoul be ‘url’
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
1 comments
Thanks, everything works well now:)