forked from marius-muja/plugin.video.canada.on.demand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
default.py
494 lines (393 loc) · 17.8 KB
/
default.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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
import os, sys
import shutil
import sha
import cgi
import xbmc, xbmcaddon, xbmcgui, xbmcplugin
import logging
logging.basicConfig(level=logging.WARNING)
import urllib,urllib2,urlparse
import time
from utils import urldecode
from channels import *
from channel import *
import socket
socket.setdefaulttimeout(50)
try:
from sqlite3 import dbapi2 as sqlite
except:
from pysqlite2 import dbapi2 as sqlite
__plugin__ = "Canada On Demand"
__author__ = 'Andre,Renaud {andrepleblanc,renaudtrudel}@gmail.com'
__url__ = 'http://xbmcaddons.com/addons/plugin.video.canada.on.demand/'
__date__ = '10-11-2013'
__version__ = '0.8.5'
__settings__ = xbmcaddon.Addon(id='plugin.video.canada.on.demand')
class OnDemandPlugin(object):
def connect_to_db(self):
path = xbmc.translatePath('special://profile/addon_data/plugin.video.canada.on.demand/')
if not os.path.exists(path):
os.makedirs(path)
self.db_conn = sqlite.connect(os.path.join(path, 'bookmarks.db'))
curs = self.db_conn.cursor()
curs.execute("""create table if not exists bookmark_folders (
id integer primary key,
name text,
parent_id integer,
path text
)""")
curs.execute("""create table if not exists bookmarks (
id integer primary key,
name text,
folder_id integer,
plugin_url text
)""")
try:
curs.execute("""insert into bookmark_folders (id, name, parent_id, path)
values (?,?,?,?)""", (1,'Bookmarks', 0, 'Bookmarks'))
except:
pass
def _urlopen(self, url, retry_limit=4):
retries = 0
while retries < retry_limit:
logging.debug("fetching %s" % (url,))
# Add referer for CTV to work properly
url_scheme, netloc, path, query, fragment = urlparse.urlsplit(url)
req = urllib2.Request(url)
req.add_header("Referer", "%s://%s/" % (url_scheme, netloc))
try:
return urllib2.urlopen(req)
except (urllib2.HTTPError, urllib2.URLError), e:
retries += 1
raise Exception("Failed to retrieve page: %s" %(url,))
def _urlretrieve(self, url, filename, retry_limit=4):
retries = 0
while retries < retry_limit:
logging.debug("fetching %s" % (url,))
try:
return urllib.urlretrieve(url, filename)
except (urllib.HTTPError, urllib.URLError), e:
retries += 1
raise Exception("Failed to retrieve page: %s" %(url,))
def fetch(self, url, max_age=None):
if max_age is None:
return self._urlopen(url)
tmpurl = url
scheme, tmpurl = tmpurl.split("://",1)
netloc, path = tmpurl.split("/",1)
fname = sha.new(path).hexdigest()
_dir = fname[:4]
cacheroot = self.get_cache_dir()
cachepath = os.path.join(cacheroot, netloc, _dir)
if not os.path.exists(cachepath):
os.makedirs(cachepath)
download = True
cfname = os.path.join(cachepath, fname)
if os.path.exists(cfname):
ctime = os.path.getctime(cfname)
if time.time() - ctime < max_age:
download = False
if download:
logging.debug("Fetching: %s" % (url,))
urllib.urlretrieve(url, cfname)
else:
logging.debug("Using Cached: %s" % (url,))
return open(cfname)
def get_url(self,urldata):
"""
Constructs a URL back into the plugin with the specified arguments.
"""
return "%s?%s" % (self.script_url, urllib.urlencode(urldata,1))
def action_channel_list(self):
"""
List all registered Channels
Channels are automatically registered simply by being imported
and being subclasses of BaseChannel.
"""
for channel_code, channel_class in sorted(ChannelMetaClass.registry.channels.iteritems()):
info = channel_class.get_channel_entry_info()
# Default to <short_name>.png if no icon is set.
if info['Thumb'] is None:
info['Thumb'] = info['channel'] + ".png"
try:
info['Thumb'] = self.get_resource_path('images','channels', info['Thumb'])
except ChannelException:
logging.warn("Couldn't Find Channel Icon for %s" % (channel_code,))
self.add_list_item(info)
self.end_list()
def get_dialog(self):
return xbmcgui.Dialog()
def set_stream_url(self, url, info=None):
"""
Resolve a Stream URL and return it to XBMC.
'info' is used to construct the 'now playing' information
via add_list_item.
"""
listitem = xbmcgui.ListItem(label='clip', path=url)
xbmcplugin.setResolvedUrl(self.handle, True, listitem)
def end_list(self, content='movies', sort_methods=None):
xbmcplugin.setContent(self.handle, content)
if sort_methods is None:
sort_methods = (xbmcplugin.SORT_METHOD_NONE,)
for sm in sort_methods:
xbmcplugin.addSortMethod(self.handle, sm)
xbmcplugin.endOfDirectory(self.handle, succeeded=True)
def get_cache_dir(self):
"""
return an acceptable cache directory.
"""
# I have no idea if this is right.
path = xbmc.translatePath('special://profile/addon_data/plugin.video.canada.on.demand/cache/')
if not os.path.exists(path):
os.makedirs(path)
return path
def get_setting(self, id):
"""
return a user-modifiable plugin setting.
"""
return __settings__.getSetting(id)
def add_list_item(self, info, is_folder=True, return_only=False,
context_menu_items=None, clear_context_menu=False, bookmark_parent=None, bookmark_id=None, bookmark_folder_id=None):
"""
Creates an XBMC ListItem from the data contained in the info dict.
if is_folder is True (The default) the item is a regular folder item
if is_folder is False, the item will be considered playable by xbmc
and is expected to return a call to set_stream_url to begin playback.
if return_only is True, the item item isn't added to the xbmc screen but
is returned instead.
Note: This function does some renaming of specific keys in the info dict.
you'll have to read the source to see what is expected of a listitem, but in
general you want to pass in self.args + a new 'action' and a new 'remote_url'
'Title' is also required, anything *should* be optional
"""
if context_menu_items is None:
context_menu_items = []
if bookmark_parent is None:
bookmark_url = self.get_url({'action': 'add_to_bookmarks', 'url': self.get_url(info)})
context_menu_items.append(("Bookmark", "XBMC.RunPlugin(%s)" % (bookmark_url,)))
else:
bminfo = {'action': 'remove_from_bookmarks', 'url': self.get_url(info), 'folder_id': bookmark_parent}
if bookmark_id is not None:
bminfo['bookmark_id'] = bookmark_id
elif bookmark_folder_id is not None:
bminfo['bookmark_folder_id'] = bookmark_folder_id
bookmark_url = self.get_url(bminfo)
context_menu_items.append(("Remove From Bookmarks", "XBMC.RunPlugin(%s)" % (bookmark_url,)))
info.setdefault('Thumb', '')
info.setdefault('Icon', info['Thumb'])
if 'Rating' in info:
del info['Rating']
li=xbmcgui.ListItem(
label=info['Title'],
iconImage=info['Icon'],
thumbnailImage=info['Thumb']
)
if not is_folder:
li.setProperty("IsPlayable", "true")
context_menu_items.append(("Queue Item", "Action(Queue)"))
li.setInfo(type='Video', infoLabels=dict((k, unicode(v)) for k, v in info.iteritems()))
# Add Context Menu Items
if context_menu_items:
li.addContextMenuItems(context_menu_items,
replaceItems=clear_context_menu)
# Handle the return-early case
if not return_only:
kwargs = dict(
handle=self.handle,
url=self.get_url(info),
listitem=li,
isFolder=is_folder
)
return xbmcplugin.addDirectoryItem(**kwargs)
return li
def get_resource_path(self, *path):
"""
Returns a full path to a plugin resource.
eg. self.get_resource_path("images", "some_image.png")
"""
p = os.path.join(__settings__.getAddonInfo('path'), 'resources', *path)
if os.path.exists(p):
return p
raise ChannelException("Couldn't Find Resource: %s" % (p, ))
def get_modal_keyboard_input(self, default=None, heading=None, hidden=False):
keyb = xbmc.Keyboard(default, heading, hidden)
keyb.doModal()
val = keyb.getText()
if keyb.isConfirmed():
return val
return None
def get_existing_bookmarks(self):
fpath = os.path.join(self.plugin.get_cache_dir(), 'canada.on.demand.%s.categories.cache' % (self.get_cache_key(),))
def add_bookmark_folder(self):
curs = self.db_conn.cursor()
curs.execute("select id, name, parent_id, path from bookmark_folders order by path desc")
rows = curs.fetchall()
items = [r[3] for r in rows]
dialog = self.get_dialog()
val = dialog.select("Select a Parent for the New Folder", items)
if val == -1:
return None
parent = rows[val]
name = self.get_modal_keyboard_input('New Folder', 'Enter the name for the new folder')
if name is None:
return None
newpath = parent[3]+"/"+name
curs = self.db_conn.cursor()
curs.execute("select * from bookmark_folders where path=?", (newpath,))
if curs.fetchall():
dialog.ok("Failed!", "Couldn't create folder: %s because it already exists" % (newpath,))
return None
curs.execute("insert into bookmark_folders (name, parent_id, path) values (?, ?, ?)", (name, parent[0], newpath))
curs.execute("select id, name, parent_id, path from bookmark_folders where path=?", (newpath,))
self.db_conn.commit()
return curs.fetchall()[0]
def action_add_to_bookmarks(self):
curs = self.db_conn.cursor()
curs.execute("select id, name, parent_id, path from bookmark_folders order by path asc")
rows = curs.fetchall()
logging.debug(rows)
items = ["(New Folder)"]
items += [r[3] for r in rows]
dialog = self.get_dialog()
val = dialog.select("Select a Bookmark Folder", items)
logging.debug("VAL:%s" % (val,))
if val == -1:
return xbmcplugin.endOfDirectory(self.handle, succeeded=False)
elif val == 0:
folder = self.add_bookmark_folder()
if not folder:
return xbmcplugin.endOfDirectory(self.handle, succeeded=False)
else:
logging.debug("ITEMS:%s" % (items,))
logging.debug("ROWS:%s" % (rows,))
folder = [r for r in rows if r[3]==items[val]][0]
bm = urldecode(self.args['url'].split("?",1)[1])
name = self.get_modal_keyboard_input(bm['Title'], 'Bookmark Title')
if name is None:
return None
curs.execute("select * from bookmarks where folder_id = ? and plugin_url = ?", (folder[0], self.args['url']))
if curs.fetchall():
dialog.ok("Bookmark Already Exists", "This location is already bookmarked in %s" % (folder[3],))
return None
curs.execute("insert into bookmarks (name, folder_id, plugin_url) values (?,?,?)", (name, folder[0], self.args['url']))
self.db_conn.commit()
dialog.ok("Success!", "%s has been bookmarked!" % (name,))
return xbmcplugin.endOfDirectory(self.handle, succeeded=False)
def action_browse_bookmarks(self):
folder_id = int(self.args['folder_id'])
curs = self.db_conn.cursor()
curs.execute("select id, name, parent_id, path from bookmark_folders where parent_id = ?", (folder_id,))
for folder in curs.fetchall():
self.add_list_item({
'Thumb': self.get_resource_path("images", "bookmark.png"),
'folder_id': folder[0],
'Title': "[%s]" % (folder[1],),
'action': 'browse_bookmarks',
}, bookmark_parent=folder_id, bookmark_folder_id=folder[0])
curs.execute("select id, name, plugin_url, folder_id from bookmarks where folder_id = ?", (folder_id,))
logging.debug("Checking For Bookmarks")
bookmarks = curs.fetchall()
if not bookmarks:
self.add_list_item({'Title': '-no bookmarks-'})
else:
for bm in bookmarks:
data = urldecode(bm[2].split("?", 1)[1])
data['Title'] = bm[1]
self.add_list_item(data, is_folder=True, bookmark_parent=bm[3], bookmark_id=bm[0])
self.end_list(sort_methods=(xbmcplugin.SORT_METHOD_LABEL,))
def action_remove_from_bookmarks(self):
logging.debug("REMOVE BOOKMARK: %s" % (self.args['url'],))
is_folder = bool(self.args.get('bookmark_folder_id', False))
parent_id = self.args['folder_id']
if is_folder:
return self.remove_folder_from_bookmarks(parent_id=parent_id, folder_id=self.args['bookmark_folder_id'])
else:
return self.remove_bookmark_from_bookmarks(parent_id=parent_id, bookmark_id=self.args['bookmark_id'])
def remove_folder_from_bookmarks(self, parent_id, folder_id):
curs = self.db_conn.cursor()
curs.execute("select id, name, parent_id, path from bookmark_folders where parent_id = ? and id = ?", (parent_id, folder_id))
record = curs.fetchall()[0]
dialog = self.get_dialog()
if dialog.yesno("Are you Sure?", "Are you sure you wish to delete the bookmark folder: %s\n(All Bookmarks and Folders within it will be deleted!)" % (record[3],)):
logging.debug("BM:Removing Bookmark Folder!")
curs.execute("select id from bookmark_folders where path like ?", (record[3]+"%",))
rows = curs.fetchall()
for row in rows:
logging.debug("deleting row: %s" % (row,))
curs.execute("delete from bookmark_folders where id=?", row)
curs.execute("delete from bookmarks where folder_id=?", row)
self.db_conn.commit()
return xbmc.executebuiltin("Container.Refresh")
def remove_bookmark_from_bookmarks(self, parent_id, bookmark_id):
curs = self.db_conn.cursor()
curs.execute("select id, name, folder_id, plugin_url from bookmarks where folder_id = ? and id = ?", (parent_id, bookmark_id))
record = curs.fetchall()[0]
dialog = self.get_dialog()
if dialog.yesno("Are you Sure?", "Are you sure you wish to delete the bookmark: %s" % (record[1],)):
logging.debug("BM:Removing Bookmark!")
curs.execute("delete from bookmarks where folder_id = ? and id = ?", (parent_id, bookmark_id))
self.db_conn.commit()
else:
logging.debug("They Said No?")
return xbmc.executebuiltin("Container.Refresh")
def action_plugin_root(self):
self.add_list_item({
'Title': 'Bookmarks',
'action': 'browse_bookmarks',
'folder_id': 1,
'Thumb': self.get_resource_path("images", "bookmark.png")
}, bookmark_parent=0)
self.add_list_item({
'Title': 'All Channels',
'action': 'channel_list',
'Thumb': os.path.join(__settings__.getAddonInfo('path'), 'icon.png')
})
self.end_list()
def __call__(self):
"""
This is the main entry point of the plugin.
the querystring has already been parsed into self.args
"""
action = self.args.get('action', None)
if not action:
action = 'plugin_root'
if hasattr(self, 'action_%s' % (action,)):
func = getattr(self, 'action_%s' % (action,))
return func()
# If there is an action, then there should also be a channel
channel_code = self.args.get('channel', None)
# The meta class has a registry of all concrete Channel subclasses
# so we look up the appropriate one here.
channel_class = ChannelMetaClass.registry.channels[channel_code]
chan = channel_class(self, **self.args)
return chan()
def check_cache(self):
cachedir = self.get_cache_dir()
version_file = os.path.join(cachedir, __version__)
if not os.path.exists(version_file):
shutil.rmtree(cachedir)
os.mkdir(cachedir)
f = open(os.path.join(cachedir, __version__), 'w')
f.write("\n")
f.close()
def __init__(self, script_url, handle, querystring):
proxy = self.get_setting("http_proxy")
port = self.get_setting("http_proxy_port")
if proxy and port:
proxy_handler = urllib2.ProxyHandler({'http':'%s:%s'%(proxy,port)})
opener = urllib2.build_opener(proxy_handler)
urllib2.install_opener(opener)
self.script_url = script_url
self.handle = int(handle)
if len(querystring) > 2:
self.querystring = querystring[1:]
items = urldecode(self.querystring)
self.args = dict(items)
else:
self.querystring = querystring
self.args = {}
self.connect_to_db()
self.check_cache()
logging.debug("Constructed Plugin %s" % (self.__dict__,))
if __name__ == '__main__':
plugin = OnDemandPlugin(*sys.argv)
plugin()