-
Notifications
You must be signed in to change notification settings - Fork 0
/
default.py
617 lines (436 loc) · 19.1 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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
# -*- coding: utf-8 -*-
"""
WebBrowser plugin
Auteur : CinPoU
"""
import sys,os,re, shutil
import urllib, urllib2
import xbmcplugin,xbmcgui,xbmc
import xml.dom.minidom
import unicodedata
from xbmcaddon import Addon
import random, string
#Browser - by CinPoU
# script constants
__plugin__ = "WebBrowser"
__addonID__ = "plugin.programm.webbrowser"
__author__ = "CinPoU"
__url__ = "https://github.com/CinPoU/XBMC-WebBrowser"
__credits__ = ""
__platform__ = "xbmc media center, [LINUX, OS X, WIN32]"
__date__ = "01-01-2012"
__version__ = "1.6"
__settings__ = Addon(id=__addonID__)
__string__ = __settings__.getLocalizedString
__language__ = __settings__.getLocalizedString
# source path for launchers data
PLUGIN_DATA_PATH = xbmc.translatePath( os.path.join( "special://profile/addon_data", "plugin.programm.webbrowser") )
# INITIALISATION CHEMIN RACINE
ROOTDIR = __settings__.getAddonInfo('path')
# Shared resources
BASE_RESOURCE_PATH = os.path.join( ROOTDIR, "resources" )
# append the proper platforms folder to our path, xbox is the same as win32
env = ( os.environ.get( "OS", "win32" ), "win32", )[ os.environ.get( "OS", "win32" ) == "xbox" ]
sys.path.append( os.path.join( BASE_RESOURCE_PATH, "platform_libraries", env ) )
# append the proper libs folder to our path
sys.path.append( os.path.join( BASE_RESOURCE_PATH, "libs" ) )
# append the proper etree folder to our path
sys.path.append( os.path.join( BASE_RESOURCE_PATH, "libs", "elementtree" ) )
#modules custom
from specialpath import *
from httpsConnect import *
from elementtree import ElementTree
#Initialisation des chemins
xmlUserDir = os.path.join( PLUGIN_DATA_PATH, "xml" )
if not os.path.isdir(xmlUserDir) :
os.mkdir(xmlUserDir)
imgUserDir = os.path.join( PLUGIN_DATA_PATH, "img" )
if not os.path.isdir(imgUserDir) :
os.mkdir(imgUserDir)
cookieUserDir = os.path.join( PLUGIN_DATA_PATH, "cookie" )
if not os.path.isdir(cookieUserDir) :
os.mkdir(cookieUserDir)
imgDir = os.path.join(BASE_RESOURCE_PATH,"images")
defaultIcon = os.path.join(imgDir,"Default.png")
defaultGoogleIcon = os.path.join(imgDir,"DefaultGoogle.png")
defaultAddIcon = os.path.join(imgDir,"DefaultAdd.png")
defaultLaunchIcon = os.path.join(imgDir,"launch.png")
defaultFolderIcon = os.path.join(imgDir,"DefaultFolder.png")
defaultGoogleFolderIcon = os.path.join(imgDir,"DefaultGoogleFolder.png")
dialog = xbmcgui.Dialog()
#DEF
#Clavier
def show_keyboard (textDefault, textHead, textHide=False) :
"""Show the keyboard's dialog"""
keyboard = xbmc.Keyboard(textDefault, textHead)
inputText = ""
if textHide == True :
keyboard.setHiddenInput(True)
keyboard.doModal()
if (keyboard.isConfirmed()):
inputText = keyboard.getText()
dialogInfo = xbmcgui.Dialog()
del keyboard
return inputText
def cleanString(s):
"""Removes all accents from the string"""
if isinstance(s,str):
s = unicode(s,"utf8","replace")
s = unicodedata.normalize('NFD',s)
return s.encode('ascii','ignore')
#Lance le navigateur
def launchBrowser(url) :
if (os.environ.get( "OS", "xbox" ) == "xbox"):
xbmc.executebuiltin('XBMC.Runxbe(' + cmd + ' '+ __settings__.getSetting('arguments') + ' '+ url + ')')
else:
if (sys.platform == 'win32'):
xbmc.executebuiltin("%s(\"\\\"%s\\\" %s %s\")" % ("System.ExecWait", cmd, __settings__.getSetting('arguments') , url))
elif (sys.platform.startswith('linux')):
url = '"'+url+'"'
#os.system("%s %s" % ("/usr/bin/vlc", url))
os.system("%s %s %s" % (cmd,__settings__.getSetting('arguments'), url))
else:
pass;
# unsupported platform
def writeXml(xmlPath) :
if not os.path.isfile( xmlPath ):
xmlSrc = os.path.join( BASE_RESOURCE_PATH, "modeles" , "default.xml" )
try :
shutil.copy2(xmlSrc,xmlPath)
except : "Unable to write the xml file"
#Lister les Favoris Google
def getGoogleXml() :
#Create the Xml
xmlPath = os.path.join( xmlUserDir, "google.xml" )
writeXml(xmlPath)
#Adresse du formulaire de connexion
the_url = 'https://www.google.com/accounts/Login?hl=fr&continue=https://www.google.com/bookmarks/?output=rss&num=1000'
#Initialisation de HttpsConnect
cookieFile = os.path.join( cookieUserDir, 'cookies.lwp' )
#print cookieFile
test = HttpsConnect(cookieFile,userAgent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)')
#print test
the_page = test.httpsRequest(the_url,dataRequest={},headerRequest={})[0]
#Traitement du formulaire
dshPattern = re.compile('name="dsh" id="dsh" value="(.+?)"')
dshInput = dshPattern.search(the_page).groups()
galxPattern = re.compile('<input type="hidden"\s*name="GALX"\s*value="(.+?)"')
galxInput = galxPattern.search(the_page).groups()
print galxInput, dshInput
#Information de connexion
url = 'https://www.google.com/accounts/LoginAuth'
values = {'continue' : 'https://www.google.com/bookmarks/?output=rss',
'dsh' : dshInput[0],
'GALX' : galxInput[0],
'Email' : __settings__.getSetting('login'),
'Passwd' : __settings__.getSetting('password'),
'PersistentCookie' : "yes",
'rmShown' : "1",
'signIn' : "Connexion",
'asts' : '' }
#Identification
test.httpsRequest(url,dataRequest=values,headerRequest={})
#Requêtes
readXml = test.httpsRequest("https://www.google.com/bookmarks/?output=rss")[0]
xmlFile=open(xmlPath,"wb") #on créé un fichier en écriture mode binaire
xmlFile.write(readXml) #on ecrit ce qu'on lit de l'image
xmlFile.close()#on ferme le fichier image
def returnGoogle() :
xmlPath = os.path.join( xmlUserDir, "google.xml" )
xmlFile=open(xmlPath,"r")
txt = xmlFile.read()
xmlFile.close()
tree = ElementTree.fromstring(txt)
channel = tree.find("channel")
bookmarks = channel.findall("item")
googleDico = {}
googleMain = []
for bookmark in bookmarks:
title = bookmark.findtext("title")
url = bookmark.findtext("link")
guid = bookmark.findtext("{http://www.google.com/history/}bkmk_id")
#print guid
tags = bookmark.findall("{http://www.google.com/history/}bkmk_label")
googleItem = {"title" : title , "url" : url , "guid" : guid}
if len(tags) == 0 :
googleMain.append(googleItem)
else :
for tag in tags :
if googleDico.has_key(tag.text) :
googleItems = googleDico[tag.text]
googleItems.append(googleItem)
else :
googleItems = []
googleItems.append(googleItem)
googleDico[tag.text] = googleItems
#print googleDico
#print googleMain
return googleDico, googleMain
#Récupère les paramètres
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
#Ouvrir une URL
def launchUrl():
u=sys.argv[0]+"?mode=3"
ok=True
liz=xbmcgui.ListItem(__language__(11), iconImage=defaultLaunchIcon, thumbnailImage=defaultLaunchIcon)
liz.setInfo( type="files", infoLabels={ "Title": __language__(11) } )
ok=xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=liz)
return ok
#Ajoute un lien
def addLink(name,url,iconimage, imgPath , readonly = False):
u=sys.argv[0]+"?url="+urllib.quote_plus(url)+"&mode=2&name="+urllib.quote_plus(name)
ok=True
liz=xbmcgui.ListItem(name, iconImage=iconimage, thumbnailImage=iconimage)
liz.setInfo( type="files", infoLabels={ "Title": name } )
c_items = []
# ajout d'un bouton dans le contextmenu pour rafraichir l'image
refreshLabelButton = __language__(201)
refreshActionButton = 'XBMC.RunPlugin(%s?mode=7&name=%s&url=%s)' % ( sys.argv[ 0 ], urllib.quote_plus( imgPath ), urllib.quote_plus( url ), )
c_items.append(( refreshLabelButton, refreshActionButton ))
if readonly == False :
deleteLabelButton = __language__(202)
deleteActionButton = 'XBMC.RunPlugin(%s?mode=8&name=%s&url=%s)' % ( sys.argv[ 0 ], urllib.quote_plus( name ), urllib.quote_plus( url ), )
c_items.append(( deleteLabelButton, deleteActionButton ))
else :
addXmlLabelButton = __language__(203)
addXmlActionButton = 'XBMC.RunPlugin(%s?mode=6&name=%s&url=%s)' % ( sys.argv[ 0 ], urllib.quote_plus( name ), urllib.quote_plus( url ), )
c_items.append(( addXmlLabelButton, addXmlActionButton ))
# replaceItems=True, seulement mon bouton va etre visible dans le contextmenu
# si on veut tous les boutons plus le notre on mets rien listitem.addContextMenuItems( c_items ), car False est par default
liz.addContextMenuItems( c_items, replaceItems=True )
ok=xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=liz)
return ok
#Ajoute un dossier
def addDir(name,url,mode,iconimage):
u=sys.argv[0]+"?url="+urllib.quote_plus(url)+"&mode="+str(mode)+"&name="+urllib.quote_plus(name)
ok=True
iconimage = os.path.join(imgDir,iconimage)
liz=xbmcgui.ListItem(name, iconImage=iconimage, thumbnailImage=iconimage)
liz.setInfo( type="Video", infoLabels={ "Title": name } )
ok=xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=liz,isFolder=True)
return ok
def randomString(length=None):
# on définit les caractères acceptés
chars = string.letters + string.digits
# longueur de la chaine, paramètre défini ou entre 6 et 24
length = length or random.randint(6,24)
# une chaine concaténée a partir d'un choix aléatoire d'éléments
return ''.join(random.sample(chars, length))
def randomFile(dirUrl, fileExt) :
fileName = randomString()
fileUrl = os.path.join(dirUrl, fileName)
return fileName
def getThumb(PageWebURL,name) :
# état des variables initiales
ThumbSize = "640x480" #taille en pixel, pas plus de 1024
urllib.urlcleanup()
ThumbUrl = "http://www.apercite.fr/api/apercite/%s/non/non/%s" % (ThumbSize , PageWebURL)
#Ecriture de l'image
#name = "%s.jpg" % (name)
imgPath = os.path.join(imgUserDir, name)
f=open(imgPath,"wb") #on créé un fichier en écriture mode binaire
f.write(urllib.urlopen(ThumbUrl).read()) #on ecrit ce qu'on lit de l'image
f.close()#on ferme le fichier image
return imgPath
def checkThumb(PageWebURL,name=None) :
if name == None :
name = randomFile(imgUserDir)
name = "%s.jpg" % (name)
imgPath = os.path.join(imgUserDir, name)
if not os.path.isfile(imgPath) :
getThumb(PageWebURL,name)
return name
#Ajouter un favoris
def addBookmark(discuss = True, titleStr = "", urlStr = ""):
xmlPath = os.path.join(xmlUserDir , "default.xml")
if discuss == True :
urlStr = show_keyboard ("http://", __language__(101))
titleStr = show_keyboard ("", __language__(102))
imgPath = cleanString(titleStr)
titleStr = "%s" % titleStr.decode("utf-8")
#print titleStr, imgPath
if urlStr != "" and titleStr != "" :
#Création de la miniature :
getThumb(urlStr,imgPath)
# Open the document
doc=xml.dom.minidom.parse(xmlPath)
main=doc.childNodes[0]
# Create a main <fav> element
fav = doc.createElement("fav")
favbal=main.appendChild(fav)
# Create the fav <title> element
title = doc.createElement("title")
titlebal=favbal.appendChild(title)
# Give the <title> element some text
titletext = doc.createTextNode(titleStr)
titlebal.appendChild(titletext)
# Create the fav <link> element
link = doc.createElement("link")
linkbal=favbal.appendChild(link)
# Give the <title> element some text
linktext = doc.createTextNode(urlStr)
linkbal.appendChild(linktext)
# Create the fav <image> element
image = doc.createElement("image")
imagebal=favbal.appendChild(image)
# Give the <image> element some text
imagelink = os.path.join
imagetext = doc.createTextNode("%s" % imgPath)
imagebal.appendChild(imagetext)
# Print our newly created XML
#print doc.toprettyxml(encoding="UTF-8")
outputfile = open(xmlPath, 'wb')
outputfile.write(doc.toxml(encoding="UTF-8"))
outputfile.close()
dialog.ok(__language__(105) , __language__(106))
else :
dialog.ok(__language__(103) , __language__(104))
def removeXml(nameBookmark, urlBookmark) :
xmlPath = os.path.join(xmlUserDir , "default.xml")
# Open the document
doc=xml.dom.minidom.parse(xmlPath)
node = doc.firstChild
for e in node.childNodes :
if e.nodeType == e.ELEMENT_NODE and e.localName == "fav" :
foundUrl = False
foundName = False
for c in e.childNodes :
if c.nodeType == c.ELEMENT_NODE and c.localName == "title" :
if c.firstChild.nodeValue.encode("utf-8") == nameBookmark :
foundName = True
#print "foundName"
if c.nodeType == c.ELEMENT_NODE and c.localName == "link" :
#print "ok"
if c.firstChild.nodeValue.encode("utf-8") == urlBookmark :
foundUrl = True
#print "foundUrl"
if foundUrl == True and foundName == True :
node.removeChild(e)
#print "removeChild"
outputfile = open(xmlPath, 'wb')
outputfile.write(doc.toxml(encoding="UTF-8"))
outputfile.close()
dialog.ok(__language__(107) , __language__(106))
break
def getXmlFav(xmlName) :
#Path of the xml file
xmlPath = os.path.join( xmlUserDir, xmlName )
#Parse xml file
document = xml.dom.minidom.parse(xmlPath)
for item in document.getElementsByTagName('fav'):
name = item.getElementsByTagName('title')[0].firstChild.data
url = item.getElementsByTagName('link')[0].firstChild.data
imgPath = item.getElementsByTagName('image')[0].firstChild.data
thumbnail = "%s.jpg" % imgPath
thumbnail = os.path.join(imgUserDir, thumbnail)
addLink(name.encode("utf-8"),url,thumbnail,imgPath)
#INIT
#Init Parameter
if __settings__.getSetting('cmdline') == "" and __settings__.getSetting('path') == "":
pass #__settings__.openSettings(url=sys.argv[0])
elif __settings__.getSetting('cmdline') != "" :
cmd = __settings__.getSetting('cmdline')
else:
cmd = __settings__.getSetting('path')
#Variable
params = get_params()
url = None
name = None
mode = None
#Language
TextLanguage = __language__
#Recup Parameter
try:
url=urllib.unquote_plus(params["url"])
except:
pass
try:
name=urllib.unquote_plus(params["name"])
except:
pass
try:
mode=int(params["mode"])
except:
pass
print "Mode: "+str(mode)
print "URL: "+str(url)
print "Name: "+str(name)
if mode == None:
if __settings__.getSetting('login') != "" and __settings__.getSetting('password') != "":
getGoogleXml()
if mode == None or mode == 0 or mode == 4 :
if __settings__.getSetting('login') != "" and __settings__.getSetting('password') != "":
googleTags, googleMainTags = returnGoogle()
#LAUNCH LIST
#Default
if mode == None or mode == 0:
print "CATEGORY INDEX : "
launchUrl()
#XML BOOKMARKS
writeXml(os.path.join(xmlUserDir, "default.xml"))
getXmlFav("default.xml")
if __settings__.getSetting('login') != "" and __settings__.getSetting('password') != "":
#GOOGLE BOOKMARKS
for googleBookmark in googleMainTags :
#print googleBookmark
imgPath = checkThumb(googleBookmark["url"].encode("utf-8"),name=googleBookmark["guid"])
thumbnail = os.path.join(imgUserDir, imgPath)
addLink(googleBookmark["title"].encode("utf-8"),googleBookmark["url"].encode("utf-8"),thumbnail, imgPath, readonly = True)
for googleTag in googleTags.keys() :
addDir(googleTag.encode("utf-8"),"none",4,defaultGoogleFolderIcon)
u=sys.argv[0]+"?mode=5"
ok=True
liz=xbmcgui.ListItem(__language__(12), iconImage=defaultAddIcon, thumbnailImage=defaultAddIcon)
liz.setInfo( type="files", infoLabels={ "Title": __language__(12) } )
ok=xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=liz)
#Lister les favoris
elif mode == 1:
print "GET INDEX OF PAGE : " + url
#Lancer un favoris
elif mode == 2:
print "LAUNCH URL: " + url
launchBrowser(url)
#Ouvrir une URL
elif mode == 3:
print "ASK URL: "
url = show_keyboard ("http://", __language__(101))
launchBrowser(url)
#Afficher les favoris du tag google
elif mode == 4:
print "SHOW GOOGLE TAGS BOOKMARKS: "
#print googleTags
for googleBookmark in googleTags[name.decode("utf-8")] :
imgPath = checkThumb(googleBookmark["url"].encode("utf-8"),name=googleBookmark["guid"])
thumbnail = os.path.join(imgUserDir, imgPath)
addLink(googleBookmark["title"].encode("utf-8"),googleBookmark["url"].encode("utf-8"),thumbnail, imgPath ,readonly = True)
#Ajouter un favoris
elif mode == 5:
print "ADD A BOOKMARK: "
addBookmark()
elif mode == 6 :
print "ADD A GOOGLE BOOKMARK: "
addBookmark(discuss = False, titleStr = name, urlStr = url)
elif mode == 7 :
print "REFRESH A THUMB: "
getThumb(url,name)
dialog.ok(__language__(108) , __language__(106))
elif mode == 8 :
print "DELETE A BOOKMARK: "
removeXml(name, url)
xbmc.executebuiltin("Container.Refresh")
xbmcplugin.endOfDirectory(int(sys.argv[1]))