Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- import urllib
- import sys
- import re
- import os
- import subprocess
- import xbmcplugin
- import xbmcgui
- import xbmcaddon
- import signal
- addon = xbmcaddon.Addon()
- pluginhandle = int(sys.argv[1])
- addonID = addon.getAddonInfo('id')
- addonPath = addon.getAddonInfo('path')
- translation = addon.getLocalizedString
- osWin = xbmc.getCondVisibility('system.platform.windows')
- osOsx = xbmc.getCondVisibility('system.platform.osx')
- osLinux = xbmc.getCondVisibility('system.platform.linux')
- useOwnProfile = addon.getSetting("useOwnProfile") == "true"
- useCustomPath = addon.getSetting("useCustomPath") == "true"
- startScriptBefore = addon.getSetting("startScriptBefore") == "true"
- customPath = str(addon.getSetting("customPath"))
- scriptPath = str(addon.getSetting("scriptPath"))
- scriptDelay = int(addon.getSetting("scriptDelay"))
- userDataFolder = xbmc.translatePath("special://profile/addon_data/"+addonID)
- profileFolder = os.path.join(userDataFolder, 'profile')
- siteFolder = os.path.join(userDataFolder, 'sites')
- browserPIDFile = os.path.join(userDataFolder, 'browser.pid')
- browserPID = 0 # we'll use this instead of the file since the file give a "full url"
- browserProcess = None #we'll use this for the process itself. May make working with windows easier
- if not os.path.isdir(userDataFolder):
- os.mkdir(userDataFolder)
- if not os.path.isdir(profileFolder):
- os.mkdir(profileFolder)
- if not os.path.isdir(siteFolder):
- os.mkdir(siteFolder)
- youtubeUrl = "http://www.youtube.com/leanback"
- vimeoUrl = "http://www.vimeo.com/couchmode"
- def index():
- files = os.listdir(siteFolder)
- for file in files:
- if file.endswith(".link"):
- fh = open(os.path.join(siteFolder, file), 'r')
- title = ""
- url = ""
- thumb = ""
- kiosk = "yes"
- stopPlayback = "no"
- for line in fh.readlines():
- entry = line[:line.find("=")]
- content = line[line.find("=")+1:]
- if entry == "title":
- title = content.strip()
- elif entry == "url":
- url = content.strip()
- elif entry == "thumb":
- thumb = content.strip()
- elif entry == "kiosk":
- kiosk = content.strip()
- elif entry == "stopPlayback":
- stopPlayback = content.strip()
- fh.close()
- addSiteDir(title, url, 'showSite', thumb, stopPlayback, kiosk)
- addDir("[ Vimeo Couchmode ]", vimeoUrl, 'showSite', os.path.join(addonPath, "vimeo.png"), "yes", "yes")
- addDir("[ Youtube Leanback ]", youtubeUrl, 'showSite', os.path.join(addonPath, "youtube.png"), "yes", "yes")
- addDir("[B]- "+translation(30001)+"[/B]", "", 'addSite', "")
- xbmcplugin.endOfDirectory(pluginhandle)
- def addSite(site="", title=""):
- if site:
- filename = getFileName(title)
- content = "title="+title+"\nurl="+site+"\nthumb=DefaultFolder.png\nstopPlayback=no\nkiosk=yes"
- fh = open(os.path.join(siteFolder, filename+".link"), 'w')
- fh.write(content)
- fh.close()
- else:
- keyboard = xbmc.Keyboard('', translation(30003))
- keyboard.doModal()
- if keyboard.isConfirmed() and keyboard.getText():
- title = keyboard.getText()
- keyboard = xbmc.Keyboard('http://', translation(30004))
- keyboard.doModal()
- if keyboard.isConfirmed() and keyboard.getText():
- url = keyboard.getText()
- keyboard = xbmc.Keyboard('no', translation(30009))
- keyboard.doModal()
- if keyboard.isConfirmed() and keyboard.getText():
- stopPlayback = keyboard.getText()
- keyboard = xbmc.Keyboard('yes', translation(30016))
- keyboard.doModal()
- if keyboard.isConfirmed() and keyboard.getText():
- kiosk = keyboard.getText()
- content = "title="+title+"\nurl="+url+"\nthumb=DefaultFolder.png\nstopPlayback="+stopPlayback+"\nkiosk="+kiosk
- fh = open(os.path.join(siteFolder, getFileName(title)+".link"), 'w')
- fh.write(content)
- fh.close()
- xbmc.executebuiltin("Container.Refresh")
- def getFileName(title):
- return (''.join(c for c in unicode(title, 'utf-8') if c not in '/\\:?"*|<>')).strip()
- def getFullPath(path, url, useKiosk, userAgent):
- profile = ""
- if useOwnProfile:
- profile = '--user-data-dir="'+profileFolder+'" '
- kiosk = ""
- if useKiosk=="yes":
- kiosk = '--kiosk '
- if userAgent:
- userAgent = '--user-agent="'+userAgent+'" '
- return '"'+path+'" '+profile+userAgent+'--start-maximized --disable-translate --disable-new-tab-first-run --no-default-browser-check --no-first-run '+kiosk+'"'+url+'"'
- def showSite(url, stopPlayback, kiosk, userAgent):
- global browserPID
- global browserProcess
- fullUrl = ""
- if stopPlayback == "yes":
- xbmc.Player().stop()
- if osWin:
- if startScriptBefore and scriptPath:
- subprocess.Popen(scriptPath, shell=False)
- if scriptDelay>0:
- xbmc.sleep(scriptDelay*1000)
- path = 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe'
- path64 = 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'
- if useCustomPath and os.path.exists(customPath):
- fullUrl = getFullPath(customPath, url, kiosk, userAgent)
- browserProcess = subprocess.Popen(fullUrl, shell=False)
- browserPID = browserProcess.pid
- elif os.path.exists(path):
- fullUrl = getFullPath(path, url, kiosk, userAgent)
- browserProcess = subprocess.Popen(fullUrl, shell=False)
- browserPID = browserProcess.pid
- elif os.path.exists(path64):
- fullUrl = getFullPath(path64, url, kiosk, userAgent)
- browserProcess = subprocess.Popen(fullUrl, shell=False)
- browserPID = browserProcess.pid
- else:
- xbmc.executebuiltin('XBMC.Notification(Info:,'+str(translation(30005))+'!,5000)')
- addon.openSettings()
- elif osOsx:
- if startScriptBefore and scriptPath:
- subprocess.Popen(scriptPath, shell=True)
- if scriptDelay>0:
- xbmc.sleep(scriptDelay*1000)
- path = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
- if useCustomPath and os.path.exists(customPath):
- fullUrl = getFullPath(customPath, url, kiosk, userAgent)
- subprocess.Popen(fullUrl, shell=True)
- elif os.path.exists(path):
- fullUrl = getFullPath(path, url, kiosk, userAgent)
- subprocess.Popen(fullUrl, shell=True)
- else:
- xbmc.executebuiltin('XBMC.Notification(Info:,'+str(translation(30005))+'!,5000)')
- addon.openSettings()
- elif osLinux:
- if startScriptBefore and scriptPath:
- subprocess.Popen(scriptPath, shell=True)
- if scriptDelay>0:
- xbmc.sleep(scriptDelay*1000)
- path = "/usr/bin/google-chrome"
- if useCustomPath and os.path.exists(customPath):
- fullUrl = getFullPath(customPath, url, kiosk, userAgent)
- subprocess.Popen(fullUrl, shell=True)
- elif os.path.exists(path):
- fullUrl = getFullPath(path, url, kiosk, userAgent)
- subprocess.Popen(fullUrl, shell=True)
- else:
- xbmc.executebuiltin('XBMC.Notification(Info:,'+str(translation(30005))+'!,5000)')
- addon.openSettings()
- print("fullUrl: %s" % fullUrl)
- if fullUrl != "":
- if os.path.exists(browserPIDFile):
- os.unlink(browserPIDFile)
- fh = open(browserPIDFile, 'w+')
- fh.write(fullUrl)
- fh.close()
- xbmcgui.Dialog().ok(addon.getAddonInfo('name') , translation(30017))
- killBrowser()
- def killBrowser():
- global browserPID
- global browserProcess
- if os.path.exists(browserPIDFile):
- fh = open(browserPIDFile)
- fullUrl = fh.read()
- fh.close()
- if osWin:
- # use pid instead of url for window
- print("Browser PID from global var: %s" % browserPID)
- if browserPID <= 0:
- # WINDOWS VERSION NEEDS TO BE FIXED
- psgrep = 'tasklist | FIND "chrome"'
- processList = subprocess.check_output(psgrep, shell=True)
- strProcessList = str(processList)
- processMatch = re.findall('\d+', strProcessList).pop(0)
- browserPID = int(processMatch)
- browserProcess.terminate()
- browserPID = 0
- elif len(fullUrl) > 0:
- # a full re.escape() is too much
- fullUrlEsc = fullUrl.replace('"','')
- browserPID = 0
- if osWin:
- print("trying to process browser by URL for windows -- BAD")
- if osLinux:
- psgrep = '/bin/ps ax | /bin/grep "'+fullUrlEsc+'"'
- processList = subprocess.check_output( psgrep, shell=True )
- processMatch = re.findall('^\d*', processList).pop(0)
- browserPID = int(processMatch)
- if osOsx:
- psgrep = '/bin/ps ax | /usr/bin/grep "'+fullUrlEsc+'"'
- processList = subprocess.check_output( psgrep, shell=True )
- processMatch = re.findall('^\d*', processList).pop(0)
- browserPID = int(processMatch)
- else:
- browserPID = -1
- if browserPID > 0:
- os.kill(browserPID, signal.SIGTERM)
- os.unlink(browserPIDFile)
- def removeSite(title):
- os.remove(os.path.join(siteFolder, getFileName(title)+".link"))
- xbmc.executebuiltin("Container.Refresh")
- def editSite(title):
- filenameOld = getFileName(title)
- file = os.path.join(siteFolder, filenameOld+".link")
- fh = open(file, 'r')
- title = ""
- url = ""
- kiosk = "yes"
- thumb = "DefaultFolder.png"
- stopPlayback = "no"
- for line in fh.readlines():
- entry = line[:line.find("=")]
- content = line[line.find("=")+1:]
- if entry == "title":
- title = content.strip()
- elif entry == "url":
- url = content.strip()
- elif entry == "kiosk":
- kiosk = content.strip()
- elif entry == "thumb":
- thumb = content.strip()
- elif entry == "stopPlayback":
- stopPlayback = content.strip()
- fh.close()
- oldTitle = title
- keyboard = xbmc.Keyboard(title, translation(30003))
- keyboard.doModal()
- if keyboard.isConfirmed() and keyboard.getText():
- title = keyboard.getText()
- keyboard = xbmc.Keyboard(url, translation(30004))
- keyboard.doModal()
- if keyboard.isConfirmed() and keyboard.getText():
- url = keyboard.getText()
- keyboard = xbmc.Keyboard(stopPlayback, translation(30009))
- keyboard.doModal()
- if keyboard.isConfirmed() and keyboard.getText():
- stopPlayback = keyboard.getText()
- keyboard = xbmc.Keyboard(kiosk, translation(30016))
- keyboard.doModal()
- if keyboard.isConfirmed() and keyboard.getText():
- kiosk = keyboard.getText()
- content = "title="+title+"\nurl="+url+"\nthumb="+thumb+"\nstopPlayback="+stopPlayback+"\nkiosk="+kiosk
- fh = open(os.path.join(siteFolder, getFileName(title)+".link"), 'w')
- fh.write(content)
- fh.close()
- if title != oldTitle:
- os.remove(os.path.join(siteFolder, filenameOld+".link"))
- xbmc.executebuiltin("Container.Refresh")
- def parameters_string_to_dict(parameters):
- paramDict = {}
- if parameters:
- paramPairs = parameters[1:].split("&")
- for paramsPair in paramPairs:
- paramSplits = paramsPair.split('=')
- if (len(paramSplits)) == 2:
- paramDict[paramSplits[0]] = paramSplits[1]
- return paramDict
- def addDir(name, url, mode, iconimage, stopPlayback="", kiosk=""):
- u = sys.argv[0]+"?url="+urllib.quote_plus(url)+"&mode="+urllib.quote_plus(mode)+"&stopPlayback="+urllib.quote_plus(stopPlayback)+"&kiosk="+urllib.quote_plus(kiosk)
- ok = True
- liz = xbmcgui.ListItem(name, iconImage="DefaultFolder.png", 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 addSiteDir(name, url, mode, iconimage, stopPlayback, kiosk):
- u = sys.argv[0]+"?url="+urllib.quote_plus(url)+"&mode="+urllib.quote_plus(mode)+"&stopPlayback="+urllib.quote_plus(stopPlayback)+"&kiosk="+urllib.quote_plus(kiosk)
- ok = True
- liz = xbmcgui.ListItem(name, iconImage="DefaultFolder.png", thumbnailImage=iconimage)
- liz.setInfo(type="Video", infoLabels={"Title": name})
- 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)+')',)])
- ok = xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]), url=u, listitem=liz, isFolder=True)
- return ok
- params = parameters_string_to_dict(sys.argv[2])
- mode = urllib.unquote_plus(params.get('mode', ''))
- name = urllib.unquote_plus(params.get('name', ''))
- url = urllib.unquote_plus(params.get('url', ''))
- stopPlayback = urllib.unquote_plus(params.get('stopPlayback', 'no'))
- kiosk = urllib.unquote_plus(params.get('kiosk', 'yes'))
- userAgent = urllib.unquote_plus(params.get('userAgent', ''))
- if mode == 'addSite':
- addSite()
- elif mode == 'showSite':
- showSite(url, stopPlayback, kiosk, userAgent)
- elif mode == 'removeSite':
- removeSite(url)
- elif mode == 'editSite':
- editSite(url)
- else:
- index()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement