[064626]: plugin.video.elisa.viihde / addon.py  Maximize  Restore  History

Download this file

402 lines (333 with data), 13.3 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
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
# -*- coding: iso-8859-1 -*-
import urllib
import urllib2
import re
import cookielib
import os
import sys
import time
import datetime
import threading
# Enable Eclipse debugger
REMOTE_DBG = False
# append pydev remote debugger
if REMOTE_DBG:
# Make pydev debugger works for auto reload.
# Note pydevd module need to be copied in XBMC\system\python\Lib\pysrc
try:
import pysrc.pydevd as pydevd
# stdoutToServer and stderrToServer redirect stdout and stderr to eclipse console
pydevd.settrace('localhost', stdoutToServer=True, stderrToServer=True)
except ImportError:
sys.stderr.write("Error: " +
"You must add org.python.pydev.debug.pysrc to your PYTHONPATH.")
sys.exit(1)
try:
import xbmc
import xbmcplugin
import xbmcgui
import xbmcaddon
import simplejson
__settings__ = xbmcaddon.Addon(id='plugin.video.elisa.viihde')
__language__ = __settings__.getLocalizedString
BASE_RESOURCE_PATH = xbmc.translatePath( os.path.join( __settings__.getAddonInfo('path'), "resources" ) )
sys.path.append( os.path.join( BASE_RESOURCE_PATH, "lib" ) )
vkopaivat = {0:__language__(30006), 1:__language__(30007), 2:__language__(30008), 3:__language__(30009), 4:__language__(30010), 5:__language__(30011), 6:__language__(30012)}
except ImportError:
pass
#Elisa Viihde
import BeautifulSoup
time_format = "%d.%m.%Y %H:%M:%S"
class UpdateProgramDataThread(threading.Thread):
def __init__(self, prgid, link):
self.prgid = prgid
self.link = link
super(UpdateProgramDataThread, self).__init__()
def run(self):
prog_data = get_prog_data(self.prgid)
if prog_data.get('tn'):
self.link.setThumbnailImage(prog_data['tn'])
self.link.setInfo('video', dict(plotoutline=prog_data['short_text'],
plot=prog_data['short_text'],
))
def get_login_url(username, password):
return "http://elisaviihde.fi/etvrecorder/login.sl?username=" + username + "&password=" + password + "&savelogin=true&ajax=true"
#logging in
def login(username=None, password=None):
if 'xbmc' in globals():
COOKIEFILE = xbmc.translatePath('special://profile/addon_data/plugin.video.elisa.viihde/cookies.lwp')
else:
COOKIEFILE = ''
if username == None:
username = __settings__.getSetting("username")
if password == None:
password = __settings__.getSetting("password")
urlopen = urllib2.urlopen
cj = cookielib.LWPCookieJar() # This is a subclass of FileCookieJar that has useful load and save methods
Request = urllib2.Request
if os.path.isfile(COOKIEFILE):
cj.load(COOKIEFILE)
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
txdata = None
txheaders = {'User-agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}
login_url = get_login_url(username, password)
req = Request(login_url, txdata, txheaders)
handle = urlopen(req)
if COOKIEFILE:
cj.save(COOKIEFILE)
def get_params():
param=[]
paramstring=sys.argv[2]
if len(paramstring)>=2:
params=sys.argv[2]
cleanedparams=params.replace('?','')
if (params[len(params)-1]=='/'):
params=params[0:len(params)-2]
pairsofparams=cleanedparams.split('&')
param={}
for i in range(len(pairsofparams)):
splitparams={}
splitparams=pairsofparams[i].split('=')
if (len(splitparams))==2:
param[splitparams[0]]=splitparams[1]
return param
def add_search():
u=sys.argv[0]+"?search="+str('indiana')
print "Search: " + u
liz=xbmcgui.ListItem(label=__language__(30017), iconImage="DefaultFolder.png")
xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=liz,isFolder=True)
return liz
def parse_datetime(dtstring, format="%d.%m.%Y %H:%M"):
"""
Parse a datetime object from datetime string.
@param dtstring: The datetime string
@param format: excepted format of parse string
e.g. 'Su 05.05.2012 12:00'
"""
parts = dtstring.split(' ')
if len(parts) > 2:
dtstring = ' '.join(parts[-2:])
parsestr = time.strptime
print "DATETIME parsing %s, %s" % (dtstring, format)
parsed_time = parsestr(dtstring, format)
return datetime.datetime.fromtimestamp(time.mktime(parsed_time))
def parse_program_id(program_url_str):
"""
parse the program id from the url string
"""
return program_url_str.split('=')[1]
def search_items(qstr):
print "search_items: " + qstr
prog_url = "http://elisaviihde.fi/etvrecorder/search.sl?q=" + str(qstr)
req = urllib2.Request(prog_url)
response = urllib2.urlopen(req)
search_html = response.read()
search_html= fix_chars(search_html)
# parse the search data from html
search_data = BeautifulSoup.BeautifulSoup(search_html)
rows = []
existingh3 = search_data.find('h3', {'class': 'recordings_exist'})
if existingh3:
rtable = existingh3.findNextSibling('table', {'class': 'recordings_table'})
for row in rtable.findAll('tr'):
tds = row.findAll('td')
link_td = tds[2].a
channel_td = tds[3]
time_td = tds[4]
stime = parse_datetime(time_td.text)
drow = {'name': link_td.text,
'desc': link_td['title'],
'url': link_td['href'],
'channel': channel_td.text,
'start_time': stime.strftime(time_format),
'program_id': parse_program_id(link_td['href'])
}
rows.append(drow)
return rows
def show_search_items(qstr):
print "search_items: " + qstr
items = search_items(qstr)
for item in items:
name = create_name(item)
link = add_watch_link(name,
item['program_id'],
totalItems=len(items),
)
def add_dir(name,id,iconimage):
u=sys.argv[0]+"?id="+str(id)
liz=xbmcgui.ListItem(label=name,iconImage="DefaultFolder.png", thumbnailImage=iconimage)
liz.setInfo('video', { "Title": name } )
xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=liz,isFolder=True)
return liz
def add_watch_link(name, progid, totalItems=None, **kwargs):
u = sys.argv[0] + "?watch=true&progid=" + str(progid)
liz=xbmcgui.ListItem(name)
kwargs['Title'] = name
liz.setInfo('video', kwargs)
xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]), url=u, listitem=liz, totalItems=totalItems)
return liz
def get_prog_data(prog_id):
prog_url = "http://elisaviihde.fi/etvrecorder/program.sl?programid=" + str(prog_id) + "&ajax"
# Limit concurrent download threads to 60
# It there are too many threads (depending on hardware and OS)
# the master Python process will stop execution and whole plugin fails.
#
# Also it seems that Elisa.fi is throttling concurrent requests from
# same client, so better not bombard them with over 300 requests per
# second anyway.
#
# Best solution would be a thread pool, but this quick hack
# "while sleep" solves the problem too.
while threading.active_count() > 60:
time.sleep(2)
req = urllib2.Request(prog_url)
response = urllib2.urlopen(req)
link = response.read()
link = fix_chars(link)
prog_data = simplejson.loads(link)
return prog_data
def create_name(prog_data):
parsed_time = time.strptime(prog_data['start_time'], time_format)
weekday_numb = int(time.strftime("%w", parsed_time))
prog_date = datetime.date.fromtimestamp(time.mktime(parsed_time))
today = datetime.date.today()
diff = today - prog_date
if diff.days == 0:
date_name = __language__(30013) + " " + time.strftime("%H:%M", parsed_time)
elif diff.days == 1:
date_name = __language__(30014) + " " + time.strftime("%H:%M", parsed_time)
else:
date_name = str(vkopaivat[weekday_numb])+" "+time.strftime("%d.%m.%Y %H:%M",parsed_time)
return prog_data['name'] + " (" + prog_data['channel'] + ", " + date_name + ")"
def watch_program(prog_id):
prog_data = get_prog_data(prog_id)
print prog_data
url = prog_data['url']
name = create_name(prog_data)
listitem = xbmcgui.ListItem(name, thumbnailImage=prog_data['tn'])
listitem.setInfo('video', {'Title': name, 'plot': prog_data['short_text']})
xbmc.Player().play(url, listitem)
#mark program watched
view_url = "http://elisaviihde.fi/etvrecorder/program.sl?programid=" + str(prog_id) + "&view=true"
urllib2.urlopen(view_url)
return True
def fix_chars(string):
string = string.replace("%20", " ")
string = re.sub('%C3%A4','\u00E4',string) #\E4
string = re.sub('%C3%B6','\u00F6',string) #\F6
string = re.sub('%C3%A5','\u00E5',string) #\E5
string = re.sub('%C3%84','\u00C4',string) #\C4
string = re.sub('%C3%96','\u00D6',string) #\D6
string = re.sub('%C3%85','\u00C5',string) #\C5
string = re.sub('%2C', ',',string) #pilkku
string = re.sub('%26', '&',string) #&
string = re.sub('%3F', '?',string) #?
string = re.sub('%3A', ':',string) #:
string = re.sub('%2F', '/',string) #/
return string
def show_dir(id):
if str(id)=="0":
#show root directory
folder_id = ""
add_search()
else:
#show directory by id
folder_id = str(id)
folder_url = "http://elisaviihde.fi/etvrecorder/ready.sl?folderid=" + folder_id + "&ajax"
response = urllib2.urlopen(folder_url)
link = response.read()
link = fix_chars(link)
response.close()
data = simplejson.loads(link)
data = data['ready_data']
data = data.pop()
totalItems = len(data['folders']) + len(data['recordings'])
#list folders
for row in data['folders']:
name = row['name']
id = row['id']
add_dir(name, id, "")
#list recordings
for row in data['recordings']:
if row['viewcount'] == "0":
print_star = "* "
else:
print_star = ""
parsed_time = time.strptime(row['timestamp'][:-5], "%Y-%m-%dT%H:%M:%S")
weekday_numb = int(time.strftime("%w", parsed_time))
starttime = time.strftime("%d.%m %H:%M", parsed_time)
prog_date = datetime.date.fromtimestamp(time.mktime(parsed_time))
today = datetime.date.today()
diff = today - prog_date
if diff.days == 0:
date_name = __language__(30013) + " " + time.strftime("%H:%M", parsed_time)
elif diff.days == 1:
date_name = __language__(30014) + " " + time.strftime("%H:%M", parsed_time)
else:
date_name = str(vkopaivat[weekday_numb]) + " " + time.strftime("%d.%m.%Y %H:%M", parsed_time)
date_string = time.strftime("%Y-%m-%d", parsed_time)
name = print_star + row['name'] + " (" + row['channel'] + ", " + date_name + ")"
link = add_watch_link(name,
row['program_id'],
playcount=row['viewcount'],
totalItems=totalItems,
duration=row['length'],
date=date_string
)
t = UpdateProgramDataThread(row['program_id'],
link)
t.start()
def mainloop():
#check login
username = __settings__.getSetting("username")
password = __settings__.getSetting("password")
response = urllib2.urlopen(get_login_url(username, password))
link = response.read()
if not str(link) == "TRUE":
dialog = xbmcgui.Dialog()
ok = dialog.ok('XBMC', __language__(30003), __language__(30004))
if ok == True:
__settings__.openSettings(url=sys.argv[0])
else:
login()
params = get_params()
#dialog = xbmcgui.Dialog()
#ok = dialog.ok('XBMC', str(params))
folder_id = None
prog_id = None
watch = None
search = None
try:
folder_id = int(params["id"])
except:
pass
try:
prog_id = int(params["progid"])
except:
pass
try:
watch = str(params["watch"])
except:
pass
try:
search = str(params["search"])
print "Searching for " + search
except:
pass
if search <> None:
keyboard = xbmc.Keyboard()
keyboard.doModal()
if (keyboard.isConfirmed()):
show_search_items(str(keyboard.getText()))
elif folder_id == None and prog_id == None:
show_dir("0")
elif prog_id == None and folder_id <> None:
show_dir(str(folder_id))
elif watch == "true" and prog_id <> None:
watch_program(str(prog_id))
else:
show_dir("0")
xbmcplugin.endOfDirectory(int(sys.argv[1]))
if __name__ == '__main__':
mainloop()