Advertisement
Guest User

Untitled

a guest
May 7th, 2014
549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.42 KB | None | 0 0
  1. import xbmcplugin
  2. import xbmcgui
  3. import sys
  4. import urllib, urllib2
  5. import time
  6. import re
  7. import random
  8. from htmlentitydefs import name2codepoint as n2cp
  9.  
  10. thisPlugin = int(sys.argv[1])
  11.  
  12. urlHost = "http://rtl-now.rtl.de"
  13. ajaxUrl = "/xajaxuri.php"
  14. # -----regexContent-------
  15. # [0] is url (/foo.php)
  16. # [1] is name (foo is bar)
  17. # ------------------------
  18. # -----regexSeries--------
  19. # [0] is class even or odd
  20. # [1] is url to videourl
  21. # [2] is title
  22. # [3] is time
  23. # All content is in!
  24. # the free-content will be filtered in code (paytype)
  25.  
  26. n_regexContent = '<div class="m03medium".*?<div class="m03img">.*?<img.*?src="([^"]+)">.*?<div class="m03text"[^>]*?>.*?<span class="m03date">(FREE|PAY|PRE-TV).*?<h2>(.*?)</h2>(.*?)</div>.*?<div class="m03link">.*?href="([^"]+)" target="_self"([^>]?)>.*?</div>'
  27. regexContent = '<div class="seriennavi_free" style=""><a href="(.*?)".*?>FREE.*?</div>.*?<div style="" class="seriennavi_link">.*?">(.*?)</a>.*?</div>'
  28. regexSeries = '<div class="line (even|odd) "><div onclick="link\(\'(.*?)\'\); return false;".*?<a href=".*?" title=".*?">(.*?)</a>.*?class="time">.*?<div.*?</div>(.*?)</div>.*?class="minibutton">(.*?)</a></div></div>'
  29. n_regexMeta = '<meta property="og:description" content="([^"]+)">.*?<meta property="og:image" content="([^"]+)">'
  30. regexVideoData = "data:'(.*?)'"
  31. regexF4mVideo = "http://.*?videos/(.*?).f4m\?ts.*?"
  32. regexBiggerPic = "(.*?)(\d{3}x\d{3})([^\d]{1})(.*)"
  33. regexXML = '<filename.*?><!\[CDATA\[(.*?)\]\]></filename>'
  34. regexTextOnly = '<\s*\/?\s*\s*.*?>'
  35. regexTabVars = '<select\s*?onchange.*?xajax_show_top_and_movies.*?\'(.*?)\'.*?\'(.*?)\'.*?\'(.*?)\'.*?\'(.*?)\'.*?\'(.*?)\'.*?>(.*?)</select>'
  36. regexTabEntry = '<option.*?value=\'(\d)\'.*?>'
  37. # ------------------------
  38.  
  39. newRegexContent = '<div class="m03medium".*?</h4>'
  40. newLinkRegexContent = '<a href="([^"]+?)"'
  41. newImgRegexContent = '<img.+?src="([^"]+?)">'
  42. newFreeRegexContent = 'FREE'
  43. newTitleRegexContent = '<h4>([^<]+?)</h4>'
  44.  
  45. newsRegexContent = '<div id="navi_.*?</a>\s*</div>\s*</div>'
  46. newsLinkRegexContent = 'href="([^"]+?)"'
  47. newsFreeRegexContent = 'FREE'
  48. newsTitleRegexContent = '<a class[^<]+?>([^<]+?)</a>'
  49.  
  50.  
  51. def showGenre():
  52.     global thisPlugin
  53.     addDirectoryItem("Serien und Shows", {"urlC": "/sendung_a_z.php"})
  54.     addDirectoryItem("News und Reportagen", {"urlN": "/newsuebersicht.php"})
  55.     #addDirectoryItem("Filme (Pay only)", {"urlC": "/film_a_z.php"})
  56.     xbmcplugin.endOfDirectory(thisPlugin)
  57.  
  58. def showContent(urlC):
  59.     global thisPlugin
  60.     print "[phr] -- urlC "+urlC
  61.     content = getUrl(urlC)
  62.     #print "[phr] -- content "+content
  63.     match = re.compile(newRegexContent,re.DOTALL).findall(content)
  64.     for showcontent in match:
  65.         matchLink = re.compile(newLinkRegexContent,re.DOTALL).findall(showcontent)
  66.         matchImg = re.compile(newImgRegexContent,re.DOTALL).findall(showcontent)
  67.         matchFree = re.compile(newFreeRegexContent,re.DOTALL).findall(showcontent)
  68.         matchTitle = re.compile(newTitleRegexContent,re.DOTALL).findall(showcontent)
  69.  
  70.         link = matchLink[0]
  71.         img = matchImg[0]
  72.         title = matchTitle[0]
  73.  
  74.         if matchFree:
  75.             print "FREE show: "+title+", link: "+link+", image: "+img
  76.             addDirectoryItem(title, {"urlS": "/"+link}, img)  
  77.         else:
  78.             print "PAY  show: "+title+", link: "+link+", image: "+img
  79.  
  80.     print "[phr] --- showContent ok"
  81.     xbmcplugin.endOfDirectory(thisPlugin)
  82.  
  83. def showNews(urlN):
  84.     print urlN
  85.     global thisPlugin
  86.     content = getUrl(urlN)
  87.     match = re.compile(newsRegexContent,re.DOTALL).findall(content)
  88.     for showcontent in match:
  89.         #print showcontent
  90.         #print "-----------"
  91.         matchLink = re.compile(newsLinkRegexContent,re.DOTALL).findall(showcontent)
  92.         matchFree = re.compile(newsFreeRegexContent,re.DOTALL).findall(showcontent)
  93.         matchTitle = re.compile(newsTitleRegexContent,re.DOTALL).findall(showcontent)
  94.  
  95.         link = matchLink[0]
  96.         title = matchTitle[0].strip()
  97.  
  98.         if matchFree:
  99.             print "FREE show: "+title+", link: "+link+", image: "
  100.             addDirectoryItem(title, {"urlS": "/"+link}, "")  
  101.         else:
  102.             print "PAY  show: "+title+", link: "+link+", image: "
  103.  
  104.     print "[phr] --- showContent ok"
  105.     xbmcplugin.endOfDirectory(thisPlugin)
  106.  
  107. def showSeries(urlS):
  108.     global thisPlugin
  109.  
  110.     content = getUrl(urlS)
  111.     meta = re.compile(n_regexMeta,re.DOTALL).findall(content)
  112.     print "[phr] --- MetaData"
  113.     print meta
  114.     vars = re.compile(regexTabVars,re.DOTALL).search(content)
  115.    
  116.     if vars:
  117.         tabVars = "&xajaxargs[]="+vars.group(1)+"&xajaxargs[]="+vars.group(2)+"&xajaxargs[]="+vars.group(3)+"&xajaxargs[]="+vars.group(4)+"&xajaxargs[]="+vars.group(5)+"&xajax=show_top_and_movies&xajaxr="+str(time.time()).replace('.','')
  118.         tabentries = re.compile(regexTabEntry,re.DOTALL).findall(vars.group(6))
  119.         content = ""
  120.        
  121.         for te in tabentries:
  122.             ajcon = postUrl(urlHost+ajaxUrl,"xajaxargs[]="+te+tabVars);
  123.             content += ajcon;
  124.     #print content
  125.     match = re.compile(regexSeries,re.DOTALL).findall(content)
  126.     #print match
  127.     for m in match:
  128.         #print m
  129.         if "kostenlos" in m[4]:
  130.             date = remHTML(m[3])
  131.             if date == "232.000x":
  132.                 date = ""
  133.             else:
  134.                 date = " - " + date
  135.             title = decode_htmlentities(m[2]) + date
  136.             metatext = title.encode("UTF-8") + " : "+decode_htmlentities(meta[0][0])
  137.             print "[phr] -- metatext: "+metatext
  138.             #decode_htmlentities(title + " - " + meta[0][0])
  139.             addPlayableItem(title, {"urlV": m[1], "vidN": m[2]},meta[0][1], metatext)
  140.         ##print m[2]  
  141.     print "[phr] --- showSeries ok"
  142.     xbmcplugin.endOfDirectory(thisPlugin)
  143.  
  144. def showVideo(urlV, vidN):
  145.     print "[phr] --- showVideo"
  146.     global thisPlugin
  147.     print "[phr] --- "+urlV
  148.     content = getUrl(urlV)
  149.     match=re.compile(regexVideoData).findall(content)
  150.     xmlUrl = urlHost+urllib.unquote(match[0])
  151.     print "[phr] --- "+xmlUrl
  152.     contentB = getUrl(xmlUrl)
  153.     print contentB
  154.     print "[phr] ------DebugOutput showVideo--"
  155.     matchfilename = re.compile(regexXML).findall(contentB)
  156.     if matchfilename[0].find(".f4v.f4m?ts=") != -1:
  157.         f4mXML = getUrl(matchfilename[0])
  158.         #print f4mXML
  159.         videoReged = re.compile(regexF4mVideo).findall(matchfilename[0])
  160.         print "[phr] -- found f4m videourl: "+videoReged[0]
  161.         videoUrl = "rtmpe://fms-fra"+str(random.randint(1, 20))+".rtl.de/rtlnow/"
  162.         playpath = "mp4:"+videoReged[0]
  163.        
  164.     else:
  165.         print "[phr] -- found flv videourl: "+ matchfilename[0]
  166.         splitted = matchfilename[0].split('/')
  167.         print splitted
  168.         # -----
  169.         videoUrl=splitted[0]+"//"+splitted[2]+"/"+splitted[3]+"/"
  170.         videoUrlB=splitted[2]+"/"+splitted[3]+"/"
  171.         addpre=""  
  172.         if splitted[5][-4:-1] == ".f4":
  173.             addpre="mp4:"
  174.         if splitted[5][-4:-1] == ".fl":
  175.             splitted[5]=splitted[5][0:-4]
  176.         playpath = addpre+splitted[4]+"/"+splitted[5]
  177.         #print "[phr] playPath -- "+playpath
  178.        
  179.     swfUrl = "http://rtl-now.rtl.de/includes/vodplayer.swf"
  180.     pageUrl = "http://rtl-now.rtl.de/p"
  181.    
  182.     listitem = xbmcgui.ListItem(vidN)
  183.     fullData = videoUrl+' swfVfy=1 playpath='+playpath+' app=rtlnow/_definst_ pageUrl='+pageUrl+'/ tcUrl='+videoUrl+' swfUrl='+swfUrl
  184.     print fullData
  185.     print "[phr] ------------"
  186.     listitem = xbmcgui.ListItem(path=fullData)
  187.    
  188.     return xbmcplugin.setResolvedUrl(thisPlugin, True, listitem)
  189.    
  190.  
  191. # ------ helper ------
  192.  
  193. def returnBiggerPic(picUrl):
  194.     #matchPic = re.compile("(.*?format/)(.*?)(/format.*?)",re.DOTALL).findall(m[0])
  195.     #if !matchPic:
  196.     matchPic = re.compile(regexBiggerPic,re.DOTALL).findall(picUrl)
  197.     Res = matchPic[0][1].split("x")
  198.     resx = int(Res[0])*3
  199.     resy = int(Res[1])*3
  200.     newUrl = matchPic[0][0]+str(resx)+"x"+str(resy)+matchPic[0][2]+matchPic[0][3]
  201.     return newUrl
  202.    
  203. def remHTML(text):
  204.     result = re.compile(regexTextOnly,re.DOTALL).sub('',text)
  205.     return result
  206.  
  207. def postUrl(url, values):
  208.     req = urllib2.Request(url)
  209.     response = urllib2.urlopen(req, values)
  210.     link=response.read()
  211.     response.close()
  212.     return link
  213.  
  214. def getUrl(url):
  215.     req = urllib2.Request(url)
  216.     req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
  217.     response = urllib2.urlopen(req)
  218.     link=response.read()
  219.     response.close()
  220.     return link
  221.  
  222. def addDirectoryItem(name, parameters={},pic=""):
  223.     li = xbmcgui.ListItem(name,iconImage="DefaultFolder.png", thumbnailImage=pic)
  224.     url = sys.argv[0] + '?' + urllib.urlencode(parameters)
  225.     return xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]), url=url, listitem=li, isFolder=True)
  226.  
  227. def addPlayableItem(name, parameters={}, pic="", desc=""):
  228.     li = xbmcgui.ListItem(name,iconImage="DefaultFolder.png", thumbnailImage=pic)
  229.     li.setInfo( type="Video", infoLabels={ "Title": name, "Plot" : desc, "PlotOutline":desc, "Label":desc } )
  230.     li.setProperty('IsPlayable', 'true')
  231.     url = sys.argv[0] + '?' + urllib.urlencode(parameters)
  232.     return xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]), url=url, listitem=li, isFolder=False)
  233.    
  234. def parameters_string_to_dict(parameters):
  235.     ''' Convert parameters encoded in a URL to a dict. '''
  236.     paramDict = {}
  237.     if parameters:
  238.         paramPairs = parameters[1:].split("&")
  239.         for paramsPair in paramPairs:
  240.             paramSplits = paramsPair.split('=')
  241.             if (len(paramSplits)) == 2:
  242.                 paramDict[paramSplits[0]] = paramSplits[1]
  243.     return paramDict
  244.  
  245. def substitute_entity(match):
  246.     ent = match.group(3)
  247.    
  248.     if match.group(1) == "#":
  249.         if match.group(2) == '':
  250.             return unichr(int(ent))
  251.         elif match.group(2) == 'x':
  252.             return unichr(int('0x'+ent, 16))
  253.     else:
  254.         cp = n2cp.get(ent)
  255.  
  256.         if cp:
  257.             return unichr(cp)
  258.         else:
  259.             return match.group()
  260.  
  261. def decode_htmlentities(string):
  262.     entity_re = re.compile(r'&(#?)(x?)(\w+);')
  263.     return entity_re.subn(substitute_entity, string)[0]
  264.  
  265. # ----- main -----
  266.  
  267. params = parameters_string_to_dict(sys.argv[2])
  268. urlContent = str(params.get("urlC", ""))
  269. urlNews = str(params.get("urlN", ""))
  270. urlSeries = str(params.get("urlS", ""))
  271. urlVideo = str(params.get("urlV", ""))
  272. vidName = str(params.get("vidN", ""))
  273.  
  274. if not sys.argv[2]:
  275.     # new start
  276.     ok = showGenre()
  277. else:
  278.     if urlContent:
  279.         newUrl = urlHost + urllib.unquote(urlContent)
  280.         print "[phr] showContent: "+newUrl
  281.         ok = showContent(newUrl)
  282.     if urlNews:
  283.         newUrl = urlHost + urllib.unquote(urlNews)
  284.         print "[phr] showNews: "+newUrl
  285.         ok = showNews(newUrl)
  286.     if urlSeries:
  287.         newUrl = urlHost + urllib.unquote(urlSeries)
  288.         print "[phr] showSeries: "+newUrl
  289.         ok = showSeries(newUrl)
  290.     if urlVideo:
  291.         newUrl = urlHost + decode_htmlentities(urllib.unquote(urlVideo))
  292.         print "[phr] showVideo: "+newUrl
  293.         ok = showVideo(newUrl, decode_htmlentities(urllib.unquote_plus(vidName)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement