-
Notifications
You must be signed in to change notification settings - Fork 29
/
default.py
executable file
·227 lines (173 loc) · 7.46 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
'''
gdrive XBMC Plugin
Copyright (C) 2013 dmdsoftware
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
from resources.lib import gdrive
import sys
import urllib
import cgi
import re
import xbmc, xbmcgui, xbmcplugin, xbmcaddon
def log(msg, err=False):
if err:
xbmc.log(addon.getAddonInfo('name') + ': ' + msg, xbmc.LOGERROR)
else:
xbmc.log(addon.getAddonInfo('name') + ': ' + msg, xbmc.LOGDEBUG)
def parse_query(query):
queries = cgi.parse_qs(query)
q = {}
for key, value in queries.items():
q[key] = value[0]
q['mode'] = q.get('mode', 'main')
return q
def addVideo(url, infolabels, img='', fanart='', total_items=0,
cm=[], cm_replace=False):
infolabels = decode_dict(infolabels)
log('adding video: %s - %s' % (infolabels['title'], url))
listitem = xbmcgui.ListItem(infolabels['title'], iconImage=img,
thumbnailImage=img)
listitem.setInfo('video', infolabels)
listitem.setProperty('IsPlayable', 'true')
listitem.setProperty('fanart_image', fanart)
if cm:
listitem.addContextMenuItems(cm, cm_replace)
xbmcplugin.addDirectoryItem(plugin_handle, url, listitem,
isFolder=False, totalItems=total_items)
#http://stackoverflow.com/questions/1208916/decoding-html-entities-with-python/1208931#1208931
def _callback(matches):
id = matches.group(1)
try:
return unichr(int(id))
except:
return id
def decode(data):
return re.sub("&#(\d+)(;|(?=\s))", _callback, data).strip()
def decode_dict(data):
for k, v in data.items():
if type(v) is str or type(v) is unicode:
data[k] = decode(v)
return data
plugin_url = sys.argv[0]
plugin_handle = int(sys.argv[1])
plugin_queries = parse_query(sys.argv[2][1:])
addon = xbmcaddon.Addon(id='plugin.video.gdrive')
#plugin_path = addon.getAddonInfo('path')
try:
remote_debugger = addon.getSetting('remote_debugger')
remote_debugger_host = addon.getSetting('remote_debugger_host')
# append pydev remote debugger
if remote_debugger == 'true':
# Make pydev debugger works for auto reload.
# Note pydevd module need to be copied in XBMC\system\python\Lib\pysrc
import pysrc.pydevd as pydevd
# stdoutToServer and stderrToServer redirect stdout and stderr to eclipse console
pydevd.settrace(remote_debugger_host, stdoutToServer=True, stderrToServer=True)
except ImportError:
sys.stderr.write("Error: " + "You must add org.python.pydev.debug.pysrc to your PYTHONPATH.")
sys.exit(1)
except :
pass
username = addon.getSetting('username')
password = addon.getSetting('password')
auth_writely = addon.getSetting('auth_writely')
auth_wise = addon.getSetting('auth_wise')
user_agent = addon.getSetting('user_agent')
save_auth_token = addon.getSetting('save_auth_token')
useWRITELY = addon.getSetting('force_writely')
mode = plugin_queries['mode']
# allow for playback of public videos without authentication
if (mode.lower() == 'streamurl'):
authenticate = False
else:
authenticate = True
# you need to have at least a username&password set or an authorization token
if ((username == '' or password == '') and (auth_writely == '' and auth_wise == '') and (authenticate == True)):
xbmcgui.Dialog().ok(addon.getLocalizedString(30000), 'Set the Username and Password in addon settings before running.')
log('Set the Username and Password in addon settings before running.', True)
xbmcplugin.endOfDirectory(plugin_handle)
#let's log in
gdrive = gdrive.gdrive(username, password, auth_writely, auth_wise, user_agent, authenticate)
log('plugin google authorization: ' + gdrive.returnHeaders())
log('plugin url: ' + plugin_url)
log('plugin queries: ' + str(plugin_queries))
log('plugin handle: ' + str(plugin_handle))
#dump a list of videos available to play
if mode.lower() == 'main':
log(mode)
cacheType = addon.getSetting('playback_type')
if cacheType == 0:
videos = gdrive.getVideosHashMemoryCache()
else:
videos = gdrive.getVideosHashStream()
for title in sorted(videos.iterkeys()):
addVideo(videos[title]['url'],
{ 'title' : title , 'plot' : title },
img=videos[title]['thumbnail'])
#play a URL that is passed in (presumably requires authorizated session)
elif mode.lower() == 'play':
url = plugin_queries['url']
item = xbmcgui.ListItem(path=url)
log('play url: ' + url)
xbmcplugin.setResolvedUrl(int(sys.argv[1]), True, item)
#play a video given its exact-title
elif mode.lower() == 'playvideo':
title = plugin_queries['title']
log('play title: ' + title)
cacheType = addon.getSetting('playback_type')
if cacheType == 0:
videoURL = gdrive.getVideoLink(title)
else:
videoURL = gdrive.getVideoPlayerLink(title)
#effective 2014/02, video stream calls require a wise token instead of writely token
if useWRITELY == 'true':
videoURL = videoURL + '|' + gdrive.returnHeaders(True)
else:
videoURL = videoURL + '|' + gdrive.returnHeaders()
item = xbmcgui.ListItem(path=videoURL)
log('play url: ' + videoURL)
xbmcplugin.setResolvedUrl(int(sys.argv[1]), True, item)
#force memory-cache - play a video given its exact-title
elif mode.lower() == 'memorycachevideo':
title = plugin_queries['title']
videoURL = gdrive.getVideoLink(title)
#effective 2014/02, video stream calls require a wise token instead of writely token
if useWRITELY == 'true':
videoURL = videoURL + '|' + gdrive.returnHeaders(True)
else:
videoURL = videoURL + '|' + gdrive.returnHeaders()
item = xbmcgui.ListItem(path=videoURL)
log('play url: ' + videoURL)
xbmcplugin.setResolvedUrl(int(sys.argv[1]), True, item)
#force stream - play a video given its exact-title
elif mode.lower() == 'streamvideo':
title = plugin_queries['title']
videoURL = gdrive.getVideoPlayerLink(title)
item = xbmcgui.ListItem(path=videoURL)
log('play url: ' + videoURL)
xbmcplugin.setResolvedUrl(int(sys.argv[1]), True, item)
elif mode.lower() == 'streamurl':
url = plugin_queries['url']
videoURL = gdrive.getPlayerLinkURL(url)
item = xbmcgui.ListItem(path=videoURL)
log('play url: ' + videoURL)
xbmcplugin.setResolvedUrl(int(sys.argv[1]), True, item)
#clear the authorization token
elif mode.lower() == 'clearauth':
addon.setSetting('auth_writely', '')
addon.setSetting('auth_wise', '')
# if we don't have an authorization token set for the plugin, set it with the recent login.
# auth_token will permit "quicker" login in future executions by reusing the existing login session (less HTTPS calls = quicker video transitions between clips)
if auth_writely == '' and save_auth_token == 'true':
addon.setSetting('auth_writely', gdrive.writely)
addon.setSetting('auth_wise', gdrive.wise)
xbmcplugin.endOfDirectory(plugin_handle)