Advertisement
Guest User

Untitled

a guest
Dec 11th, 2013
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.92 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import xbmcaddon,xbmcplugin,xbmcgui,sys,urllib,urllib2,re,socket
  4. from bs4 import BeautifulSoup
  5.  
  6.  
  7. pluginhandle = int(sys.argv[1])
  8. xbox = xbmc.getCondVisibility("System.Platform.xbox")
  9. settings = xbmcaddon.Addon(id='plugin.video.gronkh_de')
  10. translation = settings.getLocalizedString
  11.  
  12. forceViewMode=settings.getSetting("forceViewMode")
  13. if forceViewMode=="true":
  14.   forceViewMode=True
  15. else:
  16.   forceViewMode=False
  17. viewMode=str(settings.getSetting("viewMode"))
  18.  
  19. def index():
  20.         addDir(translation(30001),"http://gronkh.de/","listVideos","")
  21.         addDir(translation(30002),"http://gronkh.de/lets-play","listGames","")
  22.         xbmcplugin.endOfDirectory(pluginhandle)
  23.         if forceViewMode==True:
  24.           xbmc.executebuiltin('Container.SetViewMode('+viewMode+')')
  25.  
  26. def listVideos(url):
  27.         content = getUrl(url)
  28.     soup = BeautifulSoup(content, 'lxml')
  29.     results = soup.body.findAll( 'div', attrs={ 'class':'entry entry-letsplay' })
  30.  
  31.     #Nächste Seite
  32.     nextbar = soup.body.find( 'div', attrs={ 'class':'navigation' })
  33.     nextlink = nextbar.find('span', attrs={'class':'page current'}).parent.next_sibling.a.get('href')
  34.     #print nextlink
  35.  
  36.  
  37.     for i in range(1,len(results),1):
  38.       result = results[i]
  39.      
  40.       linkresult = result.find( 'a', attrs={ 'class':'thumb' })
  41.       textresult = result.find( 'div', attrs={ 'class':'text' })
  42.      
  43.       #Folgen Link
  44.       url = linkresult.get('href')
  45.       #print url
  46.       #Thumbnail   
  47.       #print preresult.get('img src')
  48.       thumb = ''
  49.       #Serien Titel
  50.       title = textresult.h1.a.get('title')
  51.       #print title
  52.       #Folgen Titel
  53.       title2 = textresult.h2.a.string
  54.       #print title2          
  55.       title = title + ' - ' + title2
  56.       #print title
  57.           #addLink(title,url,'playVideo',thumb)
  58.  
  59.           addLink(title,url,'playVideo',thumb)
  60.  
  61.         if len(nextlink)>0:
  62.           addDir(translation(30003),nextlink,"listVideos","")
  63.         xbmcplugin.endOfDirectory(pluginhandle)
  64.         if forceViewMode==True:
  65.           xbmc.executebuiltin('Container.SetViewMode('+viewMode+')')
  66.  
  67. def listGames():
  68.         content = getUrl("http://gronkh.de/lets-play")
  69.         content = content[content.find('<div class="postpadding">'):]
  70.         content = content[:content.find('</div>')]
  71.         spl=content.split('<a href=')
  72.         for i in range(1,len(spl),1):
  73.           entry=spl[i]
  74.           match=re.compile('"(.+?)"', re.DOTALL).findall(entry)
  75.           url="http://gronkh.de"+match[0]
  76.           match=re.compile('title="(.+?)"', re.DOTALL).findall(entry)
  77.           title=match[0]
  78.           match=re.compile('<img src="(.+?)"', re.DOTALL).findall(entry)
  79.           thumb=match[0]
  80.           title=cleanTitle(title)
  81.           addDir(title,url,'listVideos',thumb)
  82.         xbmcplugin.endOfDirectory(pluginhandle)
  83.         if forceViewMode==True:
  84.           xbmc.executebuiltin('Container.SetViewMode('+viewMode+')')
  85.  
  86. def playVideo(url):
  87.         content = getUrl(url)
  88.         match=re.compile('src="http://www.youtube.com/embed/(.+?)\\?', re.DOTALL).findall(content)
  89.         youtubeID=match[0]
  90.         if xbox==True:
  91.           fullData = "plugin://video/YouTube/?path=/root/video&action=play_video&videoid=" + youtubeID
  92.         else:
  93.           fullData = "plugin://plugin.video.youtube/?path=/root/video&action=play_video&videoid=" + youtubeID
  94.         listitem = xbmcgui.ListItem(path=fullData)
  95.         return xbmcplugin.setResolvedUrl(pluginhandle, True, listitem)
  96.  
  97. def getUrl(url):
  98.         req = urllib2.Request(url)
  99.         req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0')
  100.         if xbox==True:
  101.           socket.setdefaulttimeout(30)
  102.           response = urllib2.urlopen(req)
  103.         else:
  104.           response = urllib2.urlopen(req,timeout=30)
  105.         link=response.read()
  106.         response.close()
  107.         return link
  108.  
  109. def cleanTitle(title):
  110.         title=title.replace("&lt;","<").replace("&gt;",">").replace("&amp;","&").replace("&#039;","\\").replace("&quot;","\"").replace("&szlig;","ß").replace("&ndash;","-")
  111.         title=title.replace("&#038;","&").replace("&#8230;","...").replace("&#8211;","-").replace("&#8220;","-").replace("&#8221;","-").replace("&#8217;","'")
  112.         title=title.replace("&Auml;","Ä").replace("&Uuml;","Ü").replace("&Ouml;","Ö").replace("&auml;","ä").replace("&uuml;","ü").replace("&ouml;","ö")
  113.         title=title.strip()
  114.         return title
  115.  
  116. def addLink(name,url,mode,iconimage):
  117.         u=sys.argv[0]+"?url="+urllib.quote_plus(url)+"&mode="+str(mode)
  118.         ok=True
  119.         liz=xbmcgui.ListItem(name, iconImage="DefaultVideo.png", thumbnailImage=iconimage)
  120.         liz.setInfo( type="Video", infoLabels={ "Title": name } )
  121.         liz.setProperty('IsPlayable', 'true')
  122.         ok=xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=liz)
  123.         return ok
  124.  
  125. def addDir(name,url,mode,iconimage):
  126.         u=sys.argv[0]+"?url="+urllib.quote_plus(url)+"&mode="+str(mode)
  127.         ok=True
  128.         liz=xbmcgui.ListItem(name, iconImage="DefaultFolder.png", thumbnailImage=iconimage)
  129.         liz.setInfo( type="Video", infoLabels={ "Title": name } )
  130.         ok=xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=liz,isFolder=True)
  131.         return ok
  132.  
  133. def parameters_string_to_dict(parameters):
  134.     ''' Convert parameters encoded in a URL to a dict. '''
  135.     paramDict = {}
  136.     if parameters:
  137.         paramPairs = parameters[1:].split("&")
  138.         for paramsPair in paramPairs:
  139.             paramSplits = paramsPair.split('=')
  140.             if (len(paramSplits)) == 2:
  141.                 paramDict[paramSplits[0]] = paramSplits[1]
  142.     return paramDict
  143.  
  144. params=parameters_string_to_dict(sys.argv[2])
  145. mode=params.get('mode')
  146. url=params.get('url')
  147. if type(url)==type(str()):
  148.   url=urllib.unquote_plus(url)
  149.  
  150. if mode == 'listGames':
  151.     listGames()
  152. elif mode == 'listVideos':
  153.     listVideos(url)
  154. elif mode == 'playVideo':
  155.     playVideo(url)
  156. else:
  157.     index()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement