Skip to content

Commit

Permalink
Quick API fix
Browse files Browse the repository at this point in the history
 The new iHeartRadio API has changed enough that more far-reaching
 changes are needed to do this right. For the moment, most streams
 should at least return a usable URL again.
  • Loading branch information
oldlaptop committed Jul 16, 2017
1 parent 4528a21 commit 9c554c5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
13 changes: 7 additions & 6 deletions iheart-url
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import time
BAD_STATION = "no://station/found"

def dump_station_details (station, dumpfunc = print):
dumpfunc ("station name:", station['streams'][0]['name'])
dumpfunc ("call letters:", station['streams'][0]['call_letters'])
dumpfunc ("location:", station['streams'][0]['city'] + ",", station['streams'][0]['state'] + ",", station['streams'][0]['country'])
dumpfunc ("description:", station['streams'][0]['description'])
dumpfunc ("broadcast format:", station['streams'][0]['format'])
dumpfunc ("station name: %s", station['name'])
dumpfunc ("call letters: %s", station['callLetters'])
dumpfunc ("location: %s", station['markets'][0]['city'] + ", " + station['markets'][0]['stateAbbreviation'] + ", " + station['markets'][0]['country'])
dumpfunc ("description: %s", station['description'])
dumpfunc ("broadcast format: %s", station['format'])

def print_url (station_id):
station = None
Expand Down Expand Up @@ -52,8 +52,9 @@ def print_url (station_id):

try:
station_url = parse_iheart_json.get_station_url (station)
except KeyError:
except KeyError as inst:
logging.error ("stream not found for this station!")
logging.debug (inst)
logging.debug ("full dictionary dump:")
logging.debug (station)
print (BAD_STATION)
Expand Down
18 changes: 11 additions & 7 deletions parse_iheart_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,17 @@ def station_info (station_id):
# http://proxy.iheart.com/streams_list_by_ids/?stream_ids=STATION_ID_NUMBER&apikey=null

# The base URL for our API request (%s is for string substitution)
iheart_base_url = 'http://proxy.iheart.com/streams_list_by_ids/?stream_ids=%s&apikey=null'
iheart_base_url = "http://api.iheart.com/api/v2/content/liveStations/%s"

response = urllib.request.urlopen (iheart_base_url % station_id)

# We assume we're dealing with UTF-8 encoded JSON, if we aren't the API
# has probably changed in ways we can't deal with.
assert (response.getheader ('Content-Type') == 'application/json')
(response.getheader ('Content-Type') == 'application/json;charset=UTF-8')

station = json.loads (response.read ().decode('utf-8'))
# TODO: investigate this new "hits" structure, perhaps searching and
# stream-fetching can be unified
station = json.loads (response.read ().decode('utf-8'))['hits'][0]

if (not station['streams']):
raise RuntimeError("station streams list empty")
Expand Down Expand Up @@ -155,7 +157,9 @@ def get_station_url (station):
# rather than a direct HTTP/RTMP URL. Not all media players (notably
# mplayer) can process this directly, so we simply pull the first entry
# out of it and return that.
if ((station['streams'][0]['url'])[-3:] == "pls"):
return depls (station['streams'][0]['url'])
else:
return station['streams'][0]['url']

# TODO: reimplement station detection
if ((station['streams']['pls_stream'])[-3:] == "pls"):
return depls (station['streams']['pls_stream'])
#else:
# return station['streams'][0]

0 comments on commit 9c554c5

Please sign in to comment.