home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 May / maximum-cd-2010-05.iso / DiscContents / boxee-0.9.20.10711.exe / scripts / OpenSubtitles / resources / lib / osdb.py < prev    next >
Encoding:
Python Source  |  2009-11-25  |  14.6 KB  |  403 lines

  1. import sys
  2. import os
  3. import xmlrpclib
  4. import urllib, urllib2
  5. import unzip
  6. import globals
  7. import RecursiveParser
  8. from xml.dom import minidom  
  9.  
  10. from utilities import *
  11.  
  12. _ = sys.modules[ "__main__" ].__language__
  13.  
  14. BASE_URL_XMLRPC_DEV = u"http://dev.opensubtitles.org/xml-rpc"
  15. BASE_URL_XMLRPC = u"http://www.opensubtitles.org/xml-rpc"
  16. BASE_URL_SEARCH = u"http://www.opensubtitles.com/%s/search/moviename-%s/simplexml"
  17. BASE_URL_SEARCH_ALL = u"http://www.opensubtitles.com/en/search/sublanguageid-all/moviename-%s/simplexml"
  18. BASE_URL_DOWNLOAD = u"http://dev.opensubtitles.org/%s"
  19. BASE_URL_OSTOK = "http://app.boxee.tv/api/ostok"
  20.  
  21. def compare_columns(a, b):
  22.         # sort on ascending index 0, descending index 2
  23.         return cmp( a["language_name"], b["language_name"] )  or cmp( b["sync"], a["sync"] ) 
  24.  
  25. class OSDBServer:
  26.     def Create(self):
  27.     self.subtitles_list = []
  28.     self.subtitles_hash_list = []
  29.     self.subtitles_name_list = []
  30.     self.subtitles_imdbid_list = []
  31.     self.languages_list = []
  32.     self.folderfilesinfo_list = []
  33.     self.osdb_token = ""
  34.     self.connected = False
  35.     self.smbfile = False
  36.  
  37.     def connect( self, osdb_server, username, password ):
  38.     LOG( LOG_INFO, "Connecting to server " + osdb_server + "..." )
  39.     try:
  40.         if osdb_server:
  41.             self.server = xmlrpclib.Server(osdb_server)
  42.             info = self.server.ServerInfo()
  43. #            if username:
  44. #                LOG( LOG_INFO, "Logging in " + username + "..." )
  45. #                login = self.server.LogIn(username, password, "en", "XBMC")
  46. #            else:
  47. #                LOG( LOG_INFO, "Logging in anonymously..." )
  48. #                login = self.server.LogIn("", "", "en", "XBMC")
  49.             socket = urllib.urlopen( BASE_URL_OSTOK )
  50.             result = socket.read()
  51.             socket.close()
  52.             xmldoc = minidom.parseString(result)
  53.             token = xmldoc.getElementsByTagName("token")[0].firstChild.data
  54.             if token:
  55. #            if (login["status"].find("200") > -1):
  56.                 self.connected = True
  57.                 self.osdb_token = token
  58. #                self.osdb_token = login["token"]
  59.                 LOG( LOG_INFO, "Connected" )
  60.                 return True, ""
  61.             else:
  62.                 self.connected = False
  63. #                error = "Login " + login["status"]
  64.                 error = _( 653 )
  65.                 LOG( LOG_ERROR, error )
  66.                 return False, error
  67.         else:
  68.             self.connected = False
  69.             error = _( 730 )
  70.             LOG( LOG_ERROR, error )
  71.             return False, error
  72.     except Exception, e:
  73.         error = _( 731 ) % ( _( 732 ), str ( e ) )
  74.         LOG( LOG_ERROR, error )
  75.         return False, error
  76.  
  77.  
  78.     def disconnect( self ):
  79.     try:
  80.         if ( self.osdb_token ) and ( self.connected ):
  81.             LOG( LOG_INFO, "Disconnecting from server..." )
  82.             logout = self.server.LogOut(self.osdb_token)
  83.             self.connected = False
  84.             LOG( LOG_DEBUG, logout )
  85.             LOG( LOG_INFO, "Disconnected" )
  86.             return True, ""
  87.         else:
  88.             error = _( 737 )
  89.             LOG( LOG_ERROR, error )
  90.             return False, error
  91.     except Exception, e:
  92.         error = _( 731 ) % ( _( 733 ), str ( e ) )
  93.         LOG( LOG_ERROR, error )
  94.         return False, error
  95.  
  96.  
  97.     def getlanguages( self ):
  98.     try:
  99.         if self.connected:
  100.             LOG( LOG_INFO, "Retrieve subtitle languages..." )
  101.             languages = self.server.GetSubLanguages()
  102.             LOG( LOG_INFO, "Retrieved subtitle languages" )      
  103.             self.languages_list = []
  104.             if languages["data"]:
  105.                 for item in languages["data"]:
  106.                     self.languages_list.append( {"language_name":item["LanguageName"], "language":item["SubLanguageID"], "flag_image":"flags/" + item["ISO639"] + ".png"} )
  107.             return True, ""
  108.         else:
  109.             error = _( 737 )
  110.             LOG( LOG_ERROR, error )
  111.             return False, error
  112.     except Exception, e:
  113.         error = _( 731 ) % ( _( 734 ), str ( e ) )
  114.         LOG( LOG_ERROR, error )
  115.         return False, error
  116.  
  117.     def getfolderfilesinfo( self, folder ):
  118.     recursivefiles = []
  119.     hashedfiles_list = []
  120.     hashes_list = []
  121.     self.folderfilesinfo_list = []
  122.     try:
  123.         if ( self.connected ) and ( os.path.isdir( folder ) ):
  124.             parser = RecursiveParser.RecursiveParser()
  125.             recursivefiles = parser.getRecursiveFileList(folder, globals.videos_ext)
  126.             for item in recursivefiles:
  127.                 filename = globals.EncodeLocale(os.path.basename(item))
  128.                 filenameurl = (item) #Corrects the accent characters.
  129.                 if not os.path.exists(item):
  130.                     error = _( 738 ) % ( item, )
  131.                     LOG( LOG_ERROR, error )
  132.                     return False, error
  133.                 else:
  134.                     hash = globals.hashFile(item)
  135.                     if hash == "IOError" or hash == "SizeError":
  136.                         error = _( 739 ) % ( item, hash, )
  137.                         LOG( LOG_ERROR, error )
  138.                         return False, errors
  139.                     else:
  140.                         hashedfiles_list.append( {'filename':item, 'hash':hash} )
  141.                         hashes_list.append( hash )
  142.             if hashedfiles_list:
  143.                 hashes = self.server.CheckMovieHash(self.osdb_token, hashes_list)
  144.                 for item in hashes_list:
  145.                     for file in hashedfiles_list:
  146.                         if file["hash"] == item:
  147.                             f = file["filename"]                
  148.                     if hashes["data"][item]:
  149.                         movie = hashes["data"][item]
  150.                         self.folderfilesinfo_list.append( {'filename':f, 'moviename':movie['MovieName'], 'imdbid':movie['MovieImdbID']} )
  151.                         
  152.     except Exception, e:
  153.         error = _( 731 ) % ( _( 735 ), str ( e ) ) 
  154.         LOG( LOG_ERROR, error )
  155.         return False, error
  156.         
  157.     def mergesubtitles( self ):
  158.         self.subtitles_list = []
  159.         if( len ( self.subtitles_hash_list ) > 0 ):
  160.             for item in self.subtitles_hash_list:
  161.                 if item["format"].find( "srt" ) == 0:
  162.                     self.subtitles_list.append( item )
  163.         if( len ( self.subtitles_name_list ) > 0 ):
  164.             for item in self.subtitles_name_list:
  165.                 if item["format"].find( "srt" ) == 0:
  166.                     self.subtitles_list.append( item )
  167.         if( len ( self.subtitles_imdbid_list ) > 0 ):
  168.             for item in self.subtitles_imdbid_list:
  169.                 if item["format"].find( "srt" ) == 0:
  170.                     self.subtitles_list.append( item )
  171.         if( len ( self.subtitles_list ) > 0 ):
  172.             self.subtitles_list = sorted(self.subtitles_list, compare_columns)
  173.         
  174.     def searchsubtitles( self, file, hash, size, language="all" ):
  175.     self.subtitles_hash_list = []
  176.         self.allow_exception = False
  177.     
  178.     try:
  179.         if ( self.osdb_token ) and ( self.connected ):
  180.             LOG( LOG_INFO, "Searching subtitles by hash for " + file )
  181.             filename = globals.EncodeLocale( os.path.basename( file ) )
  182.             
  183.             videofilesize = size
  184.             linkhtml_index =  "search/moviebytesize-"+str( videofilesize )+"/moviehash-"+hash
  185.             videofilename = filename
  186.             pathvideofilename = file
  187.             videohash = hash
  188.             hashresult = {"hash":hash, "filename":filename, "pathvideofilename":file, "filesize":str( videofilesize ), "linkhtml_index":linkhtml_index}
  189.             searchlist = []
  190.             searchlist.append({'sublanguageid':language,'moviehash':hashresult["hash"],'moviebytesize':str( hashresult["filesize"] ) })
  191.             search = self.server.SearchSubtitles( self.osdb_token, searchlist )
  192.  
  193.             if search["data"]:
  194.                 for item in search["data"]:
  195.                     if item["ISO639"]:
  196.                         flag_image = "flags/" + item["ISO639"] + ".png"
  197.                     else:                                
  198.                         flag_image = "-.png"
  199.                     self.subtitles_hash_list.append({'filename':item["SubFileName"],'link':item["ZipDownloadLink"],"language_name":item["LanguageName"],"language_flag":flag_image,"language_id":item["SubLanguageID"],"ID":item["IDSubtitle"],"rating":str( int( item["SubRating"][0] ) ),"format":item["SubFormat"],"sync":True})
  200.                 self.subtitles_list.append( self.subtitles_hash_list )
  201.  
  202.                 message = _( 742 ) % ( str( len ( self.subtitles_hash_list ) ), )
  203.                 LOG( LOG_INFO, message )
  204.                 return True, message
  205.             else: 
  206.                 message = _( 741 )
  207.                 LOG( LOG_INFO, message )
  208.                 return True, message
  209.     except Exception, e:
  210.         error = _( 731 ) % ( _( 736 ), str ( e ) ) 
  211.         LOG( LOG_ERROR, error )
  212.         return False, error
  213.  
  214.     def searchsubtitlesbyhash( self, file, language="all" ):
  215.     self.subtitles_hash_list = []
  216.         self.allow_exception = False
  217.     
  218.     try:
  219.         if ( self.osdb_token ) and ( self.connected ):
  220.             LOG( LOG_INFO, "Searching subtitles by hash for " + file )
  221.             
  222.             #We try and check if the file is from an smb share
  223.             if file.find( "//" ) == 0:
  224.                 error = _( 740 )
  225.                 LOG( LOG_ERROR, error )
  226.                 return False, error
  227.  
  228.             filename = globals.EncodeLocale( os.path.basename( file ) )
  229.             filenameurl = ( filename ) #Corrects the accent characters.
  230.             
  231.             if not os.path.exists( file ):
  232.                 error = _( 738 ) % ( file, )
  233.                 LOG( LOG_ERROR, error )
  234.                 return False, error
  235.             else:
  236.                 hash = globals.hashFile( file )
  237.                 if hash == "IOError" or hash== "SizeError":
  238.                     error = _( 739 ) % ( file, hash, )
  239.                     LOG( LOG_ERROR, error )
  240.                     return False, error        
  241.                 #We keep going if there was no error.
  242.                 videofilesize = os.path.getsize( file )
  243.                 linkhtml_index =  "search/moviebytesize-"+str( videofilesize )+"/moviehash-"+hash
  244.                 videofilename = filename
  245.                 pathvideofilename = file
  246.                 videohash = hash
  247.                 hashresult = {"hash":hash, "filename":filename, "pathvideofilename":file, "filesize":str( videofilesize )
  248.                         , "linkhtml_index":linkhtml_index}
  249.                 searchlist = []
  250.                 searchlist.append({'sublanguageid':language,'moviehash':hashresult["hash"],'moviebytesize':str( hashresult["filesize"] ) })
  251.                 search = self.server.SearchSubtitles( self.osdb_token, searchlist )
  252.  
  253.                 if search["data"]:
  254.                     for item in search["data"]:
  255.                         if item["ISO639"]:
  256.                             flag_image = "flags/" + item["ISO639"] + ".png"
  257.                         else:    
  258.                             flag_image = "-.png"
  259.                         self.subtitles_hash_list.append({'filename':item["SubFileName"],'link':item["ZipDownloadLink"],"language_name":item["LanguageName"],"language_flag":flag_image,"language_id":item["SubLanguageID"],"ID":item["IDSubtitle"],"rating":str( int( item["SubRating"][0] ) ),"format":item["SubFormat"],"sync":True})
  260.                     self.subtitles_list.append ( self.subtitles_hash_list )
  261.  
  262.                     message = _( 742 ) % ( str( len ( self.subtitles_hash_list ) ), )
  263.                     LOG( LOG_INFO, message )
  264.                     return True, message
  265.                 else: 
  266.                     message = _( 741 )
  267.                     LOG( LOG_INFO, message )
  268.                     return True, message
  269.     except Exception, e:
  270.         error = _( 731 ) % ( _( 736 ), str ( e ) ) 
  271.         LOG( LOG_ERROR, error )
  272.         return False, error
  273.  
  274.     def searchsubtitlesbyimdbid( self, imdbid, language="all" ):
  275.     self.subtitles_imdbid_list = []
  276.         self.allow_exception = False
  277.     
  278.     try:
  279.         if ( self.osdb_token ) and ( self.connected ):
  280.             LOG( LOG_INFO, "Searching subtitles by imdbid for " + imdbid )
  281.             
  282.             #We try and check if the file is from an smb share
  283.             if file.find( "//" ) == 0:
  284.                 error = _( 740 )
  285.                 LOG( LOG_ERROR, error )
  286.                 return False, error
  287.  
  288.             filename = globals.EncodeLocale( os.path.basename( file ) )
  289.             filenameurl = ( filename ) #Corrects the accent characters.
  290.             
  291.             if not os.path.exists( file ):
  292.                 error = _( 738 ) % ( file, )
  293.                 LOG( LOG_ERROR, error )
  294.                 return False, error
  295.             else:
  296.                 hash = globals.hashFile( file )
  297.                 if hash == "IOError" or hash== "SizeError":
  298.                     error = _( 739 ) % ( file, hash, )
  299.                     LOG( LOG_ERROR, error )
  300.                     return False, error        
  301.                 #We keep going if there was no error.
  302.                 videofilesize = os.path.getsize( file )
  303.                 linkhtml_index =  "search/moviebytesize-"+str( videofilesize )+"/moviehash-"+hash
  304.                 videofilename = filename
  305.                 pathvideofilename = file
  306.                 videohash = hash
  307.                 hashresult = {"hash":hash, "filename":filename, "pathvideofilename":file, "filesize":str( videofilesize )
  308.                         , "linkhtml_index":linkhtml_index}
  309.                 searchlist = []
  310.                 searchlist.append({'sublanguageid':language,'moviehash':hashresult["hash"],'moviebytesize':str( hashresult["filesize"] ) })
  311.                 search = self.server.SearchSubtitles( self.osdb_token, searchlist )
  312.                 if search["data"]:
  313.                     for item in search["data"]:
  314.                         if item["ISO639"]:
  315.                             flag_image = "flags/" + item["ISO639"] + ".png"
  316.                         else:    
  317.                             flag_image = "-.png"
  318.                         self.subtitles_imdbid_list.append({'filename':item["SubFileName"],'link':item["ZipDownloadLink"],"language_name":item["LanguageName"],"language_flag":flag_image,"language_id":item["SubLanguageID"],"ID":item["IDSubtitle"],"rating":str( int( item["SubRating"][0] ) ),"format":item["SubFormat"],"sync":True})
  319.                     self.subtitles_list.append ( self.subtitles_imdbid_list )
  320.  
  321.                     message = _( 742 ) % ( str( len ( self.subtitles_imdbid_list ) ), )
  322.                     LOG( LOG_INFO, message )
  323.                     return True, message
  324.                 else: 
  325.                     message = _( 741 )
  326.                     LOG( LOG_INFO, message )
  327.                     return True, message
  328.     except Exception, e:
  329.         error = _( 731 ) % ( _( 736 ), str ( e ) ) 
  330.         LOG( LOG_ERROR, error )
  331.         return False, error
  332.  
  333.     def searchsubtitlesbyname( self, name, language="all" ):
  334.     self.subtitles_name_list = []
  335.         self.allow_exception = False
  336.     search_url = ""
  337.     
  338.     try:
  339.         LOG( LOG_INFO, "Searching subtitles by name for " + name )
  340.         
  341.         if language == "all":
  342.             search_url = BASE_URL_SEARCH_ALL % ( os.path.basename( name ), )
  343.         else:
  344.             search_url = BASE_URL_SEARCH % ( language, os.path.basename( name ) )
  345.         
  346.         search_url.replace( " ", "+" )
  347.         LOG( LOG_INFO, search_url )
  348.  
  349.         socket = urllib.urlopen( search_url )
  350.         result = socket.read()
  351.         socket.close()
  352.         xmldoc = minidom.parseString(result)
  353.  
  354.         subtitles = xmldoc.getElementsByTagName("subtitle")
  355.  
  356.         if subtitles:
  357.             url_base = xmldoc.childNodes[0].childNodes[1].firstChild.data
  358.             for subtitle in subtitles:
  359.                 filename = ""
  360.                 movie = ""
  361.                 lang_name = ""
  362.                 subtitle_id = ""
  363.                 lang_id = ""
  364.                 flag_image = ""
  365.                 link = ""
  366.                 if subtitle.getElementsByTagName("releasename")[0].firstChild:
  367.                     filename = subtitle.getElementsByTagName("releasename")[0].firstChild.data
  368.                 if subtitle.getElementsByTagName("format")[0].firstChild:
  369.                     format = subtitle.getElementsByTagName("format")[0].firstChild.data
  370.                     filename = filename + "." +  format
  371.                 if subtitle.getElementsByTagName("movie")[0].firstChild:
  372.                     movie = subtitle.getElementsByTagName("movie")[0].firstChild.data
  373.                 if subtitle.getElementsByTagName("language")[0].firstChild:
  374.                     lang_name = subtitle.getElementsByTagName("language")[0].firstChild.data
  375.                 if subtitle.getElementsByTagName("idsubtitle")[0].firstChild:
  376.                     subtitle_id = subtitle.getElementsByTagName("idsubtitle")[0].firstChild.data
  377.                 if subtitle.getElementsByTagName("iso639")[0].firstChild:
  378.                     lang_id = subtitle.getElementsByTagName("iso639")[0].firstChild.data
  379.                     flag_image = "flags/" + lang_id + ".png"
  380.                 if subtitle.getElementsByTagName("download")[0].firstChild:
  381.                     link = subtitle.getElementsByTagName("download")[0].firstChild.data
  382.                     link = url_base + link
  383.                 if subtitle.getElementsByTagName("subrating")[0].firstChild:
  384.                     rating = subtitle.getElementsByTagName("subrating")[0].firstChild.data
  385.                 
  386.                     
  387.                 self.subtitles_name_list.append({'filename':filename,'link':link,'language_name':lang_name,'language_id':lang_id,'language_flag':flag_image,'movie':movie,"ID":subtitle_id,"rating":str( int( rating[0] ) ),"format":format,"sync":False})
  388.             self.subtitles_list.append ( self.subtitles_name_list )
  389.  
  390.             message = _( 742 ) % ( str( len ( self.subtitles_name_list ) ), )
  391.             LOG( LOG_INFO, message )
  392.             return True, message
  393.         else: 
  394.             message = _( 741 )
  395.             LOG( LOG_INFO, message )
  396.             return True, message
  397.  
  398.     except Exception, e:
  399.         error = _( 743 ) % ( search_url, str ( e ) ) 
  400.         LOG( LOG_ERROR, error )
  401.         return False, error
  402.  
  403.