* fix possible Enigma2 crash while decoding thumbnails
[vuplus_dvbapp-plugin] / mytube / src / MyTubeService.py
index 01715bc..fc5a579 100755 (executable)
@@ -1,5 +1,5 @@
 # -*- coding: iso-8859-1 -*-
-from __init__ import _
+from __init__ import bin2long, long2bin, rsa_pub1024, decrypt_block
 import gdata.youtube
 import gdata.youtube.service
 from gdata.service import BadAuthentication
@@ -13,12 +13,24 @@ from twisted.internet import reactor
 from urllib2 import Request, URLError, HTTPError, urlopen as urlopen2
 from socket import gaierror,error
 import re, os, sys, socket
-import urllib
-from urllib import FancyURLopener, quote
+from urllib import quote, unquote_plus, unquote
 import cookielib
 from httplib import HTTPConnection,CannotSendRequest,BadStatusLine,HTTPException
 HTTPConnection.debuglevel = 1
 
+def validate_cert(cert, key):
+       buf = decrypt_block(cert[8:], key) 
+       if buf is None:
+               return None
+       return buf[36:107] + cert[139:196]
+
+def get_rnd():
+       try:
+               rnd = os.urandom(8)
+               return rnd
+       except:
+               return None
+
 std_headers = {
        'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2',
        'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
@@ -31,9 +43,8 @@ std_headers = {
 #config.plugins.mytube.general.useHTTPProxy = ConfigYesNo(default = False)
 #config.plugins.mytube.general.ProxyIP = ConfigIP(default=[0,0,0,0])
 #config.plugins.mytube.general.ProxyPort = ConfigNumber(default=8080)
-
-class MyOpener(FancyURLopener):
-       version = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12'
+#class MyOpener(FancyURLopener):
+#      version = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12'
 
 
 class GoogleSuggestions():
@@ -83,9 +94,9 @@ class MyTubeFeedEntry():
                self.entry = entry
                self.favoritesFeed = favoritesFeed
                self.thumbnail = {}
-               self.myopener = MyOpener()
+               """self.myopener = MyOpener()
                urllib.urlopen = MyOpener().open
-               """if config.plugins.mytube.general.useHTTPProxy.value is True:
+               if config.plugins.mytube.general.useHTTPProxy.value is True:
                        proxy = {'http': 'http://'+str(config.plugins.mytube.general.ProxyIP.getText())+':'+str(config.plugins.mytube.general.ProxyPort.value)}
                        self.myopener = MyOpener(proxies=proxy)
                        urllib.urlopen = MyOpener(proxies=proxy).open
@@ -186,32 +197,32 @@ class MyTubeFeedEntry():
                mrl = None
                isHDAvailable = False
                video_id = str(self.getTubeId())
+               #URLs for YouTube video pages will change from the format http://www.youtube.com/watch?v=ylLzyHk54Z0 to http://www.youtube.com/watch#!v=ylLzyHk54Z0.
                watch_url = "http://www.youtube.com/watch?v=" + video_id
                watchrequest = Request(watch_url, None, std_headers)
                try:
-                       print "trying to find out if a HD Stream is available"
+                       print "trying to find out if a HD Stream is available",watch_url
                        watchvideopage = urlopen2(watchrequest).read()
                except (urllib2.URLError, httplib.HTTPException, socket.error), err:
-                       print "[MyTube] Error: Unable to retrieve watchpage"
-                       print "[MyTube] Error code: ", str(err)
+                       print "[MyTube] Error: Unable to retrieve watchpage - Error code: ", str(err)
                        print "[MyTube] No valid mp4-url found"
                        return mrl
 
-               if "isHDAvailable = true" in watchvideopage:
+               if "'IS_HD_AVAILABLE': true" in watchvideopage:
                        isHDAvailable = True
                        print "HD AVAILABLE"
                else:
                        print "HD Stream NOT AVAILABLE"
 
                # Get video info
-               info_url = 'http://www.youtube.com/get_video_info?&video_id=%s&el=detailpage&ps=default&eurl=&gl=US&hl=en' % video_id
+               #info_url = 'http://www.youtube.com/get_video_info?&video_id=%s&el=detailpage&ps=default&eurl=&gl=US&hl=en' % video_id
+               info_url = 'http://www.youtube.com/get_video_info?&video_id=%s' % video_id
                inforequest = Request(info_url, None, std_headers)
                try:
                        print "getting video_info_webpage",info_url
                        infopage = urlopen2(inforequest).read()
                except (urllib2.URLError, httplib.HTTPException, socket.error), err:
-                       print "[MyTube] Error: Unable to retrieve infopage"
-                       print "[MyTube] Error code: ", str(err)
+                       print "[MyTube] Error: Unable to retrieve infopage, error:", str(err)
                        print "[MyTube] No valid mp4-url found"
                        return mrl
 
@@ -222,12 +233,13 @@ class MyTubeFeedEntry():
                        if mobj is None:
                                print 'ERROR: unable to extract "t" parameter for unknown reason'
                        else:
-                               reason = urllib.unquote_plus(mobj.group(1))
+                               reason = unquote_plus(mobj.group(1))
                                print 'ERROR: YouTube said: %s' % reason.decode('utf-8')
                        return mrl
        
-               token = urllib.unquote(mobj.group(1))
-               myurl = 'http://www.youtube.com/get_video?video_id=%s&t=%s&eurl=&el=detailpage&ps=default&gl=US&hl=en' % (video_id, token)
+               token = unquote(mobj.group(1))
+               #myurl = 'http://www.youtube.com/get_video?video_id=%s&t=%s&eurl=&el=detailpage&ps=default&gl=US&hl=en' % (video_id, token)
+               myurl = 'http://www.youtube.com/get_video?video_id=%s&t=%s' % (video_id, token)
                if isHDAvailable is True:
                        mrl = '%s&fmt=%s' % (myurl, '22')
                        print "[MyTube] GOT HD URL: ", mrl