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.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-12-06  |  12.9 KB  |  501 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. import sys
  5. import os
  6. import xmlrpclib
  7. import urllib
  8. import urllib2
  9. import unzip
  10. import globals
  11. import RecursiveParser
  12. from xml.dom import minidom
  13. from utilities import *
  14. _ = sys.modules['__main__'].__language__
  15. BASE_URL_XMLRPC_DEV = u'http://dev.opensubtitles.org/xml-rpc'
  16. BASE_URL_XMLRPC = u'http://www.opensubtitles.org/xml-rpc'
  17. BASE_URL_SEARCH = u'http://www.opensubtitles.com/%s/search/moviename-%s/simplexml'
  18. BASE_URL_SEARCH_ALL = u'http://www.opensubtitles.com/en/search/sublanguageid-all/moviename-%s/simplexml'
  19. BASE_URL_DOWNLOAD = u'http://dev.opensubtitles.org/%s'
  20. BASE_URL_OSTOK = 'http://app.boxee.tv/api/ostok'
  21.  
  22. def compare_columns(a, b):
  23.     if not cmp(a['language_name'], b['language_name']):
  24.         pass
  25.     return cmp(b['sync'], a['sync'])
  26.  
  27.  
  28. class OSDBServer:
  29.     
  30.     def Create(self):
  31.         self.subtitles_list = []
  32.         self.subtitles_hash_list = []
  33.         self.subtitles_name_list = []
  34.         self.subtitles_imdbid_list = []
  35.         self.languages_list = []
  36.         self.folderfilesinfo_list = []
  37.         self.osdb_token = ''
  38.         self.connected = False
  39.         self.smbfile = False
  40.  
  41.     
  42.     def connect(self, osdb_server, username, password):
  43.         LOG(LOG_INFO, 'Connecting to server ' + osdb_server + '...')
  44.         
  45.         try:
  46.             if osdb_server:
  47.                 self.server = xmlrpclib.Server(osdb_server)
  48.                 info = self.server.ServerInfo()
  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.                     self.connected = True
  56.                     self.osdb_token = token
  57.                     LOG(LOG_INFO, 'Connected')
  58.                     return (True, '')
  59.                 else:
  60.                     self.connected = False
  61.                     error = _(653)
  62.                     LOG(LOG_ERROR, error)
  63.                     return (False, error)
  64.             else:
  65.                 self.connected = False
  66.                 error = _(730)
  67.                 LOG(LOG_ERROR, error)
  68.                 return (False, error)
  69.         except Exception:
  70.             e = None
  71.             error = _(731) % (_(732), str(e))
  72.             LOG(LOG_ERROR, error)
  73.             return (False, error)
  74.  
  75.  
  76.     
  77.     def disconnect(self):
  78.         
  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:
  92.             e = None
  93.             error = _(731) % (_(733), str(e))
  94.             LOG(LOG_ERROR, error)
  95.             return (False, error)
  96.  
  97.  
  98.     
  99.     def getlanguages(self):
  100.         
  101.         try:
  102.             if self.connected:
  103.                 LOG(LOG_INFO, 'Retrieve subtitle languages...')
  104.                 languages = self.server.GetSubLanguages()
  105.                 LOG(LOG_INFO, 'Retrieved subtitle languages')
  106.                 self.languages_list = []
  107.                 if languages['data']:
  108.                     for item in languages['data']:
  109.                         self.languages_list.append({
  110.                             'language_name': item['LanguageName'],
  111.                             'language': item['SubLanguageID'],
  112.                             'flag_image': 'flags/' + item['ISO639'] + '.png' })
  113.                     
  114.                 
  115.                 return (True, '')
  116.             else:
  117.                 error = _(737)
  118.                 LOG(LOG_ERROR, error)
  119.                 return (False, error)
  120.         except Exception:
  121.             e = None
  122.             error = _(731) % (_(734), str(e))
  123.             LOG(LOG_ERROR, error)
  124.             return (False, error)
  125.  
  126.  
  127.     
  128.     def getfolderfilesinfo(self, folder):
  129.         recursivefiles = []
  130.         hashedfiles_list = []
  131.         hashes_list = []
  132.         self.folderfilesinfo_list = []
  133.         
  134.         try:
  135.             if self.connected and os.path.isdir(folder):
  136.                 parser = RecursiveParser.RecursiveParser()
  137.                 recursivefiles = parser.getRecursiveFileList(folder, globals.videos_ext)
  138.                 for item in recursivefiles:
  139.                     filename = globals.EncodeLocale(os.path.basename(item))
  140.                     filenameurl = item
  141.                     if not os.path.exists(item):
  142.                         error = _(738) % (item,)
  143.                         LOG(LOG_ERROR, error)
  144.                         return (False, error)
  145.                         continue
  146.                     hash = globals.hashFile(item)
  147.                     if hash == 'IOError' or hash == 'SizeError':
  148.                         error = _(739) % (item, hash)
  149.                         LOG(LOG_ERROR, error)
  150.                         return (False, errors)
  151.                         continue
  152.                     hashedfiles_list.append({
  153.                         'filename': item,
  154.                         'hash': hash })
  155.                     hashes_list.append(hash)
  156.                 
  157.                 if hashedfiles_list:
  158.                     hashes = self.server.CheckMovieHash(self.osdb_token, hashes_list)
  159.                     for item in hashes_list:
  160.                         for file in hashedfiles_list:
  161.                             if file['hash'] == item:
  162.                                 f = file['filename']
  163.                                 continue
  164.                         
  165.                         if hashes['data'][item]:
  166.                             movie = hashes['data'][item]
  167.                             self.folderfilesinfo_list.append({
  168.                                 'filename': f,
  169.                                 'moviename': movie['MovieName'],
  170.                                 'imdbid': movie['MovieImdbID'] })
  171.                             continue
  172.                     
  173.                 
  174.         except Exception:
  175.             e = None
  176.             error = _(731) % (_(735), str(e))
  177.             LOG(LOG_ERROR, error)
  178.             return (False, error)
  179.  
  180.  
  181.     
  182.     def mergesubtitles(self):
  183.         self.subtitles_list = []
  184.         if len(self.subtitles_hash_list) > 0:
  185.             for item in self.subtitles_hash_list:
  186.                 if item['format'].find('srt') == 0:
  187.                     self.subtitles_list.append(item)
  188.                     continue
  189.             
  190.         
  191.         if len(self.subtitles_name_list) > 0:
  192.             for item in self.subtitles_name_list:
  193.                 if item['format'].find('srt') == 0:
  194.                     self.subtitles_list.append(item)
  195.                     continue
  196.             
  197.         
  198.         if len(self.subtitles_imdbid_list) > 0:
  199.             for item in self.subtitles_imdbid_list:
  200.                 if item['format'].find('srt') == 0:
  201.                     self.subtitles_list.append(item)
  202.                     continue
  203.             
  204.         
  205.         if len(self.subtitles_list) > 0:
  206.             self.subtitles_list = sorted(self.subtitles_list, compare_columns)
  207.         
  208.  
  209.     
  210.     def searchsubtitles(self, file, hash, size, language = 'all'):
  211.         self.subtitles_hash_list = []
  212.         self.allow_exception = False
  213.         
  214.         try:
  215.             if self.osdb_token and self.connected:
  216.                 LOG(LOG_INFO, 'Searching subtitles by hash for ' + file)
  217.                 filename = globals.EncodeLocale(os.path.basename(file))
  218.                 videofilesize = size
  219.                 linkhtml_index = 'search/moviebytesize-' + str(videofilesize) + '/moviehash-' + hash
  220.                 videofilename = filename
  221.                 pathvideofilename = file
  222.                 videohash = hash
  223.                 hashresult = {
  224.                     'hash': hash,
  225.                     'filename': filename,
  226.                     'pathvideofilename': file,
  227.                     'filesize': str(videofilesize),
  228.                     'linkhtml_index': linkhtml_index }
  229.                 searchlist = []
  230.                 searchlist.append({
  231.                     'sublanguageid': language,
  232.                     'moviehash': hashresult['hash'],
  233.                     'moviebytesize': str(hashresult['filesize']) })
  234.                 search = self.server.SearchSubtitles(self.osdb_token, searchlist)
  235.                 if search['data']:
  236.                     for item in search['data']:
  237.                         if item['ISO639']:
  238.                             flag_image = 'flags/' + item['ISO639'] + '.png'
  239.                         else:
  240.                             flag_image = '-.png'
  241.                         self.subtitles_hash_list.append({
  242.                             'filename': item['SubFileName'],
  243.                             'link': item['ZipDownloadLink'],
  244.                             'language_name': item['LanguageName'],
  245.                             'language_flag': flag_image,
  246.                             'language_id': item['SubLanguageID'],
  247.                             'ID': item['IDSubtitle'],
  248.                             'rating': str(int(item['SubRating'][0])),
  249.                             'format': item['SubFormat'],
  250.                             'sync': True })
  251.                     
  252.                     self.subtitles_list.append(self.subtitles_hash_list)
  253.                     message = _(742) % (str(len(self.subtitles_hash_list)),)
  254.                     LOG(LOG_INFO, message)
  255.                     return (True, message)
  256.                 else:
  257.                     message = _(741)
  258.                     LOG(LOG_INFO, message)
  259.                     return (True, message)
  260.         except Exception:
  261.             e = None
  262.             error = _(731) % (_(736), str(e))
  263.             LOG(LOG_ERROR, error)
  264.             return (False, error)
  265.  
  266.  
  267.     
  268.     def searchsubtitlesbyhash(self, file, language = 'all'):
  269.         self.subtitles_hash_list = []
  270.         self.allow_exception = False
  271.         
  272.         try:
  273.             if self.osdb_token and self.connected:
  274.                 LOG(LOG_INFO, 'Searching subtitles by hash for ' + file)
  275.                 if file.find('//') == 0:
  276.                     error = _(740)
  277.                     LOG(LOG_ERROR, error)
  278.                     return (False, error)
  279.                 
  280.                 filename = globals.EncodeLocale(os.path.basename(file))
  281.                 filenameurl = filename
  282.                 if not os.path.exists(file):
  283.                     error = _(738) % (file,)
  284.                     LOG(LOG_ERROR, error)
  285.                     return (False, error)
  286.                 else:
  287.                     hash = globals.hashFile(file)
  288.                     if hash == 'IOError' or hash == 'SizeError':
  289.                         error = _(739) % (file, hash)
  290.                         LOG(LOG_ERROR, error)
  291.                         return (False, error)
  292.                     
  293.                     videofilesize = os.path.getsize(file)
  294.                     linkhtml_index = 'search/moviebytesize-' + str(videofilesize) + '/moviehash-' + hash
  295.                     videofilename = filename
  296.                     pathvideofilename = file
  297.                     videohash = hash
  298.                     hashresult = {
  299.                         'hash': hash,
  300.                         'filename': filename,
  301.                         'pathvideofilename': file,
  302.                         'filesize': str(videofilesize),
  303.                         'linkhtml_index': linkhtml_index }
  304.                     searchlist = []
  305.                     searchlist.append({
  306.                         'sublanguageid': language,
  307.                         'moviehash': hashresult['hash'],
  308.                         'moviebytesize': str(hashresult['filesize']) })
  309.                     search = self.server.SearchSubtitles(self.osdb_token, searchlist)
  310.                     if search['data']:
  311.                         for item in search['data']:
  312.                             if item['ISO639']:
  313.                                 flag_image = 'flags/' + item['ISO639'] + '.png'
  314.                             else:
  315.                                 flag_image = '-.png'
  316.                             self.subtitles_hash_list.append({
  317.                                 'filename': item['SubFileName'],
  318.                                 'link': item['ZipDownloadLink'],
  319.                                 'language_name': item['LanguageName'],
  320.                                 'language_flag': flag_image,
  321.                                 'language_id': item['SubLanguageID'],
  322.                                 'ID': item['IDSubtitle'],
  323.                                 'rating': str(int(item['SubRating'][0])),
  324.                                 'format': item['SubFormat'],
  325.                                 'sync': True })
  326.                         
  327.                         self.subtitles_list.append(self.subtitles_hash_list)
  328.                         message = _(742) % (str(len(self.subtitles_hash_list)),)
  329.                         LOG(LOG_INFO, message)
  330.                         return (True, message)
  331.                     else:
  332.                         message = _(741)
  333.                         LOG(LOG_INFO, message)
  334.                         return (True, message)
  335.         except Exception:
  336.             e = None
  337.             error = _(731) % (_(736), str(e))
  338.             LOG(LOG_ERROR, error)
  339.             return (False, error)
  340.  
  341.  
  342.     
  343.     def searchsubtitlesbyimdbid(self, imdbid, language = 'all'):
  344.         self.subtitles_imdbid_list = []
  345.         self.allow_exception = False
  346.         
  347.         try:
  348.             if self.osdb_token and self.connected:
  349.                 LOG(LOG_INFO, 'Searching subtitles by imdbid for ' + imdbid)
  350.                 if file.find('//') == 0:
  351.                     error = _(740)
  352.                     LOG(LOG_ERROR, error)
  353.                     return (False, error)
  354.                 
  355.                 filename = globals.EncodeLocale(os.path.basename(file))
  356.                 filenameurl = filename
  357.                 if not os.path.exists(file):
  358.                     error = _(738) % (file,)
  359.                     LOG(LOG_ERROR, error)
  360.                     return (False, error)
  361.                 else:
  362.                     hash = globals.hashFile(file)
  363.                     if hash == 'IOError' or hash == 'SizeError':
  364.                         error = _(739) % (file, hash)
  365.                         LOG(LOG_ERROR, error)
  366.                         return (False, error)
  367.                     
  368.                     videofilesize = os.path.getsize(file)
  369.                     linkhtml_index = 'search/moviebytesize-' + str(videofilesize) + '/moviehash-' + hash
  370.                     videofilename = filename
  371.                     pathvideofilename = file
  372.                     videohash = hash
  373.                     hashresult = {
  374.                         'hash': hash,
  375.                         'filename': filename,
  376.                         'pathvideofilename': file,
  377.                         'filesize': str(videofilesize),
  378.                         'linkhtml_index': linkhtml_index }
  379.                     searchlist = []
  380.                     searchlist.append({
  381.                         'sublanguageid': language,
  382.                         'moviehash': hashresult['hash'],
  383.                         'moviebytesize': str(hashresult['filesize']) })
  384.                     search = self.server.SearchSubtitles(self.osdb_token, searchlist)
  385.                     if search['data']:
  386.                         for item in search['data']:
  387.                             if item['ISO639']:
  388.                                 flag_image = 'flags/' + item['ISO639'] + '.png'
  389.                             else:
  390.                                 flag_image = '-.png'
  391.                             self.subtitles_imdbid_list.append({
  392.                                 'filename': item['SubFileName'],
  393.                                 'link': item['ZipDownloadLink'],
  394.                                 'language_name': item['LanguageName'],
  395.                                 'language_flag': flag_image,
  396.                                 'language_id': item['SubLanguageID'],
  397.                                 'ID': item['IDSubtitle'],
  398.                                 'rating': str(int(item['SubRating'][0])),
  399.                                 'format': item['SubFormat'],
  400.                                 'sync': True })
  401.                         
  402.                         self.subtitles_list.append(self.subtitles_imdbid_list)
  403.                         message = _(742) % (str(len(self.subtitles_imdbid_list)),)
  404.                         LOG(LOG_INFO, message)
  405.                         return (True, message)
  406.                     else:
  407.                         message = _(741)
  408.                         LOG(LOG_INFO, message)
  409.                         return (True, message)
  410.         except Exception:
  411.             e = None
  412.             error = _(731) % (_(736), str(e))
  413.             LOG(LOG_ERROR, error)
  414.             return (False, error)
  415.  
  416.  
  417.     
  418.     def searchsubtitlesbyname(self, name, language = 'all'):
  419.         self.subtitles_name_list = []
  420.         self.allow_exception = False
  421.         search_url = ''
  422.         
  423.         try:
  424.             LOG(LOG_INFO, 'Searching subtitles by name for ' + name)
  425.             if language == 'all':
  426.                 search_url = BASE_URL_SEARCH_ALL % (os.path.basename(name),)
  427.             else:
  428.                 search_url = BASE_URL_SEARCH % (language, os.path.basename(name))
  429.             search_url.replace(' ', '+')
  430.             LOG(LOG_INFO, search_url)
  431.             socket = urllib.urlopen(search_url)
  432.             result = socket.read()
  433.             socket.close()
  434.             xmldoc = minidom.parseString(result)
  435.             subtitles = xmldoc.getElementsByTagName('subtitle')
  436.             if subtitles:
  437.                 url_base = xmldoc.childNodes[0].childNodes[1].firstChild.data
  438.                 for subtitle in subtitles:
  439.                     filename = ''
  440.                     movie = ''
  441.                     lang_name = ''
  442.                     subtitle_id = ''
  443.                     lang_id = ''
  444.                     flag_image = ''
  445.                     link = ''
  446.                     if subtitle.getElementsByTagName('releasename')[0].firstChild:
  447.                         filename = subtitle.getElementsByTagName('releasename')[0].firstChild.data
  448.                     
  449.                     if subtitle.getElementsByTagName('format')[0].firstChild:
  450.                         format = subtitle.getElementsByTagName('format')[0].firstChild.data
  451.                         filename = filename + '.' + format
  452.                     
  453.                     if subtitle.getElementsByTagName('movie')[0].firstChild:
  454.                         movie = subtitle.getElementsByTagName('movie')[0].firstChild.data
  455.                     
  456.                     if subtitle.getElementsByTagName('language')[0].firstChild:
  457.                         lang_name = subtitle.getElementsByTagName('language')[0].firstChild.data
  458.                     
  459.                     if subtitle.getElementsByTagName('idsubtitle')[0].firstChild:
  460.                         subtitle_id = subtitle.getElementsByTagName('idsubtitle')[0].firstChild.data
  461.                     
  462.                     if subtitle.getElementsByTagName('iso639')[0].firstChild:
  463.                         lang_id = subtitle.getElementsByTagName('iso639')[0].firstChild.data
  464.                         flag_image = 'flags/' + lang_id + '.png'
  465.                     
  466.                     if subtitle.getElementsByTagName('download')[0].firstChild:
  467.                         link = subtitle.getElementsByTagName('download')[0].firstChild.data
  468.                         link = url_base + link
  469.                     
  470.                     if subtitle.getElementsByTagName('subrating')[0].firstChild:
  471.                         rating = subtitle.getElementsByTagName('subrating')[0].firstChild.data
  472.                     
  473.                     self.subtitles_name_list.append({
  474.                         'filename': filename,
  475.                         'link': link,
  476.                         'language_name': lang_name,
  477.                         'language_id': lang_id,
  478.                         'language_flag': flag_image,
  479.                         'movie': movie,
  480.                         'ID': subtitle_id,
  481.                         'rating': str(int(rating[0])),
  482.                         'format': format,
  483.                         'sync': False })
  484.                 
  485.                 self.subtitles_list.append(self.subtitles_name_list)
  486.                 message = _(742) % (str(len(self.subtitles_name_list)),)
  487.                 LOG(LOG_INFO, message)
  488.                 return (True, message)
  489.             else:
  490.                 message = _(741)
  491.                 LOG(LOG_INFO, message)
  492.                 return (True, message)
  493.         except Exception:
  494.             e = None
  495.             error = _(743) % (search_url, str(e))
  496.             LOG(LOG_ERROR, error)
  497.             return (False, error)
  498.  
  499.  
  500.  
  501.