forked from jbeluch/xbmc-newyorktimes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddon.py
More file actions
executable file
·118 lines (94 loc) · 3.64 KB
/
addon.py
File metadata and controls
executable file
·118 lines (94 loc) · 3.64 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
'''
New York Times XBMC Addon
~~~~~~~~~~~~~~~~~~~~~~~~~
Watch video from The New York Times.
http://video.on.nytimes.com/
:copyright: (c) 2012 by Jonathan Beluch
:modified on 2014, 2015, 2019 by idleloop
:license: GPLv3, see LICENSE.txt for more details.
'''
from resources.lib import api
import xbmcgui
# plugin settings
import xbmcaddon
settings = xbmcaddon.Addon(id='plugin.video.newyorktimes')
from xbmcswift2 import Plugin
plugin = Plugin()
NYT_URL_BASE = 'https://www.nytimes.com/'
LATEST_VIDEOS = 'Latest Videos'
# persistent storage to store ref_id of representative videos for each NYT topic
# https://kodi.wiki/view/Add-on:Common_plugin_cache
try:
import StorageServer
except:
import storageserverdummy as StorageServer
cache = StorageServer.StorageServer("plugin.video.newyorktimes", 24) # plugin name, Cache time in hours
def global_items():
return api.get_topics()
def global_items_ref_id_storage(description='', ref_id=''):
# global_items_ref_id = {'this nyt topic description': 'has this ref_id video representative'}
global_items_ref_id = cache.get( "global_items_ref_id" )
if global_items_ref_id != '':
global_items_ref_id = eval( global_items_ref_id )
else:
global_items_ref_id = {}
if description != '':
global_items_ref_id[description] = ref_id
cache.set( "global_items_ref_id", repr( global_items_ref_id ) )
return 1
else:
if repr(global_items_ref_id) == '{}':
cache.set( "global_items_ref_id", repr( {} ) )
return global_items_ref_id
@plugin.route('/')
def show_topics():
'''The main menu, shows available video topics
'''
items = [ {
'label': name.replace('&', "&"),
'path': plugin.url_for('show_topic', url=url),
} for name, url in global_items() ]
return items
@plugin.route('/topics/<url>')
@plugin.route('/topics/<url>/<page>', name='show_topic_nextpage')
def show_topic(url, page='0'):
'''For a given topic page, shows available sub-topics (if present) as well
as videos.
'''
page = int(page)
resolution_option = settings.getSetting("resolution")
# obtain description of this url topic, in order to correctly select videos from NYT API
try:
description = [y[0] for x,y in enumerate( global_items() ) if y[1] == url][0]
except:
description = '' # subcategory
global_items_ref_id = global_items_ref_id_storage()
global_items_ref_id[LATEST_VIDEOS] = '1194811622182'
dialog = xbmcgui.Dialog()
dialog.notification( 'Retrieving videos.',
'Please, wait ...',
xbmcgui.NOTIFICATION_INFO, 5000 )
( videos, ref_id ) = api.get_videos( url,
description,
global_items_ref_id[description] if description in global_items_ref_id else '',
resolution_option,
page )
# store ref_id representative of this url topic not to repeat unnecessary browsing
if description != '' and not description in global_items_ref_id:
global_items_ref_id_storage( description, ref_id )
if (page==0):
subtopics = [{
'label': label,
'path': plugin.url_for('show_topic', url=path),
} for label, path in api.get_sub_topics(url)]
videos=subtopics+videos
# paginated results:
if description != 'New York':
# ('New York' section does not have direct correspondent json classification, and so, Next can't be calculated)
videos.append({
'label': 'Next >>',
'path': plugin.url_for( 'show_topic_nextpage', url=url, page=str(page+1) )
})
return videos
if __name__ == '__main__':
plugin.run()