-
Notifications
You must be signed in to change notification settings - Fork 10
/
iheart-url
executable file
·154 lines (134 loc) · 4.72 KB
/
iheart-url
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#! /usr/bin/env python3
import parse_iheart_json
import urllib.error
import argparse
import subprocess
import logging
import time
# Something that will cause a player to error out, written to stdout when we
# can't get a real station URL for some reason
BAD_STATION = "no://station/found"
def dump_station_details (station, dumpfunc = print):
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
try:
station = parse_iheart_json.station_info (station_id, tls)
except urllib.error.HTTPError as inst:
logging.error ("http error from iheart servers:")
logging.error (inst)
print (BAD_STATION)
return
except RuntimeError as inst:
logging.error (inst)
print (BAD_STATION)
return
if (args.verbose):
try:
dump_station_details (station, dumpfunc = logging.debug)
except KeyError:
logging.warning ("a field is missing; please run with -v and submit a github issue")
logging.debug ("full dictionary dump:")
logging.debug (station)
try:
station_url = parse_iheart_json.get_station_url (station, tls)
except KeyError as inst:
if (str (inst) == 'None'):
logging.error ("no valid stream for this station (if tls is off, try turning it on)")
print (BAD_STATION)
return
else:
logging.info ("an unknown stream type is present, please run with -v and submit a github issue")
logging.info ("KeyError instance: %s", inst)
logging.debug ("full dictionary dump:")
logging.debug (station)
print (BAD_STATION)
return
print (station_url)
if (__name__ == '__main__'):
parser = argparse.ArgumentParser (description="Print the audio stream URL for an iheartradio station")
# Optional arguments
parser.add_argument (
'-v', '--verbose',
action='count',
help="Display extra information",
)
parser.add_argument (
'-i', '--info',
action='store_true',
help="Instead of printing the stream URL for the station, output some information about it",
)
parser.add_argument (
'-c', '--cleartext',
action='store_true',
help="Disable TLS when communicating with iHeartRadio, and do not return a TLS stream URL",
)
# The three required arguments; one and only one must be given
action = parser.add_mutually_exclusive_group(required=True)
action.add_argument (
'station_id',
nargs='?',
type=int,
help="The (five-digit?) ID number of the station",
)
action.add_argument (
'-s', '--search',
metavar='TERMS',
help="List station search results for TERMS"
)
action.add_argument (
'-l', '--lucky',
metavar='TERMS',
help="\"I\'m feeling lucky\" search for TERMS (print URL for the first result)"
)
args = parser.parse_args ()
if (args.verbose):
logging.basicConfig (format = "%(levelname)s".lower () + ": %(message)s", level = logging.DEBUG)
else:
logging.basicConfig (format = "%(levelname)s: %(message)s", level = logging.INFO)
tls = not args.cleartext
if (args.search):
results = parse_iheart_json.station_search (args.search, tls)
print ("hits:", results['totalStations'])
for station in results['stations']:
if (args.verbose):
print ("\n-- name:", station['name'], "id:" + str(station['id']))
print ('\t', "callsign:", station['callLetters'])
print ('\t', "frequency:", station['frequency'])
print ('\t', "city:", station['city'])
print ('\t', "state:", station['state'])
print ('\t', "description:", station['description'])
print ('\t', "relevancy score:", station['score'])
else:
print (station['name'], '\t', station['description'], "id:" + str(station['id']))
else:
# Determine station_id, either by the lucky search or from the
# commandline
if (args.lucky):
results = parse_iheart_json.station_search (args.lucky, tls)
# iheart will not necessarily decide that one match is
# "best" for us
if (results['bestMatch'] is not None):
station_id = (results['bestMatch']['id'])
else:
logging.error ("no stations found for \"I'm feeling lucky\" search")
print (BAD_STATION)
exit ()
else:
station_id = args.station_id
# Print info XOR print URL, for the station_id found above
if (args.info):
station = None;
try:
station = parse_iheart_json.station_info (station_id, tls)
dump_station_details (station)
except KeyError:
logging.error ("information field not found (has the iHeartRadio API changed?) please run with -v and submit a github issue")
logging.debug ("full dictionary dump:")
logging.debug (station)
else:
print_url (station_id)