-
Notifications
You must be signed in to change notification settings - Fork 0
/
addon.py
77 lines (61 loc) · 2.37 KB
/
addon.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
#-*- coding: utf-8 -*-
import os
import sys
import urllib
import urlparse
import xbmcaddon
import xbmcgui
import xbmc
import xbmcplugin
import requests
from bs4 import BeautifulSoup
def build_url(query):
base_url = sys.argv[0]
return base_url + '?' + urllib.urlencode(query)
def get_page(url):
return BeautifulSoup(requests.get(url).text, 'html.parser')
def parse_page(page):
songs = {}
index = 1
try:
for item in page.find_all('item'):
topics = ""
sub_item = BeautifulSoup(item.find("description").text, "html.parser")
for topic in sub_item.find_all('a'):
topics = "{}{}\n".format(topics, topic.string.encode('utf-8'))
if len(sub_item.img['src']) > 1:
album_cover = sub_item.img['src']
if len(sub_item.audio['src']) > 1:
songs.update({index: {'album_cover': album_cover, 'title': item.title.text.encode('utf-8'), 'url': sub_item.audio["src"], 'topics': topics}})
index += 1
except Exception as e:
xbmc.log("[plugin.radiot] {}".format(e), xbmc.LOGERROR)
return songs
def build_song_list(songs):
song_list = []
for song in songs:
li = xbmcgui.ListItem(label=songs[song]['title'], thumbnailImage=songs[song]['album_cover'])
li.setInfo('music', {'Artist': songs[song]['title'], 'Title': songs[song]['topics'] })
li.setProperty('fanart_image', songs[song]['album_cover'])
li.setProperty('IsPlayable', 'true')
url = build_url({'mode': 'stream', 'url': songs[song]['url'], 'title': songs[song]['title']})
song_list.append((url, li, False))
xbmcplugin.addDirectoryItems(addon_handle, song_list, len(song_list))
xbmcplugin.setContent(addon_handle, 'songs')
xbmcplugin.endOfDirectory(addon_handle)
def play_song(url):
play_item = xbmcgui.ListItem(path=url)
xbmcplugin.setResolvedUrl(addon_handle, True, listitem=play_item)
def main():
args = urlparse.parse_qs(sys.argv[2][1:])
mode = args.get('mode', None)
if mode is None:
page = get_page(rss_feeds_url)
content = parse_page(page)
build_song_list(content)
elif mode[0] == 'stream':
play_song(args['url'][0])
if __name__ == '__main__':
rss_feeds_url = 'http://feeds.rucast.net/radio-t'
addon_handle = int(sys.argv[1])
main()