-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
216 lines (190 loc) · 7.99 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
# -*- coding: utf-8 -*-
#
# Copyright (c) 2014 Will Adams (izilly)
# Distributed under the terms of the Modified BSD License.
# The full license is in the file LICENSE.txt, distributed with this software.
import sys
from urlparse import parse_qsl
import xbmcgui
import xbmcplugin
import xbmcaddon
from xml.etree.ElementTree import ElementTree as et
import urllib2
#from pudb.remote import set_trace
#set_trace(term_size=(140, 40))
# Get the plugin url in plugin:// notation.
_url = sys.argv[0]
# Get the plugin handle as an integer number.
_handle = int(sys.argv[1])
FEEDS = {
'hardcore_history': {
'url': 'http://feeds.feedburner.com/dancarlin/history?format=xml',
#'icon':'http://www.dancarlin.com/wp-content/uploads/2014/08/hh-cover.jpg',
#'icon': 'http://castig.org/wp-content/uploads/2015/07/mza_6421670988208781371.600x600-75.jpg',
'icon': 'http://www.dancarlin.com/wp-content/uploads/2014/07/HH-current-500x450.jpg',
'genre': 'History',
'name': xbmcaddon.Addon().getLocalizedString(30001).encode('utf-8')},
'common_sense': {
'url': 'http://feeds.feedburner.com/dancarlin/commonsense?format=xml',
#'icon': 'http://www.dancarlin.com/wp-content/uploads/2014/08/cs-cover.jpg',
#'icon': 'http://www.dancarlin.com/wp-content/uploads/2014/07/CS-current.jpg',
'icon': 'http://www.dancarlin.com/wp-content/uploads/2014/07/CS-current-500x450.jpg',
'genre': 'News',
'name': xbmcaddon.Addon().getLocalizedString(30002).encode('utf-8')}}
def get_categories():
"""
Get a list of categories (podcasts).
Returns:
list of categories
"""
return ['hardcore_history', 'common_sense']
def get_episodes(category):
"""
Get a list of episodes in a given category.
Returns:
list of episodes
"""
feed_url = FEEDS[category]['url']
xmlfile = urllib2.urlopen(feed_url)
tree = et(file=xmlfile)
root = tree.getroot()
channel = root[0]
items = channel.findall('item')
episodes = [get_episode_info(i, category) for i in items]
return episodes
def get_episode_info(episode, category):
"""
Get information on a single episode.
Returns:
dict of episode info
"""
ei = {}
ei['title'] = episode.find('title').text
ei['description'] = episode.find('description').text
ei['date'] = episode.find('pubDate').text
enclosure = episode.find('enclosure')
ei['length'] = enclosure.get('length')
ei['url'] = enclosure.get('url')
ei['thumb'] = FEEDS[category]['icon']
ei['genre'] = FEEDS[category]['genre']
return ei
def list_categories():
"""
Create the list of categories in the Kodi interface.
Returns:
None
"""
categories = get_categories()
listing = []
for category in categories:
# Create a list item with a text label and a thumbnail image.
list_item = xbmcgui.ListItem(label=FEEDS[category]['name'], thumbnailImage=FEEDS[category]['icon'])
# set fanart
my_addon = xbmcaddon.Addon('plugin.audio.dancarlin')
#list_item.setProperty('fanart_image', my_addon.getAddonInfo('fanart'))
list_item.setArt({'fanart': my_addon.getAddonInfo('fanart')})
# Set additional info for the list item.
list_item.setInfo('video', {'title': category, 'genre': 'news'})
list_item.setInfo('audio', {'title': category, 'genre': 'news'})
# Create a URL for the plugin recursive callback.
# e.g., plugin://plugin.audio.dancarlin/?action=listing&category=common_sense
url = '{0}?action=listing&category={1}'.format(_url, category)
# is_folder = True means that this item opens a sub-list of lower level items.
is_folder = True
# Add our item to the listing as a 3-element tuple.
listing.append((url, list_item, is_folder))
# Add our listing to Kodi.
xbmcplugin.addDirectoryItems(_handle, listing, len(listing))
# Add a sort method for the virtual folder items (alphabetically, ignore articles)
xbmcplugin.addSortMethod(_handle, xbmcplugin.SORT_METHOD_LABEL_IGNORE_THE)
# Finish creating a virtual folder.
xbmcplugin.endOfDirectory(_handle)
def list_episodes(category):
"""
Create the list of playable items in Kodi interface.
Args:
category: podcast/category (str)
Returns:
None
"""
episodes = get_episodes(category)
listing = []
for episode in episodes:
# Create a list item with a text label and a thumbnail image.
list_item = xbmcgui.ListItem(label=episode['title'], thumbnailImage=episode['thumb'])
# set fanart
my_addon = xbmcaddon.Addon('plugin.audio.dancarlin')
#list_item.setProperty('fanart_image', my_addon.getAddonInfo('fanart'))
list_item.setArt({'fanart': my_addon.getAddonInfo('fanart')})
# Set additional info for the list item.
#list_item.setInfo('video', {'title': episode['title'],
#'genre': episode['genre'],
#'plot': episode['description'],
#'plotoutline': episode['description'],
#})
list_item.setInfo('music', {'title': episode['title'],
'album': episode['title'],
'comment': episode['description'],
'artist': 'Dan Carlin',
'genre': episode['genre'],
})
# Set additional graphics (banner, poster, landscape etc.) for the list item.
list_item.setArt({'landscape': episode['thumb']})
# Set 'IsPlayable' property to 'true'.
# This is mandatory for playable items!
list_item.setProperty('IsPlayable', 'true')
# Create a URL for the plugin recursive callback.
# e.g., plugin://plugin.video.dancarlin/?action=play&episode=http://media.url
url = '{0}?action=play&episode={1}'.format(_url, episode['url'])
# Add the list item to a virtual Kodi folder.
# is_folder = False means that this item won't open any sub-list.
is_folder = False
# Add our item to the listing as a 3-element tuple.
listing.append((url, list_item, is_folder))
# Add our listing to Kodi.
xbmcplugin.addDirectoryItems(_handle, listing, len(listing))
# Add a sort method for the virtual folder items (alphabetically, ignore articles)
xbmcplugin.addSortMethod(_handle, xbmcplugin.SORT_METHOD_LABEL_IGNORE_THE)
# Finish creating a virtual folder.
xbmcplugin.endOfDirectory(_handle)
def play_episode(path):
"""
Play an episode with the given path.
Args:
path: Kodi url of episode (str)
Returns:
None
"""
# Create a playable item with a path to play.
play_item = xbmcgui.ListItem(path=path)
# Pass the item to the Kodi player.
xbmcplugin.setResolvedUrl(_handle, True, listitem=play_item)
def router(paramstring):
"""
Call the appropriate function for the given paramstring.
Args:
paramstring: query string passed to the add-on
Returns:
None
"""
# Parse a URL-encoded paramstring to the dictionary of
# {<parameter>: <value>} elements
params = dict(parse_qsl(paramstring))
# Check the parameters passed to the plugin
if params:
if params.get('content_type') and len(params) == 1:
list_categories()
elif params['action'] == 'listing':
# Display the list of episodes in a given category.
list_episodes(params['category'])
elif params['action'] == 'play':
# Play episode from a provided URL.
play_episode(params['episode'])
else:
# If the plugin is called from Kodi UI without any parameters,
# display the list of categories
list_categories()
if __name__ == '__main__':
# Call the router function and pass the plugin call parameters to it.
# We use string slicing to trim the leading '?' from the plugin call paramstring
router(sys.argv[2][1:])