[9634d9]: plugin.audio.sverigesradio / default.py  Maximize  Restore  History

Download this file

191 lines (171 with data), 8.1 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
# coding: latin-1
import urllib2
import xbmcgui
import xbmcplugin
import xbmcaddon
from xml.dom.minidom import parse, parseString
CHANNEL_URL = "http://api.sr.se/api/channels/channels.aspx"
PROGRAM_LIST_URL = "http://api.sr.se/api/program/programfeed.aspx"
PROGRAM_DETAIL_URL = "http://api.sr.se/api/program/broadcastfeed.aspx?unitid="
BASE_URL = "http://sr.se"
__settings__ = xbmcaddon.Addon(id='plugin.audio.sverigesradio')
QUALITIES = ["low", "normal", "high"]
PROTOCOLS = ["http", "rtsp"]
SET_QUALITY = QUALITIES[int(__settings__.getSetting("quality"))]
SET_PROTOCOL = PROTOCOLS[int(__settings__.getSetting("protocol"))]
def list_channels():
doc, state = load_xml(CHANNEL_URL)
if doc and not state:
for channel in doc.getElementsByTagName("channel"):
title = channel.getAttribute("name").encode('utf_8')
originaltitle = title
logo = get_node_value(channel, "logo")
description = get_node_value(channel, "tagline")
if description:
description = description.encode('utf_8')
title = title + " - " + description
else:
description = ''
streamingurl = channel.getElementsByTagName("streamingurl")
for url in streamingurl[0].getElementsByTagName("url"):
type = url.getAttribute("type")
protocol = url.getAttribute("protocol")
quality = url.getAttribute("quality")
playlist = url.getAttribute("playlist")
stream = url.childNodes[0].data
if type == "m4a" and quality == SET_QUALITY:
if logo:
add_posts(title, stream, description, logo, isLive = 'true', album = originaltitle, artist = 'Sveriges Radio')
else:
add_posts(title, stream, description, isLive = 'true', album = originaltitle, artist = 'Sveriges Radio')
else:
if state == "site":
xbmc.executebuiltin('Notification("Sveriges Radio","Site down")')
else:
xbmc.executebuiltin('Notification("Sveriges Radio","Malformed result")')
xbmcplugin.endOfDirectory(HANDLE)
def list_programs(url):
doc, state = load_xml(PROGRAM_LIST_URL)
if doc and not state:
for program in doc.getElementsByTagName("item"):
title = get_node_value(program, "title").encode('utf_8')
description = get_node_value(program, "description")
if description:
description = description.encode('utf_8')
title = title + " - " + description
else:
description = ''
unitid = ""
unitid = get_node_value(program, "unitid").encode('utf_8')
add_posts(title, url + unitid + "/", isFolder=True)
else:
if state == "site":
xbmc.executebuiltin('Notification("Sveriges Radio","Site down")')
else:
xbmc.executebuiltin('Notification("Sveriges Radio","Malformed result")')
xbmcplugin.endOfDirectory(HANDLE)
def list_program(unitid):
doc, state = load_xml(PROGRAM_DETAIL_URL+unitid)
if doc and not state:
urlset = doc.getElementsByTagName("urlset")[0]
base = ""
for url in urlset.getElementsByTagName("url"):
type = url.getAttribute("type")
protocol = url.getAttribute("protocol")
quality = url.getAttribute("quality")
if type == "m4a" and protocol == SET_PROTOCOL and quality == SET_QUALITY:
base = url.childNodes[0].data
if base == "":
base = "http://sverigesradio.se/topsy/ljudfil/utan/statistik/[broadcastid].m4a"
xbmc.log("Couldn't find url template, go with known (" + base + ")")
for audio in doc.getElementsByTagName("item"):
for ondemand in audio.getElementsByTagName("ondemand"):
title = ondemand.getAttribute("mainbroadcasttitle").encode('utf_8')
originaltitle = title
description = ondemand.getAttribute("mainbroadcastdescription").encode('utf_8')
if description:
title = description
else:
description = ''
date = ondemand.getAttribute("mainbroadcastdate").encode('utf_8')
if date:
title = title + " - " + date
else:
date = ''
thumb = ondemand.getAttribute("image").encode('utf_8')
if thumb:
thumb = BASE_URL + thumb
else:
thumb = ''
for broadcast in ondemand.getElementsByTagName("broadcastfilename"):
broadcastid = broadcast.getAttribute("broadcastid").encode('utf_8')
broadcastname = broadcast.childNodes[0].data.encode('utf_8')
url = base.replace("[broadcastid]", broadcastid)
url = url.replace("[broadcastfilename]", broadcastname)
add_posts(title, url, description, thumb, artist='Sveriges Radio', album=originaltitle)
else:
if state == "site":
xbmc.executebuiltin('Notification("Sveriges Radio","Site down")')
else:
xbmc.executebuiltin('Notification("Sveriges Radio","Malformed result")')
xbmcplugin.endOfDirectory(HANDLE)
def add_posts(title, url, description='', thumb='', isPlayable='true', isLive='false', isFolder=False, artist='',\
album=''):
title = title.replace("\n", " ")
listitem=xbmcgui.ListItem(title, iconImage=thumb)
listitem.setInfo(type='music', infoLabels={'title': title, 'artist': artist, 'album': album})
listitem.setProperty('IsPlayable', isPlayable)
listitem.setProperty('IsLive', isLive)
listitem.setPath(url)
return xbmcplugin.addDirectoryItem(HANDLE, url=url, listitem=listitem, isFolder=isFolder)
def add_main_menu():
listitem=xbmcgui.ListItem("Kanaler")
listitem.setInfo(type='music', infoLabels={ 'Title': "Kanaler"})
listitem.setPath('channels')
u = sys.argv[0] + "channels/"
xbmcplugin.addDirectoryItem(HANDLE, url=u, listitem=listitem, isFolder=True)
listitem=xbmcgui.ListItem("Program A-��")
listitem.setInfo(type='music', infoLabels={ 'Title': "Program A-��"})
listitem.setPath('program')
u = sys.argv[0] + "programs/"
xbmcplugin.addDirectoryItem(HANDLE, url=u, listitem=listitem, isFolder=True)
return xbmcplugin.endOfDirectory(HANDLE)
def get_node_value(parent, name, ns=""):
if ns:
if parent.getElementsByTagNameNS(ns, name) and \
parent.getElementsByTagNameNS(ns, name)[0].childNodes:
return parent.getElementsByTagNameNS(ns, name)[0].childNodes[0].data
else:
if parent.getElementsByTagName(name) and \
parent.getElementsByTagName(name)[0].childNodes:
return parent.getElementsByTagName(name)[0].childNodes[0].data
return None
def load_xml(url):
try:
req = urllib2.Request(url)
response = urllib2.urlopen(req)
except:
xbmc.log("plugin.audio.sverigesradio: unable to load url: " + url)
return None, "site"
xml = response.read()
response.close()
try:
out = parseString(xml)
except:
xbmc.log("plugin.audio.sverigesradio: malformed xml from url: " + url)
return None, "xml"
return out, None
if (__name__ == "__main__" ):
MODE=sys.argv[0]
HANDLE=int(sys.argv[1])
modes = MODE.split('/')
activemode = modes[len(modes) - 2]
parentmode = modes[len(modes) - 3]
if activemode == "programs" :
list_programs(MODE)
elif activemode == "channels" :
list_channels()
elif parentmode == "programs" :
list_program(activemode)
else :
add_main_menu()