[95f8c6]: plugin.audio.di.fm / default.py Maximize Restore History

Download this file

default.py    425 lines (342 with data), 17.3 kB

  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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#!/usr/bin/env python
# -*- coding: utf-8 -*-
########################################
# Digitally Imported XBMC plugin
# by Tim C. 'Bitcrusher' Steinmetz
# http://qualisoft.dk
# Github: https://github.com/Bitcrusher/Digitally-Imported-XBMC-plugin
# Git Read-only: git://github.com/Bitcrusher/Digitally-Imported-XBMC-plugin.git
#
# This Program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This Program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with XBMC; see the file COPYING. If not, write to
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
# http://www.gnu.org/copyleft/gpl.html
#
import os
import pickle
import sys
import re
import string
import urllib
import xbmc
import xbmcgui
import xbmcplugin
import xbmcaddon
import time
from xml.dom import minidom
from httpcomm import HTTPComm
from ConfigParser import SafeConfigParser
import json
from random import randrange
import Queue
import threading
# Import JSON - compatible with Python<v2.6
try:
import json
except ImportError:
import simplejson as json
# Config parser
pluginConfig = SafeConfigParser()
pluginConfig.read(os.path.join(os.path.dirname(__file__), "config.ini"))
# Various constants used throughout the script
HANDLE = int(sys.argv[1])
ADDON = xbmcaddon.Addon(id=pluginConfig.get('plugin', 'id'))
# Plugin constants
__plugin__ = ADDON.getAddonInfo('name')
__author__ = "Tim C. Steinmetz"
__url__ = "http://qualisoft.dk/"
__platform__ = "xbmc media center, [LINUX, OS X, WIN32]"
__date__ = pluginConfig.get('plugin', 'date')
__version__ = ADDON.getAddonInfo('version')
"""
Thread class used for scraping individual playlists for each channel
"""
class scraperThread(threading.Thread):
def __init__(self, musicObj, channel, channelMeta, channelCount):
threading.Thread.__init__(self)
self.musicObj = musicObj
self.channel = channel
self.channelMeta = channelMeta
self.channelCount = channelCount
def run(self):
self.musicObj.addChannel(self.channel, self.channelMeta, self.channelCount)
class musicAddonXbmc:
# settings used for multithreading
threadMax = 8
workQueue = Queue.Queue()
# Resolves path to where plugin settings and cache will be stored
addonProfilePath = xbmc.translatePath(ADDON.getAddonInfo('profile')).decode('utf-8')
# Init CURL thingy
curler = HTTPComm()
# regex used to find all streams in a .pls
re_playlistStreams = re.compile("File\d=([^\n]+)\s*Title\d=([^\n]+)", re.M | re.I)
#dictBitrate = {1: 40, 2: 64, 3: 128, 4: 256}
dictBitrate = [40, 64, 128, 256]
# list of channels used when caching and retrieving from cache
channelsList = []
# unique listenkey if a premium member
listenKey = ''
# stores how many new channels/channelart was found
newChannels = 0
def __init__(self):
# If stats is allowed and its been at least 24 hours since last checkin
#if (ADDON.getSetting('allowstats') == "true") and (
# self.checkFileTime(self._checkinFile, self._addonProfilePath, 86400) == True):
# open(self._checkinFile, "w")
#
# account = 'public'
# if ADDON.getSetting('username') != "":
# account = 'premium'
#
# xbmc.log('Submitting stats', xbmc.LOGNOTICE)
# self._httpComm.get('http://stats.qualisoft.dk/?plugin=' + ADDON.getAddonInfo(
# 'id') + '&version=' + __version__ + '&account=' + account + '&key=' + parser.get('plugin',
# 'checkinkey'))
#
xbmc.log("[PLUGIN] %s v%s (%s)" % ( __plugin__, __version__, __date__ ), xbmc.LOGNOTICE)
"""
Let's get some tunes!
"""
def run(self):
threads = []
# check if cache has expired
if not ADDON.getSetting("forceupdate") == "true"\
and not int(ADDON.getSetting("cacheexpire_days")) == 0\
and self.checkFileTime(pluginConfig.get('cache', 'cacheChannels'),
ADDON.getSetting("cacheexpire_days")):
ADDON.setSetting(id="forceupdate", value="true")
if ADDON.getSetting("forceupdate") == "true":
channels = []
html = ""
# if username is set, try to login and go for premium channels
if ADDON.getSetting('username') != "":
loginData = urllib.urlencode({'member_session[username]': ADDON.getSetting('username'),
'member_session[password]': ADDON.getSetting('password')})
# post login info and get frontpage html
html = self.curler.request(pluginConfig.get('urls', 'login'), 'post', loginData)
# if we could not reach di.fm at all
if not bool(html):
xbmc.log('di.fm could not be reached', xbmc.LOGWARNING)
xbmcgui.Dialog().ok(ADDON.getLocalizedString(30100),
ADDON.getLocalizedString(30101),
ADDON.getLocalizedString(30102),
ADDON.getLocalizedString(30103))
xbmcplugin.endOfDirectory(HANDLE, succeeded=True)
return True
channels = json.loads(self.curler.request(pluginConfig.get('streams', 'premium%sk' % self.dictBitrate[int(ADDON.getSetting('bitrate'))]), 'get'))
premiumConfig = self.getPremiumConfig(html)
# if premiumConfig['listenKey'] is blank, we did not log in correctly
if premiumConfig['listenKey'] == '':
xbmc.log('Login did not succeed', xbmc.LOGWARNING)
xbmcgui.Dialog().ok(ADDON.getLocalizedString(30170),
ADDON.getLocalizedString(30171),
ADDON.getLocalizedString(30172))
xbmcplugin.endOfDirectory(HANDLE, succeeded=True)
return True
# if we should get the favorites or all channels
if ADDON.getSetting("usefavorites") == 'true':
channels = self.getFavoriteChannels(html, channels)
if len(channels) == 0:
xbmcgui.Dialog().ok(ADDON.getLocalizedString(30180),
ADDON.getLocalizedString(30181),
ADDON.getLocalizedString(30182))
xbmcplugin.endOfDirectory(HANDLE, succeeded=True)
return True
# add listenkey to playlist urls
for channel in channels:
channel['playlist'] = '%s?%s' % (channel['playlist'], premiumConfig['listenKey'])
# go for free/public channels
else:
html = self.curler.request(pluginConfig.get('urls', 'frontpage'), 'get')
# if we could not reach di.fm at all
if not html:
xbmc.log(u'di.fm could not be reached', xbmc.LOGWARNING)
xbmcgui.Dialog().ok(ADDON.getLocalizedString(30100),
ADDON.getLocalizedString(30101),
ADDON.getLocalizedString(30102),
ADDON.getLocalizedString(30103))
xbmcplugin.endOfDirectory(HANDLE, succeeded=True)
return True
channels = json.loads(self.curler.request(pluginConfig.get('streams', 'public'), 'get'))
re_channelData = re.compile("NS\('AudioAddict'\).Channels\s*=\s*([^;]+);", re.M | re.I)
channelMeta = json.loads(re_channelData.findall(html)[0])
# put each playlist in a worker queue for threading
for channel in channels:
self.workQueue.put(channel)
# starts 8 threads to download streams for each channel
while not self.workQueue.empty():
xbmc.log('Worker queue size is %s' % (str(self.workQueue.qsize())), xbmc.LOGNOTICE)
if threading.activeCount() < self.threadMax and not self.workQueue.empty():
threads.append(scraperThread(self, self.workQueue.get(), channelMeta, len(channels)))
threads[-1].start()
else:
time.sleep(0.1)
# wait for all threads to finish before continuing
for t in threads:
t.join()
# Saves channels to cache and reset the "force update" flag
if len(channels) > 0:
pickle.dump(self.channelsList, open(os.path.join(self.addonProfilePath, pluginConfig.get('cache', 'cacheChannels')), "w"), protocol=0)
ADDON.setSetting(id="forceupdate", value="false")
# else load channels from cache file
else:
self.channelsList = pickle.load(open(os.path.join(self.addonProfilePath, pluginConfig.get('cache', 'cacheChannels')), "r"))
for channel in self.channelsList:
self.addItem(channel['name'],
channel['streamUrl'],
channel['description'],
channel['bitrate'],
channel['asset'],
channel['isNew'],
len(self.channelsList))
# Should channels be sorted A-Z?
if ADDON.getSetting('sortaz') == "true":
xbmcplugin.addSortMethod(HANDLE, sortMethod=xbmcplugin.SORT_METHOD_LABEL)
# tell XMBC there are no more items to list
xbmcplugin.endOfDirectory(HANDLE, succeeded=True)
if self.newChannels > 0:
xbmcgui.Dialog().ok(ADDON.getLocalizedString(30130),
ADDON.getLocalizedString(30131) + str(self.newChannels) + ADDON.getLocalizedString(30132),
ADDON.getLocalizedString(30133),
ADDON.getLocalizedString(30134))
return True
"""
Parses a playlist (if needed) and calls the function that adds it to the GUI
"""
def addChannel(self, channel, channelMeta, channelCount):
# set to true if new channelart is downloaded
isNew = 0
if not os.path.exists(self.addonProfilePath + str(channel['id']) + '.png'):
isNew = 1
self.newChannels += 1
self.getChannelAsset(str(channel['id']), channelMeta[str(channel['id'])]['asset_url'])
if ADDON.getSetting('randomstream') == "true":
playlist = self.curler.request(channel['playlist'] + self.listenKey, 'get')
playlistStreams = self.re_playlistStreams.findall(playlist)
# gets a random stream from the channels playlist
streamUrl = playlistStreams[randrange(len(playlistStreams))][0]
else:
streamUrl = channel['playlist'] + self.listenKey
bitrate = 40
if ADDON.getSetting('username') != "":
bitrate = self.dictBitrate[int(ADDON.getSetting('bitrate'))]
# stores channel in list to be cached later
co = {'name': channel['name'],
'streamUrl': streamUrl,
'description': channel['description'],
'bitrate': bitrate,
'asset': self.addonProfilePath + str(channel['id']) + '.png',
'isNew': isNew}
self.channelsList.append(dict(co))
self.addItem(channel['name'],
streamUrl,
channel['description'],
bitrate,
self.addonProfilePath + str(channel['id']) + '.png',
isNew,
channelCount)
""" Returns a bool
Adds item to the XBMC GUI
"""
def addItem(self, channelTitle, streamUrl, streamDescription, bitrate, icon, isNewChannel, totalItems):
# tart it up a bit if it's a new channel
if isNewChannel:
li = xbmcgui.ListItem(label="[COLOR FF007EFF]" + channelTitle + "[/COLOR]", thumbnailImage=icon)
xbmc.log(u"New channel found: %s" % channelTitle.encode('ascii', 'xmlcharrefreplace'), xbmc.LOGERROR)
else:
li = xbmcgui.ListItem(label=channelTitle, thumbnailImage=icon)
# 256kb/sec is MP3
if bitrate == 256 and ADDON.getSetting('username') != "":
li.setProperty("mimetype", 'audio/mpeg')
else:
li.setProperty("mimetype", 'audio/aac')
li.setInfo(type="Music", infoLabels={"Genre": channelTitle,
"Comment": streamDescription,
"Size": bitrate * 1024})
li.setProperty("IsPlayable", "true")
xbmcplugin.addDirectoryItem(handle=HANDLE, url=streamUrl, listitem=li, isFolder=False, totalItems=totalItems)
return True
""" Returns a bool
Will check if channel asset/art is present in cache - if not, try to download
"""
def getChannelAsset(self, channelId, assetUrl):
try:
data = self.curler.request(assetUrl, 'get')
filepath = self.addonProfilePath + channelId + '.png'
open(filepath, 'wb').write(data)
xbmc.log('Found new channel art for with ID %s' % str(channelId), xbmc.LOGINFO)
except Exception:
sys.exc_clear() # Clears all exceptions so the script will continue to run
xbmcgui.Dialog().ok(ADDON.getLocalizedString(30160), ADDON.getLocalizedString(30161),
ADDON.getLocalizedString(30162) + channelId)
xbmc.log(ADDON.getLocalizedString(30160) + " " + ADDON.getLocalizedString(
30161) + str(channelId) + " " + ADDON.getLocalizedString(30162) + str(channelId), xbmc.LOGERROR)
return False
return True
""" Return a list containing a dictonary
Filters out all channels that are not in the favorites list
"""
def getFavoriteChannels(self, html, allChannels):
channels = []
re_favoriteData = re.compile("NS\('AudioAddict.API.Config.member'\).Favorites\s*=\s*([^;]+);", re.M | re.I)
m = re_favoriteData.findall(html)
# if favorites list is empty, return empty list
if m[0] == '[]':
return channels
else:
favorites = json.loads(re_favoriteData.findall(html)[0])
# sort favorites after user selected positions
favorites = sorted(favorites, key=lambda k: k['position'])
for fav in favorites:
for channel in allChannels:
if fav['channel_id'] == channel['id']:
channels.append(dict(channel))
return channels
""" Return a list containing a dictonary or false
Returns the logged in users premium config
"""
def getPremiumConfig(self, html):
try:
if ADDON.getSetting("forceupdate") == "true":
re_config = re.compile("NS\('AudioAddict.API'\).Config\s*=\s*([^;]+);", re.M | re.I)
pickle.dump(re_config.findall(html)[0], open(os.path.join(self.addonProfilePath, pluginConfig.get('cache', 'cachePremiumConfig')), "w"), protocol=0)
premiumConfig = json.loads(re_config.findall(html)[0])
else:
premiumConfig = json.loads(pickle.load(open(os.path.join(self.addonProfilePath, pluginConfig.get('cache', 'cachePremiumConfig')), "r")))
return premiumConfig
except Exception:
sys.exc_clear() # Clears all exceptions so the script will continue to run
return False
""" Return a bool
Checks if a file is older than x days
"""
def checkFileTime(self, filename, days):
if not os.path.exists(self.addonProfilePath):
os.makedirs(self.addonProfilePath)
return True
daysInSecs = int(days)*60*60*24
file = os.path.join(self.addonProfilePath, filename)
# If file exists, check timestamp
if os.path.exists(file):
if os.path.getmtime(file) > (time.time() - daysInSecs):
xbmc.log(u'It has not been %s days since %s was last updated' % (days, file), xbmc.LOGNOTICE)
return False
else:
xbmc.log(u'The cache file %s + has expired' % file, xbmc.LOGNOTICE)
return True
# If file does not exist, return true so the file will be created by scraping the page
else:
xbmc.log(u'The cache file %s does not exist' % file, xbmc.LOGNOTICE)
return True
MusicAddonInstance = musicAddonXbmc()
MusicAddonInstance.run()