-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.py
300 lines (238 loc) · 10.5 KB
/
main.py
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import sys
import urllib
import urllib2
import urlparse
import xbmcgui
import xbmcplugin
import xbmcaddon
import xbmcvfs
import json
import base64
addonID = 'plugin.audio.radiobrowser'
addon = xbmcaddon.Addon(id=addonID)
base_url = sys.argv[0]
addon_handle = int(sys.argv[1])
args = urlparse.parse_qs(sys.argv[2][1:])
xbmcplugin.setContent(addon_handle, 'songs')
my_stations = {}
profile = xbmc.translatePath(addon.getAddonInfo('profile')).decode("utf-8")
mystations_path = profile+'/mystations.json'
import socket
import random
def get_radiobrowser_base_urls():
"""
Get all base urls of all currently available radiobrowser servers
Returns:
list: a list of strings
"""
hosts = []
# get all hosts from DNS
ips = socket.getaddrinfo('all.api.radio-browser.info',
80, 0, 0, socket.IPPROTO_TCP)
for ip_tupple in ips:
ip = ip_tupple[4][0]
# do a reverse lookup on every one of the ips to have a nice name for it
host_addr = socket.gethostbyaddr(ip)
# add the name to a list if not already in there
if host_addr[0] not in hosts:
hosts.append(host_addr[0])
# sort list of names
random.shuffle(hosts)
# add "https://" in front to make it an url
xbmc.log("Found hosts: " + ",".join(hosts))
return list(map(lambda x: "https://" + x, hosts))
def LANGUAGE(id):
# return id
# return "undefined"
return addon.getLocalizedString(id).encode('utf-8')
def build_url(query):
return base_url + '?' + urllib.urlencode(query)
def addLink(stationuuid, name, url, favicon, bitrate):
li = xbmcgui.ListItem(name, iconImage=favicon)
li.setProperty('IsPlayable', 'true')
li.setInfo(type="Music", infoLabels={ "Title":name, "Size":bitrate})
localUrl = build_url({'mode': 'play', 'stationuuid': stationuuid})
if stationuuid in my_stations:
contextTitle = LANGUAGE(32009)
contextUrl = build_url({'mode': 'delstation', 'stationuuid': stationuuid})
else:
contextTitle = LANGUAGE(32010)
contextUrl = build_url({'mode': 'addstation', 'stationuuid': stationuuid, 'name': name.encode('utf-8'), 'url': url, 'favicon': favicon, 'bitrate': bitrate})
li.addContextMenuItems([(contextTitle, 'RunPlugin(%s)'%(contextUrl))])
xbmcplugin.addDirectoryItem(handle=addon_handle, url=localUrl, listitem=li, isFolder=False)
def downloadFile(uri, param):
"""
Download file with the correct headers set
Returns:
a string result
"""
paramEncoded = None
if param != None:
paramEncoded = json.dumps(param)
xbmc.log('Request to ' + uri + ' Params: ' + ','.join(param))
else:
xbmc.log('Request to ' + uri)
req = urllib2.Request(uri, paramEncoded)
req.add_header('User-Agent', 'KodiRadioBrowser/1.2.0')
req.add_header('Content-Type', 'application/json')
response = urllib2.urlopen(req)
data=response.read()
response.close()
return data
def downloadApiFile(path, param):
"""
Download file with relative url from a random api server.
Retry with other api servers if failed.
Returns:
a string result
"""
servers = get_radiobrowser_base_urls()
i = 0
for server_base in servers:
xbmc.log('Random server: ' + server_base + ' Try: ' + str(i))
uri = server_base + path
try:
data = downloadFile(uri, param)
return data
except Exception as e:
xbmc.log("Unable to download from api url: " + uri, xbmc.LOGERROR)
pass
i += 1
return {}
def addPlayableLink(data):
dataDecoded = json.loads(data)
for station in dataDecoded:
addLink(station['stationuuid'], station['name'], station['url'], station['favicon'], station['bitrate'])
def readFile(filepath):
with open(filepath, 'r') as read_file:
return json.load(read_file)
def writeFile(filepath, data):
with open(filepath, 'w') as write_file:
return json.dump(data, write_file)
def addToMyStations(stationuuid, name, url, favicon, bitrate):
my_stations[stationuuid] = {'stationuuid': stationuuid, 'name': name, 'url': url, 'bitrate': bitrate, 'favicon': favicon}
writeFile(mystations_path, my_stations)
def delFromMyStations(stationuuid):
if stationuuid in my_stations:
del my_stations[stationuuid]
writeFile(mystations_path, my_stations)
xbmc.executebuiltin('Container.Refresh')
# create storage
if not xbmcvfs.exists(profile):
xbmcvfs.mkdir(profile)
if xbmcvfs.exists(mystations_path):
my_stations = readFile(mystations_path)
else:
writeFile(mystations_path, my_stations)
mode = args.get('mode', None)
if mode is None:
localUrl = build_url({'mode': 'stations', 'url': '/json/stations/topclick/100'})
li = xbmcgui.ListItem(LANGUAGE(32000), iconImage='DefaultFolder.png')
xbmcplugin.addDirectoryItem(handle=addon_handle, url=localUrl, listitem=li, isFolder=True)
localUrl = build_url({'mode': 'stations', 'url': '/json/stations/topvote/100'})
li = xbmcgui.ListItem(LANGUAGE(32001), iconImage='DefaultFolder.png')
xbmcplugin.addDirectoryItem(handle=addon_handle, url=localUrl, listitem=li, isFolder=True)
localUrl = build_url({'mode': 'stations', 'url': '/json/stations/lastchange/100'})
li = xbmcgui.ListItem(LANGUAGE(32002), iconImage='DefaultFolder.png')
xbmcplugin.addDirectoryItem(handle=addon_handle, url=localUrl, listitem=li, isFolder=True)
localUrl = build_url({'mode': 'stations', 'url': '/json/stations/lastclick/100'})
li = xbmcgui.ListItem(LANGUAGE(32003), iconImage='DefaultFolder.png')
xbmcplugin.addDirectoryItem(handle=addon_handle, url=localUrl, listitem=li, isFolder=True)
localUrl = build_url({'mode': 'tags'})
li = xbmcgui.ListItem(LANGUAGE(32004), iconImage='DefaultFolder.png')
xbmcplugin.addDirectoryItem(handle=addon_handle, url=localUrl, listitem=li, isFolder=True)
localUrl = build_url({'mode': 'countries'})
li = xbmcgui.ListItem(LANGUAGE(32005), iconImage='DefaultFolder.png')
xbmcplugin.addDirectoryItem(handle=addon_handle, url=localUrl, listitem=li, isFolder=True)
localUrl = build_url({'mode': 'search'})
li = xbmcgui.ListItem(LANGUAGE(32007), iconImage='DefaultFolder.png')
xbmcplugin.addDirectoryItem(handle=addon_handle, url=localUrl, listitem=li, isFolder=True)
localUrl = build_url({'mode': 'mystations'})
li = xbmcgui.ListItem(LANGUAGE(32008), iconImage='DefaultFolder.png')
xbmcplugin.addDirectoryItem(handle=addon_handle, url=localUrl, listitem=li, isFolder=True)
xbmcplugin.endOfDirectory(addon_handle)
elif mode[0] == 'tags':
data = downloadApiFile('/json/tags', None)
dataDecoded = json.loads(data)
for tag in dataDecoded:
tagName = tag['name']
if int(tag['stationcount']) > 1:
try:
localUrl = build_url({'mode': 'stations', 'key': 'tag', 'value' : base64.b32encode(tagName.encode('utf-8'))})
li = xbmcgui.ListItem(tagName, iconImage='DefaultFolder.png')
xbmcplugin.addDirectoryItem(handle=addon_handle, url=localUrl, listitem=li, isFolder=True)
except Exception as e:
xbmc.err(e)
pass
xbmcplugin.endOfDirectory(addon_handle)
elif mode[0] == 'countries':
data = downloadApiFile('/json/countries', None)
dataDecoded = json.loads(data)
for tag in dataDecoded:
countryName = tag['name']
if int(tag['stationcount']) > 1:
try:
localUrl = build_url({'mode': 'states', 'country': base64.b32encode(countryName.encode('utf-8'))})
li = xbmcgui.ListItem(countryName, iconImage='DefaultFolder.png')
xbmcplugin.addDirectoryItem(handle=addon_handle, url=localUrl, listitem=li, isFolder=True)
except Exception as e:
xbmc.log("Stationcount is not of type int", xbmc.LOGERROR)
pass
xbmcplugin.endOfDirectory(addon_handle)
elif mode[0] == 'states':
country = args['country'][0]
country = base64.b32decode(country)
country = country.decode('utf-8')
data = downloadApiFile('/json/states/'+urllib.quote(country)+'/', None)
dataDecoded = json.loads(data)
localUrl = build_url({'mode': 'stations', 'key': 'country', 'value': base64.b32encode(country.encode('utf-8'))})
li = xbmcgui.ListItem(LANGUAGE(32006), iconImage='DefaultFolder.png')
xbmcplugin.addDirectoryItem(handle=addon_handle, url=localUrl, listitem=li, isFolder=True)
for tag in dataDecoded:
stateName = tag['name']
if int(tag['stationcount']) > 1:
try:
localUrl = build_url({'mode': 'stations', 'key': 'state','value':base64.b32encode(stateName.encode('utf-8'))})
li = xbmcgui.ListItem(stateName, iconImage='DefaultFolder.png')
xbmcplugin.addDirectoryItem(handle=addon_handle, url=localUrl, listitem=li, isFolder=True)
except Exception as e:
xbmc.log("Stationcount is not of type int", xbmc.LOGERROR)
pass
xbmcplugin.endOfDirectory(addon_handle)
elif mode[0] == 'stations':
url = '/json/stations/search'
param = None
if 'url' in args:
url = args['url'][0]
else:
key = args['key'][0]
value = base64.b32decode(args['value'][0])
value = value.decode('utf-8')
param = dict({key:value})
param['order'] = 'clickcount'
param['reverse'] = True
data = downloadApiFile(url, param)
addPlayableLink(data)
xbmcplugin.endOfDirectory(addon_handle)
elif mode[0] == 'play':
stationuuid = args['stationuuid'][0]
data = downloadApiFile('/json/url/' + str(stationuuid),None)
dataDecoded = json.loads(data)
uri = dataDecoded['url']
xbmcplugin.setResolvedUrl(addon_handle, True, xbmcgui.ListItem(path=uri))
elif mode[0] == 'search':
dialog = xbmcgui.Dialog()
d = dialog.input(LANGUAGE(32011), type=xbmcgui.INPUT_ALPHANUM)
url = '/json/stations/byname/'+d
data = downloadApiFile(url, None)
addPlayableLink(data)
xbmcplugin.endOfDirectory(addon_handle)
elif mode[0] == 'mystations':
for station in my_stations.values():
addLink(station['stationuuid'], station['name'], station['url'], station['favicon'], station['bitrate'])
xbmcplugin.endOfDirectory(addon_handle)
elif mode[0] == 'addstation':
favicon = args['favicon'][0] if 'favicon' in args else ''
addToMyStations(args['stationuuid'][0], args['name'][0], args['url'][0], favicon, args['bitrate'][0])
elif mode[0] == 'delstation':
delFromMyStations(args['stationuuid'][0])