-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaddon.py
More file actions
361 lines (320 loc) · 12 KB
/
addon.py
File metadata and controls
361 lines (320 loc) · 12 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
# -*- coding: utf-8 -*-
import os
import requests
import routing
import sys
import xbmc
import xbmcaddon
import xbmcgui
import xbmcplugin
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'resources',
'lib', 'python-pocketcasts'))
import pocketcasts
_plugin = routing.Plugin()
_addon = xbmcaddon.Addon()
_api = pocketcasts.Api(_addon.getSetting('pocketcasts_email'),
_addon.getSetting('pocketcasts_password'))
_default_fanart = os.path.join(os.path.dirname(__file__), 'fanart.jpg')
def handleException(e):
if type(e) == requests.exceptions.HTTPError and \
e.response.status_code == requests.codes.unauthorized:
xbmcgui.Dialog().ok("Error", "Could not authenticate with "
"Pocket Casts.", "Check login credentials!")
xbmc.log(_addon.getAddonInfo('id'))
_addon.openSettings()
else:
xbmcgui.Dialog().notification('Error occurred', str(e),
xbmcgui.NOTIFICATION_ERROR)
def podcasts2items(podcasts, subscribed_podcast_uuids=None):
if not subscribed_podcast_uuids:
try:
subscribed_podcasts = _api.my_podcasts()
except requests.exceptions.RequestException as e:
handleException(e)
return
subscribed_podcast_uuids = \
[podcast.uuid for podcast in subscribed_podcasts]
list_items = []
for podcast in podcasts:
menu_items = []
podcast_subscribed = (podcast.uuid in subscribed_podcast_uuids)
list_item = xbmcgui.ListItem(podcast.title)
if podcast_subscribed:
menu_items.append(('Unsubscribe', 'XBMC.RunPlugin(' +
_plugin.url_for(unsubscribe_podcast,
podcast_uuid=podcast.uuid
) + ')'))
else:
menu_items.append(('Subscribe', 'XBMC.RunPlugin(' +
_plugin.url_for(subscribe_podcast,
podcast_uuid=podcast.uuid,
) + ')'))
list_item.addContextMenuItems(menu_items)
list_item.setArt({
'icon': podcast.thumbnail_url,
'thumb': podcast.thumbnail_url,
'fanart': _default_fanart
})
list_item.setInfo('music', {
'album': podcast.title,
'artist': podcast.author,
'comment': podcast.description,
'genre': 'Podcast'
})
url = _plugin.url_for(show_episodes, uuid=podcast.uuid)
list_items.append((url, list_item, True))
return list_items
def episodes2items(episodes, subscribed_podcast_uuids=None):
if not subscribed_podcast_uuids:
try:
subscribed_podcasts = _api.my_podcasts()
except requests.exceptions.RequestException as e:
handleException(e)
return
subscribed_podcast_uuids = \
[podcast.uuid for podcast in subscribed_podcasts]
list_items = []
for i, episode in enumerate(episodes):
podcast = episode.podcast
label = episode.title
italics = False
yellow = False
menu_items = []
podcast = episode.podcast
podcast_subscribed = (episode.podcast.uuid in subscribed_podcast_uuids)
if episode.starred and podcast_subscribed:
label = '[S]' + label
yellow = True
menu_items.append(('Unstar', 'XBMC.RunPlugin(' +
_plugin.url_for(unstar_episode,
podcast_uuid=podcast.uuid,
episode_uuid=episode.uuid,
) + ')'))
elif podcast_subscribed:
menu_items.append(('Star', 'XBMC.RunPlugin(' +
_plugin.url_for(star_episode,
podcast_uuid=podcast.uuid,
episode_uuid=episode.uuid,
) + ')'))
if episode.played_up_to > 0 and \
episode.playing_status == \
pocketcasts.Episode.PlayingStatus.Unplayed:
m, s = divmod(episode.played_up_to, 60)
h, m = divmod(m, 60)
elapsed = '{0:01d}:{1:02d}:{2:02d}'.format(h, m, s)
label = '[' + elapsed + '] ' + label
italics = True
if yellow:
label = '[COLOR yellow]' + label + '[/COLOR]'
if italics:
label = '[I]' + label + '[/I]'
list_item = xbmcgui.ListItem(label)
list_item.setArt({
'icon': podcast.thumbnail_url,
'thumb': podcast.thumbnail_url,
'fanart': _default_fanart
})
list_item.setInfo('music', {
'date': episode.published_at.strftime('%d.%m.%Y'),
'year': episode.published_at.year,
'album': episode.podcast.title,
'artist': episode.podcast.author,
'title': episode.title,
'tracknumber': len(episodes) - i,
'comment': episode.notes,
'duration': episode.duration,
'genre': 'Podcast'
})
list_item.setProperty('IsPlayable', 'true')
list_item.addContextMenuItems(menu_items)
url = episode.url
list_items.append((url, list_item, False))
return list_items
@_plugin.route('/star_episode/<podcast_uuid>/<episode_uuid>')
def star_episode(podcast_uuid, episode_uuid):
try:
_api.mark_as_starred(podcast_uuid, episode_uuid, True)
except requests.exceptions.RequestException as e:
handleException(e)
return
xbmc.executebuiltin('Container.Refresh')
@_plugin.route('/unstar_episode/<podcast_uuid>/<episode_uuid>')
def unstar_episode(podcast_uuid, episode_uuid):
try:
_api.mark_as_starred(podcast_uuid, episode_uuid, False)
except requests.exceptions.RequestException as e:
handleException(e)
return
xbmc.executebuiltin('Container.Refresh')
@_plugin.route('/subscribe_podcast/<podcast_uuid>')
def subscribe_podcast(podcast_uuid):
try:
_api.subscribe_podcast(podcast_uuid)
except requests.exceptions.RequestException as e:
handleException(e)
return
xbmc.executebuiltin('Container.Refresh')
@_plugin.route('/unsubscribe_podcast/<podcast_uuid>')
def unsubscribe_podcast(podcast_uuid):
try:
_api.unsubscribe_podcast(podcast_uuid)
except requests.exceptions.RequestException as e:
handleException(e)
return
xbmc.executebuiltin('Container.Refresh')
@_plugin.route('/')
def index():
items = [
{
'label': 'My Podcasts',
'url': _plugin.url_for(my_podcasts)
},
{
'label': 'New Releases',
'url': _plugin.url_for(new_episode_released)
},
{
'label': 'In Progress',
'url': _plugin.url_for(episodes_in_progress)
},
{
'label': 'Starred',
'url': _plugin.url_for(starred_episodes)
},
{
'label': 'Featured',
'url': _plugin.url_for(featured_podcasts)
},
{
'label': 'Top Charts',
'url': _plugin.url_for(popular_podcasts)
},
{
'label': 'Trending Now',
'url': _plugin.url_for(trending_podcasts)
},
{
'label': 'Search Podcast / Add URL',
'url': _plugin.url_for(search_podcast)
}
]
xbmcplugin.setContent(_plugin.handle, 'files')
list_items = []
for item in items:
list_item = xbmcgui.ListItem(item['label'])
list_item.setArt({
'fanart': _default_fanart
})
list_items.append((item['url'], list_item, True))
if not xbmcplugin.addDirectoryItems(_plugin.handle, list_items):
raise
xbmcplugin.endOfDirectory(_plugin.handle)
@_plugin.route('/my_podcasts')
def my_podcasts():
try:
podcasts = _api.my_podcasts()
uuids = [podcast.uuid for podcast in podcasts]
items = podcasts2items(podcasts, subscribed_podcast_uuids=uuids)
except requests.exceptions.RequestException as e:
handleException(e)
return
xbmcplugin.setContent(_plugin.handle, 'albums')
if not xbmcplugin.addDirectoryItems(_plugin.handle, items):
raise
xbmcplugin.endOfDirectory(_plugin.handle, cacheToDisc=False)
@_plugin.route('/new_episode_released')
def new_episode_released():
try:
items = episodes2items(_api.new_episodes_released())
except requests.exceptions.RequestException as e:
handleException(e)
return
xbmcplugin.setContent(_plugin.handle, 'songs')
if not xbmcplugin.addDirectoryItems(_plugin.handle, items):
raise
xbmcplugin.endOfDirectory(_plugin.handle, cacheToDisc=False)
@_plugin.route('/episodes_in_progress')
def episodes_in_progress():
try:
items = episodes2items(_api.episodes_in_progress())
except requests.exceptions.RequestException as e:
handleException(e)
return
xbmcplugin.setContent(_plugin.handle, 'songs')
if not xbmcplugin.addDirectoryItems(_plugin.handle, items):
raise
xbmcplugin.endOfDirectory(_plugin.handle, cacheToDisc=False)
@_plugin.route('/starred_episodes')
def starred_episodes():
try:
items = episodes2items(_api.starred_episodes())
except requests.exceptions.RequestException as e:
handleException(e)
return
xbmcplugin.setContent(_plugin.handle, 'songs')
if not xbmcplugin.addDirectoryItems(_plugin.handle, items):
raise
xbmcplugin.endOfDirectory(_plugin.handle, cacheToDisc=False)
@_plugin.route('/featured_podcasts')
def featured_podcasts():
try:
items = podcasts2items(_api.featured_podcasts())
except requests.exceptions.RequestException as e:
handleException(e)
return
xbmcplugin.setContent(_plugin.handle, 'albums')
if not xbmcplugin.addDirectoryItems(_plugin.handle, items):
raise
xbmcplugin.endOfDirectory(_plugin.handle, cacheToDisc=False)
@_plugin.route('/popular_podcasts')
def popular_podcasts():
try:
items = podcasts2items(_api.popular_podcasts())
except requests.exceptions.RequestException as e:
handleException(e)
return
xbmcplugin.setContent(_plugin.handle, 'albums')
if not xbmcplugin.addDirectoryItems(_plugin.handle, items):
raise
xbmcplugin.endOfDirectory(_plugin.handle, cacheToDisc=False)
@_plugin.route('/trending_podcasts')
def trending_podcasts():
try:
items = podcasts2items(_api.trending_podcasts())
except requests.exceptions.RequestException as e:
handleException(e)
return
xbmcplugin.setContent(_plugin.handle, 'albums')
if not xbmcplugin.addDirectoryItems(_plugin.handle, items):
raise
xbmcplugin.endOfDirectory(_plugin.handle, cacheToDisc=False)
@_plugin.route('/search_podcast')
def search_podcast():
term = xbmcgui.Dialog().input("Enter search term")
if term:
_plugin.redirect('/search_podcast/'+term)
@_plugin.route('/search_podcast/<term>')
def search_podcast_results(term):
try:
items = podcasts2items(_api.search_podcasts(term))
except requests.exceptions.RequestException as e:
handleException(e)
return
xbmcplugin.setContent(_plugin.handle, 'albums')
if not xbmcplugin.addDirectoryItems(_plugin.handle, items):
raise
xbmcplugin.endOfDirectory(_plugin.handle, cacheToDisc=False)
@_plugin.route('/podcast/<uuid>')
def show_episodes(uuid):
try:
podcast = _api.podcast(uuid)
except requests.exceptions.RequestException as e:
handleException(e)
return
items = episodes2items(podcast.episodes)
xbmcplugin.setContent(_plugin.handle, 'songs')
if not xbmcplugin.addDirectoryItems(_plugin.handle, items):
raise
xbmcplugin.endOfDirectory(_plugin.handle, cacheToDisc=False)
if __name__ == '__main__':
_plugin.run()