Advertisement
Fyrilin

XBMC Amazon Prime + Chrome Launcher windows close fix

Mar 1st, 2014
1,136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 14.04 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. import signal
  12.  
  13.  
  14. addon = xbmcaddon.Addon()
  15. pluginhandle = int(sys.argv[1])
  16. addonID = addon.getAddonInfo('id')
  17. addonPath = addon.getAddonInfo('path')
  18. translation = addon.getLocalizedString
  19. osWin = xbmc.getCondVisibility('system.platform.windows')
  20. osOsx = xbmc.getCondVisibility('system.platform.osx')
  21. osLinux = xbmc.getCondVisibility('system.platform.linux')
  22. useOwnProfile = addon.getSetting("useOwnProfile") == "true"
  23. useCustomPath = addon.getSetting("useCustomPath") == "true"
  24. startScriptBefore = addon.getSetting("startScriptBefore") == "true"
  25. customPath = str(addon.getSetting("customPath"))
  26. scriptPath = str(addon.getSetting("scriptPath"))
  27. scriptDelay = int(addon.getSetting("scriptDelay"))
  28.  
  29. userDataFolder = xbmc.translatePath("special://profile/addon_data/"+addonID)
  30. profileFolder = os.path.join(userDataFolder, 'profile')
  31. siteFolder = os.path.join(userDataFolder, 'sites')
  32. browserPIDFile = os.path.join(userDataFolder, 'browser.pid')
  33. browserPID = 0 # we'll use this instead of the file since the file give a "full url"
  34. browserProcess = None #we'll use this for the process itself.  May make working with windows easier
  35.  
  36. if not os.path.isdir(userDataFolder):
  37.     os.mkdir(userDataFolder)
  38. if not os.path.isdir(profileFolder):
  39.     os.mkdir(profileFolder)
  40. if not os.path.isdir(siteFolder):
  41.     os.mkdir(siteFolder)
  42.  
  43. youtubeUrl = "http://www.youtube.com/leanback"
  44. vimeoUrl = "http://www.vimeo.com/couchmode"
  45.  
  46.  
  47. def index():
  48.     files = os.listdir(siteFolder)
  49.     for file in files:
  50.         if file.endswith(".link"):
  51.             fh = open(os.path.join(siteFolder, file), 'r')
  52.             title = ""
  53.             url = ""
  54.             thumb = ""
  55.             kiosk = "yes"
  56.             stopPlayback = "no"
  57.             for line in fh.readlines():
  58.                 entry = line[:line.find("=")]
  59.                 content = line[line.find("=")+1:]
  60.                 if entry == "title":
  61.                     title = content.strip()
  62.                 elif entry == "url":
  63.                     url = content.strip()
  64.                 elif entry == "thumb":
  65.                     thumb = content.strip()
  66.                 elif entry == "kiosk":
  67.                     kiosk = content.strip()
  68.                 elif entry == "stopPlayback":
  69.                     stopPlayback = content.strip()
  70.             fh.close()
  71.             addSiteDir(title, url, 'showSite', thumb, stopPlayback, kiosk)
  72.     addDir("[ Vimeo Couchmode ]", vimeoUrl, 'showSite', os.path.join(addonPath, "vimeo.png"), "yes", "yes")
  73.     addDir("[ Youtube Leanback ]", youtubeUrl, 'showSite', os.path.join(addonPath, "youtube.png"), "yes", "yes")
  74.     addDir("[B]- "+translation(30001)+"[/B]", "", 'addSite', "")
  75.     xbmcplugin.endOfDirectory(pluginhandle)
  76.  
  77.  
  78. def addSite(site="", title=""):
  79.     if site:
  80.         filename = getFileName(title)
  81.         content = "title="+title+"\nurl="+site+"\nthumb=DefaultFolder.png\nstopPlayback=no\nkiosk=yes"
  82.         fh = open(os.path.join(siteFolder, filename+".link"), 'w')
  83.         fh.write(content)
  84.         fh.close()
  85.     else:
  86.         keyboard = xbmc.Keyboard('', translation(30003))
  87.         keyboard.doModal()
  88.         if keyboard.isConfirmed() and keyboard.getText():
  89.             title = keyboard.getText()
  90.             keyboard = xbmc.Keyboard('http://', translation(30004))
  91.             keyboard.doModal()
  92.             if keyboard.isConfirmed() and keyboard.getText():
  93.                 url = keyboard.getText()
  94.                 keyboard = xbmc.Keyboard('no', translation(30009))
  95.                 keyboard.doModal()
  96.                 if keyboard.isConfirmed() and keyboard.getText():
  97.                     stopPlayback = keyboard.getText()
  98.                     keyboard = xbmc.Keyboard('yes', translation(30016))
  99.                     keyboard.doModal()
  100.                     if keyboard.isConfirmed() and keyboard.getText():
  101.                         kiosk = keyboard.getText()
  102.                         content = "title="+title+"\nurl="+url+"\nthumb=DefaultFolder.png\nstopPlayback="+stopPlayback+"\nkiosk="+kiosk
  103.                         fh = open(os.path.join(siteFolder, getFileName(title)+".link"), 'w')
  104.                         fh.write(content)
  105.                         fh.close()
  106.     xbmc.executebuiltin("Container.Refresh")
  107.  
  108.  
  109. def getFileName(title):
  110.     return (''.join(c for c in unicode(title, 'utf-8') if c not in '/\\:?"*|<>')).strip()
  111.  
  112.  
  113. def getFullPath(path, url, useKiosk, userAgent):
  114.     profile = ""
  115.     if useOwnProfile:
  116.         profile = '--user-data-dir="'+profileFolder+'" '
  117.     kiosk = ""
  118.     if useKiosk=="yes":
  119.         kiosk = '--kiosk '
  120.     if userAgent:
  121.         userAgent = '--user-agent="'+userAgent+'" '
  122.     return '"'+path+'" '+profile+userAgent+'--start-maximized --disable-translate --disable-new-tab-first-run --no-default-browser-check --no-first-run '+kiosk+'"'+url+'"'
  123.  
  124.  
  125. def showSite(url, stopPlayback, kiosk, userAgent):
  126.     global browserPID
  127.     global browserProcess
  128.     fullUrl = ""
  129.     if stopPlayback == "yes":
  130.         xbmc.Player().stop()
  131.     if osWin:
  132.         if startScriptBefore and scriptPath:
  133.             subprocess.Popen(scriptPath, shell=False)
  134.             if scriptDelay>0:
  135.                 xbmc.sleep(scriptDelay*1000)
  136.         path = 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe'
  137.         path64 = 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'
  138.         if useCustomPath and os.path.exists(customPath):
  139.             fullUrl = getFullPath(customPath, url, kiosk, userAgent)
  140.             browserProcess = subprocess.Popen(fullUrl, shell=False)
  141.             browserPID = browserProcess.pid
  142.         elif os.path.exists(path):
  143.             fullUrl = getFullPath(path, url, kiosk, userAgent)
  144.             browserProcess = subprocess.Popen(fullUrl, shell=False)
  145.             browserPID = browserProcess.pid
  146.         elif os.path.exists(path64):
  147.             fullUrl = getFullPath(path64, url, kiosk, userAgent)
  148.             browserProcess = subprocess.Popen(fullUrl, shell=False)
  149.             browserPID = browserProcess.pid
  150.         else:
  151.             xbmc.executebuiltin('XBMC.Notification(Info:,'+str(translation(30005))+'!,5000)')
  152.             addon.openSettings()
  153.     elif osOsx:
  154.         if startScriptBefore and scriptPath:
  155.             subprocess.Popen(scriptPath, shell=True)
  156.             if scriptDelay>0:
  157.                 xbmc.sleep(scriptDelay*1000)
  158.         path = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
  159.         if useCustomPath and os.path.exists(customPath):
  160.             fullUrl = getFullPath(customPath, url, kiosk, userAgent)
  161.             subprocess.Popen(fullUrl, shell=True)
  162.         elif os.path.exists(path):
  163.             fullUrl = getFullPath(path, url, kiosk, userAgent)
  164.             subprocess.Popen(fullUrl, shell=True)
  165.         else:
  166.             xbmc.executebuiltin('XBMC.Notification(Info:,'+str(translation(30005))+'!,5000)')
  167.             addon.openSettings()
  168.     elif osLinux:
  169.         if startScriptBefore and scriptPath:
  170.             subprocess.Popen(scriptPath, shell=True)
  171.             if scriptDelay>0:
  172.                 xbmc.sleep(scriptDelay*1000)
  173.         path = "/usr/bin/google-chrome"
  174.         if useCustomPath and os.path.exists(customPath):
  175.             fullUrl = getFullPath(customPath, url, kiosk, userAgent)
  176.             subprocess.Popen(fullUrl, shell=True)
  177.         elif os.path.exists(path):
  178.             fullUrl = getFullPath(path, url, kiosk, userAgent)
  179.             subprocess.Popen(fullUrl, shell=True)
  180.         else:
  181.             xbmc.executebuiltin('XBMC.Notification(Info:,'+str(translation(30005))+'!,5000)')
  182.             addon.openSettings()
  183.     print("fullUrl: %s" % fullUrl)
  184.  
  185.     if fullUrl != "":
  186.         if os.path.exists(browserPIDFile):
  187.             os.unlink(browserPIDFile)
  188.         fh = open(browserPIDFile, 'w+')
  189.         fh.write(fullUrl)
  190.         fh.close()
  191.  
  192.     xbmcgui.Dialog().ok(addon.getAddonInfo('name') , translation(30017))
  193.     killBrowser()
  194.  
  195.  
  196. def killBrowser():
  197.     global browserPID
  198.     global browserProcess
  199.     if os.path.exists(browserPIDFile):
  200.         fh = open(browserPIDFile)
  201.         fullUrl = fh.read()
  202.         fh.close()
  203.         if osWin:
  204.             # use pid instead of url for window
  205.             print("Browser PID from global var: %s" % browserPID)
  206.             if browserPID <= 0:
  207.                 # WINDOWS VERSION NEEDS TO BE FIXED
  208.                 psgrep = 'tasklist | FIND "chrome"'
  209.                 processList = subprocess.check_output(psgrep, shell=True)
  210.                 strProcessList = str(processList)
  211.                 processMatch = re.findall('\d+', strProcessList).pop(0)
  212.                 browserPID = int(processMatch)
  213.             browserProcess.terminate()
  214.             browserPID = 0
  215.         elif len(fullUrl) > 0:
  216.             # a full re.escape() is too much
  217.             fullUrlEsc = fullUrl.replace('"','')
  218.             browserPID = 0
  219.             if osWin:
  220.                 print("trying to process browser by URL for windows -- BAD")
  221.             if osLinux:
  222.                 psgrep = '/bin/ps ax | /bin/grep "'+fullUrlEsc+'"'
  223.                 processList = subprocess.check_output( psgrep, shell=True )
  224.                 processMatch = re.findall('^\d*', processList).pop(0)
  225.                 browserPID = int(processMatch)
  226.             if osOsx:
  227.                 psgrep = '/bin/ps ax | /usr/bin/grep "'+fullUrlEsc+'"'
  228.                 processList = subprocess.check_output( psgrep, shell=True )
  229.                 processMatch = re.findall('^\d*', processList).pop(0)
  230.                 browserPID = int(processMatch)
  231.         else:
  232.             browserPID = -1
  233.         if browserPID > 0:
  234.             os.kill(browserPID, signal.SIGTERM)
  235.             os.unlink(browserPIDFile)
  236.  
  237.  
  238. def removeSite(title):
  239.     os.remove(os.path.join(siteFolder, getFileName(title)+".link"))
  240.     xbmc.executebuiltin("Container.Refresh")
  241.  
  242.  
  243. def editSite(title):
  244.     filenameOld = getFileName(title)
  245.     file = os.path.join(siteFolder, filenameOld+".link")
  246.     fh = open(file, 'r')
  247.     title = ""
  248.     url = ""
  249.     kiosk = "yes"
  250.     thumb = "DefaultFolder.png"
  251.     stopPlayback = "no"
  252.     for line in fh.readlines():
  253.         entry = line[:line.find("=")]
  254.         content = line[line.find("=")+1:]
  255.         if entry == "title":
  256.             title = content.strip()
  257.         elif entry == "url":
  258.             url = content.strip()
  259.         elif entry == "kiosk":
  260.             kiosk = content.strip()
  261.         elif entry == "thumb":
  262.             thumb = content.strip()
  263.         elif entry == "stopPlayback":
  264.             stopPlayback = content.strip()
  265.     fh.close()
  266.  
  267.     oldTitle = title
  268.     keyboard = xbmc.Keyboard(title, translation(30003))
  269.     keyboard.doModal()
  270.     if keyboard.isConfirmed() and keyboard.getText():
  271.         title = keyboard.getText()
  272.         keyboard = xbmc.Keyboard(url, translation(30004))
  273.         keyboard.doModal()
  274.         if keyboard.isConfirmed() and keyboard.getText():
  275.             url = keyboard.getText()
  276.             keyboard = xbmc.Keyboard(stopPlayback, translation(30009))
  277.             keyboard.doModal()
  278.             if keyboard.isConfirmed() and keyboard.getText():
  279.                 stopPlayback = keyboard.getText()
  280.                 keyboard = xbmc.Keyboard(kiosk, translation(30016))
  281.                 keyboard.doModal()
  282.                 if keyboard.isConfirmed() and keyboard.getText():
  283.                     kiosk = keyboard.getText()
  284.                     content = "title="+title+"\nurl="+url+"\nthumb="+thumb+"\nstopPlayback="+stopPlayback+"\nkiosk="+kiosk
  285.                     fh = open(os.path.join(siteFolder, getFileName(title)+".link"), 'w')
  286.                     fh.write(content)
  287.                     fh.close()
  288.                     if title != oldTitle:
  289.                         os.remove(os.path.join(siteFolder, filenameOld+".link"))
  290.     xbmc.executebuiltin("Container.Refresh")
  291.  
  292.  
  293. def parameters_string_to_dict(parameters):
  294.     paramDict = {}
  295.     if parameters:
  296.         paramPairs = parameters[1:].split("&")
  297.         for paramsPair in paramPairs:
  298.             paramSplits = paramsPair.split('=')
  299.             if (len(paramSplits)) == 2:
  300.                 paramDict[paramSplits[0]] = paramSplits[1]
  301.     return paramDict
  302.  
  303.  
  304. def addDir(name, url, mode, iconimage, stopPlayback="", kiosk=""):
  305.     u = sys.argv[0]+"?url="+urllib.quote_plus(url)+"&mode="+urllib.quote_plus(mode)+"&stopPlayback="+urllib.quote_plus(stopPlayback)+"&kiosk="+urllib.quote_plus(kiosk)
  306.     ok = True
  307.     liz = xbmcgui.ListItem(name, iconImage="DefaultFolder.png", thumbnailImage=iconimage)
  308.     liz.setInfo(type="Video", infoLabels={"Title": name})
  309.     ok = xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]), url=u, listitem=liz, isFolder=True)
  310.     return ok
  311.  
  312.  
  313. def addSiteDir(name, url, mode, iconimage, stopPlayback, kiosk):
  314.     u = sys.argv[0]+"?url="+urllib.quote_plus(url)+"&mode="+urllib.quote_plus(mode)+"&stopPlayback="+urllib.quote_plus(stopPlayback)+"&kiosk="+urllib.quote_plus(kiosk)
  315.     ok = True
  316.     liz = xbmcgui.ListItem(name, iconImage="DefaultFolder.png", thumbnailImage=iconimage)
  317.     liz.setInfo(type="Video", infoLabels={"Title": name})
  318.     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)+')',)])
  319.     ok = xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]), url=u, listitem=liz, isFolder=True)
  320.     return ok
  321.  
  322. params = parameters_string_to_dict(sys.argv[2])
  323. mode = urllib.unquote_plus(params.get('mode', ''))
  324. name = urllib.unquote_plus(params.get('name', ''))
  325. url = urllib.unquote_plus(params.get('url', ''))
  326. stopPlayback = urllib.unquote_plus(params.get('stopPlayback', 'no'))
  327. kiosk = urllib.unquote_plus(params.get('kiosk', 'yes'))
  328. userAgent = urllib.unquote_plus(params.get('userAgent', ''))
  329.  
  330.  
  331. if mode == 'addSite':
  332.     addSite()
  333. elif mode == 'showSite':
  334.     showSite(url, stopPlayback, kiosk, userAgent)
  335. elif mode == 'removeSite':
  336.     removeSite(url)
  337. elif mode == 'editSite':
  338.     editSite(url)
  339. else:
  340.     index()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement