forked from nils-werner/xbmc-somafm
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdefault.py
183 lines (142 loc) · 5.93 KB
/
default.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
import os
import shutil
import sys
import urllib2
import urlparse
import xml.etree.ElementTree as ET
import time
from lib.channel import Channel
import xbmcaddon
from xbmcgui import ListItem
import xbmcplugin
import xbmcgui
import xbmc
from xbmcplugin import SORT_METHOD_LISTENERS, SORT_METHOD_UNSORTED, SORT_METHOD_GENRE
import xbmcvfs
CHANNELS_FILE_NAME = "channels.xml"
__addon__ = "SomaFM"
__addonid__ = "plugin.audio.somafm"
__version__ = "1.0.1"
__ms_per_day__ = 24 * 60 * 60 * 1000
def log(msg):
xbmc.log(str(msg), level=xbmc.LOGDEBUG)
log(sys.argv)
rootURL = "https://somafm.com/"
tempdir = xbmc.translatePath("special://home/userdata/addon_data/%s" % __addonid__)
xbmcvfs.mkdirs(tempdir)
LOCAL_CHANNELS_FILE_PATH = os.path.join(tempdir, CHANNELS_FILE_NAME)
try:
plugin_url = sys.argv[0]
handle = int(sys.argv[1])
query = sys.argv[2]
except:
plugin_url = "plugin://" + __addonid__
handle = 0
query = ""
def fetch_remote_channel_data():
response = urllib2.urlopen(rootURL + CHANNELS_FILE_NAME)
channel_data = response.read()
response.close()
with open(LOCAL_CHANNELS_FILE_PATH, 'w') as local_channels_xml:
local_channels_xml.write(channel_data)
return channel_data
def fetch_local_channel_data():
with open(LOCAL_CHANNELS_FILE_PATH) as local_channels_file:
return local_channels_file.read()
def fetch_cached_channel_data():
if os.path.getmtime(LOCAL_CHANNELS_FILE_PATH) + cache_ttl_in_ms() > time.time():
print "Using cached channel.xml"
return fetch_local_channel_data()
# don't delete the cached file so we can still use it as a fallback
# if something goes wrong fetching the channel data from server
def fetch_channel_data(*strategies):
for strategy in strategies:
try:
result = strategy()
if result is not None:
return result
except:
pass
def build_directory():
channel_data = fetch_channel_data(fetch_cached_channel_data, fetch_remote_channel_data, fetch_local_channel_data)
xml_data = ET.fromstring(channel_data)
stations = xml_data.findall(".//channel")
for station in stations:
channel = Channel(handle, tempdir, station)
li = xbmcgui.ListItem(
channel.get_simple_element('title'),
channel.get_simple_element('description'),
channel.geticon(),
channel.getthumbnail(),
plugin_url + channel.getid())
li.setArt({"fanart" : xbmc.translatePath("special://home/addons/%s/fanart.jpg" % __addonid__)})
li.setProperty("IsPlayable", "true")
for element, info in [('listeners', 'listeners'),
('genre', 'genre'),
('dj', 'artist'),
('description', 'comment'),
('title', 'title')]:
value = channel.get_simple_element(element)
li.setInfo("Music", {info: value})
xbmcplugin.addDirectoryItem(
handle=handle,
url=plugin_url + channel.getid(),
listitem=li,
totalItems=len(stations))
xbmcplugin.addSortMethod(handle, SORT_METHOD_UNSORTED)
xbmcplugin.addSortMethod(handle, SORT_METHOD_LISTENERS)
xbmcplugin.addSortMethod(handle, SORT_METHOD_GENRE)
def format_priority():
setting = xbmcplugin.getSetting(handle, "priority_format")
result = [["mp3"], ["mp3", "aac"], ["aac", "mp3"], ["aac"], ][int(setting)]
print "Format setting is %s, using priority %s" % (setting, str(result))
return result
def quality_priority():
setting = xbmcplugin.getSetting(handle, "priority_quality")
result = [['slowpls', 'fastpls', 'highestpls', ], ['fastpls', 'slowpls', 'highestpls', ],
['fastpls', 'highestpls', 'slowpls', ], ['highestpls', 'fastpls', 'slowpls', ], ][int(setting)]
print "Quality setting is %s, using priority %s" % (setting, str(result))
return result
def cache_ttl_in_ms():
setting = xbmcplugin.getSetting(handle, "cache_ttl")
result = [0, __ms_per_day__, 7 * __ms_per_day__, 30 * __ms_per_day__][int(setting)]
print "Cache setting is %s, using ttl of %dms" % (setting, result)
return result
def play(item_to_play):
channel_data = fetch_channel_data(fetch_local_channel_data, fetch_remote_channel_data)
xml_data = ET.fromstring(channel_data)
try:
channel_data = xml_data.find(".//channel[@id='" + item_to_play + "']")
channel = Channel(handle, tempdir, channel_data, quality_priority(), format_priority())
except:
for element in xml_data.findall(".//channel"):
channel = Channel(handle, tempdir, element, quality_priority(), format_priority())
if channel.getid() == item_to_play:
break
list_item = ListItem(channel.get_simple_element('title'),
channel.get_simple_element('description'),
channel.geticon(),
channel.getthumbnail(),
channel.get_content_url())
list_item.setArt({"fanart" : xbmc.translatePath("special://home/addons/%s/fanart.jpg" % __addonid__)})
xbmcplugin.setResolvedUrl(handle, True, list_item)
def clearcache():
shutil.rmtree(tempdir, True)
addon = xbmcaddon.Addon(id=__addonid__)
heading = addon.getLocalizedString(32004)
message = addon.getLocalizedString(32005)
xbmcgui.Dialog().notification(
heading, message, xbmcgui.NOTIFICATION_INFO, 1000)
if handle == 0:
if query == "clearcache":
clearcache()
else:
print query
else:
path = urlparse.urlparse(plugin_url).path
item_to_play = os.path.basename(path)
if item_to_play:
play(item_to_play)
else:
build_directory()
xbmcplugin.endOfDirectory(handle)