Advertisement
kamegami

Chrome Launcher XBMC Fix

Dec 30th, 2014
533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.92 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import urllib
  4. import sys
  5. import re
  6. import os
  7. import subprocess
  8. import xbmcplugin
  9. import xbmcgui
  10. import xbmcaddon
  11.  
  12.  
  13. addon = xbmcaddon.Addon()
  14. pluginhandle = int(sys.argv[1])
  15. addonID = addon.getAddonInfo('id')
  16. addonPath = addon.getAddonInfo('path')
  17. translation = addon.getLocalizedString
  18. osWin = xbmc.getCondVisibility('system.platform.windows')
  19. osOsx = xbmc.getCondVisibility('system.platform.osx')
  20. osLinux = xbmc.getCondVisibility('system.platform.linux')
  21. useOwnProfile = addon.getSetting("useOwnProfile") == "true"
  22. useCustomPath = addon.getSetting("useCustomPath") == "true"
  23. customPath = xbmc.translatePath(addon.getSetting("customPath"))
  24.  
  25. userDataFolder = xbmc.translatePath("special://profile/addon_data/"+addonID)
  26. profileFolder = os.path.join(userDataFolder, 'profile')
  27. siteFolder = os.path.join(userDataFolder, 'sites')
  28.  
  29. if not os.path.isdir(userDataFolder):
  30.     os.mkdir(userDataFolder)
  31. if not os.path.isdir(profileFolder):
  32.     os.mkdir(profileFolder)
  33. if not os.path.isdir(siteFolder):
  34.     os.mkdir(siteFolder)
  35.  
  36. youtubeUrl = "http://www.youtube.com/leanback"
  37. vimeoUrl = "http://www.vimeo.com/couchmode"
  38.  
  39.  
  40. def index():
  41.     files = os.listdir(siteFolder)
  42.     for file in files:
  43.         if file.endswith(".link"):
  44.             fh = open(os.path.join(siteFolder, file), 'r')
  45.             title = ""
  46.             url = ""
  47.             thumb = ""
  48.             kiosk = "yes"
  49.             stopPlayback = "no"
  50.             for line in fh.readlines():
  51.                 entry = line[:line.find("=")]
  52.                 content = line[line.find("=")+1:]
  53.                 if entry == "title":
  54.                     title = content.strip()
  55.                 elif entry == "url":
  56.                     url = content.strip()
  57.                 elif entry == "thumb":
  58.                     thumb = content.strip()
  59.                 elif entry == "kiosk":
  60.                     kiosk = content.strip()
  61.                 elif entry == "stopPlayback":
  62.                     stopPlayback = content.strip()
  63.             fh.close()
  64.             addSiteDir(title, url, 'showSite', thumb, stopPlayback, kiosk)
  65.     addDir("[ Vimeo Couchmode ]", vimeoUrl, 'showSite', os.path.join(addonPath, "vimeo.png"), "yes", "yes")
  66.     addDir("[ Youtube Leanback ]", youtubeUrl, 'showSite', os.path.join(addonPath, "youtube.png"), "yes", "yes")
  67.     addDir("[B]- "+translation(30001)+"[/B]", "", 'addSite', "")
  68.     xbmcplugin.endOfDirectory(pluginhandle)
  69.  
  70.  
  71. def addSite(site="", title=""):
  72.     if site:
  73.         filename = getFileName(title)
  74.         content = "title="+title+"\nurl="+site+"\nthumb=DefaultFolder.png\nstopPlayback=no\nkiosk=yes"
  75.         fh = open(os.path.join(siteFolder, filename+".link"), 'w')
  76.         fh.write(content)
  77.         fh.close()
  78.     else:
  79.         keyboard = xbmc.Keyboard('', translation(30003))
  80.         keyboard.doModal()
  81.         if keyboard.isConfirmed() and keyboard.getText():
  82.             title = keyboard.getText()
  83.             keyboard = xbmc.Keyboard('http://', translation(30004))
  84.             keyboard.doModal()
  85.             if keyboard.isConfirmed() and keyboard.getText():
  86.                 url = keyboard.getText()
  87.                 keyboard = xbmc.Keyboard('no', translation(30009))
  88.                 keyboard.doModal()
  89.                 if keyboard.isConfirmed() and keyboard.getText():
  90.                     stopPlayback = keyboard.getText()
  91.                     keyboard = xbmc.Keyboard('yes', translation(30016))
  92.                     keyboard.doModal()
  93.                     if keyboard.isConfirmed() and keyboard.getText():
  94.                         kiosk = keyboard.getText()
  95.                         content = "title="+title+"\nurl="+url+"\nthumb=DefaultFolder.png\nstopPlayback="+stopPlayback+"\nkiosk="+kiosk
  96.                         fh = open(os.path.join(siteFolder, getFileName(title)+".link"), 'w')
  97.                         fh.write(content)
  98.                         fh.close()
  99.     xbmc.executebuiltin("Container.Refresh")
  100.  
  101.  
  102. def getFileName(title):
  103.     return (''.join(c for c in unicode(title, 'utf-8') if c not in '/\\:?"*|<>')).strip()
  104.  
  105.  
  106. def getFullPath(path, url, useKiosk, userAgent):
  107.     profile = ""
  108.     if useOwnProfile:
  109.         profile = '--user-data-dir="'+profileFolder+'" '
  110.     kiosk = ""
  111.     if useKiosk=="yes":
  112.         kiosk = '--kiosk '
  113.     if userAgent:
  114.         userAgent = '--user-agent="'+userAgent+'" '
  115.     return '"'+path+'" '+profile+userAgent+'--start-maximized --disable-translate --disable-new-tab-first-run --no-default-browser-check --no-first-run '+kiosk+'"'+url+'"'
  116.  
  117.  
  118. def showSite(url, stopPlayback, kiosk, userAgent):
  119.     if stopPlayback == "yes":
  120.         xbmc.Player().stop()
  121.     if osWin:
  122.         path = 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe'
  123.         path64 = 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'
  124.         if useCustomPath and os.path.exists(customPath):
  125.             fullUrl = getFullPath(customPath, url, kiosk, userAgent)
  126.             subprocess.Popen(fullUrl, shell=False, creationflags=0x00000008, close_fds = True)
  127.         elif os.path.exists(path):
  128.             fullUrl = getFullPath(path, url, kiosk, userAgent)
  129.             subprocess.Popen(fullUrl, shell=False, creationflags=0x00000008, close_fds = True)
  130.         elif os.path.exists(path64):
  131.             fullUrl = getFullPath(path64, url, kiosk, userAgent)
  132.             subprocess.Popen(fullUrl, shell=False, creationflags=0x00000008, close_fds = True)
  133.         else:
  134.             xbmc.executebuiltin('XBMC.Notification(Info:,'+str(translation(30005))+'!,5000)')
  135.             addon.openSettings()
  136.     elif osOsx:
  137.         path = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
  138.         if useCustomPath and os.path.exists(customPath):
  139.             fullUrl = getFullPath(customPath, url, kiosk, userAgent)
  140.             subprocess.Popen(fullUrl, shell=True)
  141.         elif os.path.exists(path):
  142.             fullUrl = getFullPath(path, url, kiosk, userAgent)
  143.             subprocess.Popen(fullUrl, shell=True)
  144.         else:
  145.             xbmc.executebuiltin('XBMC.Notification(Info:,'+str(translation(30005))+'!,5000)')
  146.             addon.openSettings()
  147.     elif osLinux:
  148.         path = "/usr/bin/google-chrome"
  149.         if useCustomPath and os.path.exists(customPath):
  150.             fullUrl = getFullPath(customPath, url, kiosk, userAgent)
  151.             subprocess.Popen(fullUrl, shell=True)
  152.         elif os.path.exists(path):
  153.             fullUrl = getFullPath(path, url, kiosk, userAgent)
  154.             subprocess.Popen(fullUrl, shell=True)
  155.         else:
  156.             xbmc.executebuiltin('XBMC.Notification(Info:,'+str(translation(30005))+'!,5000)')
  157.             addon.openSettings()
  158.  
  159.  
  160. def removeSite(title):
  161.     os.remove(os.path.join(siteFolder, getFileName(title)+".link"))
  162.     xbmc.executebuiltin("Container.Refresh")
  163.  
  164.  
  165. def editSite(title):
  166.     filenameOld = getFileName(title)
  167.     file = os.path.join(siteFolder, filenameOld+".link")
  168.     fh = open(file, 'r')
  169.     title = ""
  170.     url = ""
  171.     kiosk = "yes"
  172.     thumb = "DefaultFolder.png"
  173.     stopPlayback = "no"
  174.     for line in fh.readlines():
  175.         entry = line[:line.find("=")]
  176.         content = line[line.find("=")+1:]
  177.         if entry == "title":
  178.             title = content.strip()
  179.         elif entry == "url":
  180.             url = content.strip()
  181.         elif entry == "kiosk":
  182.             kiosk = content.strip()
  183.         elif entry == "thumb":
  184.             thumb = content.strip()
  185.         elif entry == "stopPlayback":
  186.             stopPlayback = content.strip()
  187.     fh.close()
  188.  
  189.     oldTitle = title
  190.     keyboard = xbmc.Keyboard(title, translation(30003))
  191.     keyboard.doModal()
  192.     if keyboard.isConfirmed() and keyboard.getText():
  193.         title = keyboard.getText()
  194.         keyboard = xbmc.Keyboard(url, translation(30004))
  195.         keyboard.doModal()
  196.         if keyboard.isConfirmed() and keyboard.getText():
  197.             url = keyboard.getText()
  198.             keyboard = xbmc.Keyboard(stopPlayback, translation(30009))
  199.             keyboard.doModal()
  200.             if keyboard.isConfirmed() and keyboard.getText():
  201.                 stopPlayback = keyboard.getText()
  202.                 keyboard = xbmc.Keyboard(kiosk, translation(30016))
  203.                 keyboard.doModal()
  204.                 if keyboard.isConfirmed() and keyboard.getText():
  205.                     kiosk = keyboard.getText()
  206.                     content = "title="+title+"\nurl="+url+"\nthumb="+thumb+"\nstopPlayback="+stopPlayback+"\nkiosk="+kiosk
  207.                     fh = open(os.path.join(siteFolder, getFileName(title)+".link"), 'w')
  208.                     fh.write(content)
  209.                     fh.close()
  210.                     if title != oldTitle:
  211.                         os.remove(os.path.join(siteFolder, filenameOld+".link"))
  212.     xbmc.executebuiltin("Container.Refresh")
  213.  
  214.  
  215. def parameters_string_to_dict(parameters):
  216.     paramDict = {}
  217.     if parameters:
  218.         paramPairs = parameters[1:].split("&")
  219.         for paramsPair in paramPairs:
  220.             paramSplits = paramsPair.split('=')
  221.             if (len(paramSplits)) == 2:
  222.                 paramDict[paramSplits[0]] = paramSplits[1]
  223.     return paramDict
  224.  
  225.  
  226. def addDir(name, url, mode, iconimage, stopPlayback="", kiosk=""):
  227.     u = sys.argv[0]+"?url="+urllib.quote_plus(url)+"&mode="+urllib.quote_plus(mode)+"&stopPlayback="+urllib.quote_plus(stopPlayback)+"&kiosk="+urllib.quote_plus(kiosk)
  228.     ok = True
  229.     liz = xbmcgui.ListItem(name, iconImage="DefaultFolder.png", thumbnailImage=iconimage)
  230.     liz.setInfo(type="Video", infoLabels={"Title": name})
  231.     ok = xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]), url=u, listitem=liz, isFolder=True)
  232.     return ok
  233.  
  234.  
  235. def addSiteDir(name, url, mode, iconimage, stopPlayback, kiosk):
  236.     u = sys.argv[0]+"?url="+urllib.quote_plus(url)+"&mode="+urllib.quote_plus(mode)+"&stopPlayback="+urllib.quote_plus(stopPlayback)+"&kiosk="+urllib.quote_plus(kiosk)
  237.     ok = True
  238.     liz = xbmcgui.ListItem(name, iconImage="DefaultFolder.png", thumbnailImage=iconimage)
  239.     liz.setInfo(type="Video", infoLabels={"Title": name})
  240.     liz.addContextMenuItems([(translation(30006), 'RunPlugin(plugin://'+addonID+'/?mode=editSite&url='+urllib.quote_plus(name)+')',), (translation(30002), 'RunPlugin(plugin://'+addonID+'/?mode=removeSite&url='+urllib.quote_plus(name)+')',)])
  241.     ok = xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]), url=u, listitem=liz, isFolder=True)
  242.     return ok
  243.  
  244. params = parameters_string_to_dict(sys.argv[2])
  245. mode = urllib.unquote_plus(params.get('mode', ''))
  246. name = urllib.unquote_plus(params.get('name', ''))
  247. url = urllib.unquote_plus(params.get('url', ''))
  248. stopPlayback = urllib.unquote_plus(params.get('stopPlayback', 'no'))
  249. kiosk = urllib.unquote_plus(params.get('kiosk', 'yes'))
  250. userAgent = urllib.unquote_plus(params.get('userAgent', ''))
  251.  
  252.  
  253. if mode == 'addSite':
  254.     addSite()
  255. elif mode == 'showSite':
  256.     showSite(url, stopPlayback, kiosk, userAgent)
  257. elif mode == 'removeSite':
  258.     removeSite(url)
  259. elif mode == 'editSite':
  260.     editSite(url)
  261. else:
  262.     index()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement