This forum is now read-only. Please use our new forums! Go to forums

banner
Close banner
0 points
Submitted by Saulius Petreikis
almost 10 years

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

Permalink

In your function parse_station_json at the if url in station, url shoul be ‘url’

points
Submitted by Ellen Robinson
over 9 years

1 comments

Saulius Petreikis over 9 years

Thanks, everything works well now:)