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

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. import sys
  5. import os
  6. import xbmc
  7. import xbmcgui
  8. import xbmcplugin
  9. import threading
  10. import osdb
  11. from osdb import OSDBServer
  12. from utilities import *
  13. import socket
  14. import urllib
  15. import unzip
  16.  
  17. try:
  18.     current_dlg_id = xbmcgui.getCurrentWindowDialogId()
  19. except:
  20.     current_dlg_id = 0
  21.  
  22. current_win_id = xbmcgui.getCurrentWindowId()
  23. _ = sys.modules['__main__'].__language__
  24. __scriptname__ = sys.modules['__main__'].__scriptname__
  25. __version__ = sys.modules['__main__'].__version__
  26. STATUS_LABEL = 100
  27. LOADING_IMAGE = 110
  28. SUBTITLES_LIST = 120
  29. OSDB_SERVER = 'http://www.opensubtitles.org/xml-rpc'
  30.  
  31. class GUI(xbmcgui.WindowXMLDialog):
  32.     socket.setdefaulttimeout(5.0)
  33.     
  34.     def __init__(self, *args, **kwargs):
  35.         pass
  36.  
  37.     
  38.     def set_filepath(self, path):
  39.         LOG(LOG_INFO, 'set_filepath [%s]', path)
  40.         self.file_original_path = path
  41.         self.file_path = path[path.find(os.sep):len(path)]
  42.  
  43.     
  44.     def set_filehash(self, hash):
  45.         LOG(LOG_INFO, 'set_filehash [%s]', hash)
  46.         self.file_hash = hash
  47.  
  48.     
  49.     def set_filesize(self, size):
  50.         LOG(LOG_INFO, 'set_filesize [%s]', size)
  51.         self.file_size = size
  52.  
  53.     
  54.     def set_searchstring(self, search):
  55.         LOG(LOG_INFO, 'set_searchstring [%s]', search)
  56.         self.search_string = search
  57.  
  58.     
  59.     def onInit(self):
  60.         LOG(LOG_INFO, 'onInit')
  61.         self.setup_all()
  62.         if self.file_path or self.search_string:
  63.             self.connThread = threading.Thread(target = self.connect, args = ())
  64.             self.connThread.start()
  65.         
  66.  
  67.     
  68.     def setup_all(self):
  69.         self.setup_variables()
  70.  
  71.     
  72.     def setup_variables(self):
  73.         self.controlId = -1
  74.         self.allow_exception = False
  75.         self.osdb_server = OSDBServer()
  76.         self.osdb_server.Create()
  77.         if xbmc.Player().isPlayingVideo():
  78.             self.set_filepath(xbmc.Player().getPlayingFile())
  79.         
  80.  
  81.     
  82.     def connect(self):
  83.         self.getControl(LOADING_IMAGE).setVisible(True)
  84.         self.getControl(STATUS_LABEL).setLabel(_(646))
  85.         (ok, msg) = self.osdb_server.connect(OSDB_SERVER, '', '')
  86.         if not ok:
  87.             self.getControl(STATUS_LABEL).setLabel(_(653))
  88.             self.getControl(LOADING_IMAGE).setVisible(False)
  89.             return None
  90.         else:
  91.             self.getControl(STATUS_LABEL).setLabel(_(635))
  92.         self.osdb_server.getlanguages()
  93.         self.search_subtitles()
  94.         self.getControl(LOADING_IMAGE).setVisible(False)
  95.         self.getControl(STATUS_LABEL).setVisible(False)
  96.  
  97.     
  98.     def search_subtitles(self):
  99.         
  100.         try:
  101.             if len(self.file_path) > 0:
  102.                 LOG(LOG_INFO, _(642) % (os.path.basename(self.file_path),))
  103.                 self.getControl(STATUS_LABEL).setLabel(_(642) % ('...',))
  104.                 self.set_filehash(xbmc.getFileHash(self.file_original_path))
  105.                 self.set_filesize(xbmc.getFileSize(self.file_original_path))
  106.                 (ok, msg) = self.osdb_server.searchsubtitles(self.file_path, self.file_hash, self.file_size)
  107.                 LOG(LOG_INFO, msg)
  108.             
  109.             if len(self.search_string) > 0:
  110.                 LOG(LOG_INFO, _(642) % (os.path.basename(self.search_string),))
  111.                 self.getControl(STATUS_LABEL).setLabel(_(642) % ('......',))
  112.                 (ok2, msg2) = self.osdb_server.searchsubtitlesbyname(self.search_string)
  113.                 LOG(LOG_INFO, msg2)
  114.             
  115.             self.osdb_server.mergesubtitles()
  116.             if not ok and not ok2:
  117.                 self.getControl(STATUS_LABEL).setLabel(_(634) % (msg,))
  118.             elif self.osdb_server.subtitles_list:
  119.                 for item in self.osdb_server.subtitles_list:
  120.                     listitem = xbmcgui.ListItem(label = item['language_name'], label2 = item['filename'], iconImage = item['rating'], thumbnailImage = item['language_flag'])
  121.                     if item['sync']:
  122.                         listitem.setProperty('sync', 'true')
  123.                     else:
  124.                         listitem.setProperty('sync', 'false')
  125.                     self.getControl(SUBTITLES_LIST).addItem(listitem)
  126.                 
  127.             
  128.             self.setFocus(self.getControl(SUBTITLES_LIST))
  129.             self.getControl(SUBTITLES_LIST).selectItem(0)
  130.         except Exception:
  131.             e = None
  132.             error = _(634) % ('search_subtitles:' + str(e))
  133.             LOG(LOG_ERROR, error)
  134.             return (False, error)
  135.  
  136.  
  137.     
  138.     def show_control(self, controlId):
  139.         self.getControl(STATUS_LABEL).setVisible(controlId == STATUS_LABEL)
  140.         self.getControl(SUBTITLES_LIST).setVisible(controlId == SUBTITLES_LIST)
  141.         page_control = controlId == STATUS_LABEL
  142.         
  143.         try:
  144.             self.setFocus(self.getControl(controlId + page_control))
  145.         except:
  146.             self.setFocus(self.getControl(controlId))
  147.  
  148.  
  149.     
  150.     def file_download(self, url, dest):
  151.         dp = xbmcgui.DialogProgress()
  152.         dp.create(__scriptname__, _(633), os.path.basename(dest))
  153.         
  154.         try:
  155.             urllib.urlretrieve(url, dest, (lambda nb, bs, fs, url = url: self._pbhook(nb, bs, fs, url, dp)))
  156.             return (True, 'Downloaded')
  157.         except Exception:
  158.             e = None
  159.             error = _(634) % str(e)
  160.             LOG(LOG_ERROR, error)
  161.             return (False, error)
  162.  
  163.  
  164.     
  165.     def _pbhook(self, numblocks, blocksize, filesize, url = None, dp = None):
  166.         
  167.         try:
  168.             percent = min(numblocks * blocksize * 100 / filesize, 100)
  169.             LOG(LOG_INFO, 'download precent %s' % (precent,))
  170.             dp.update(percent)
  171.         except:
  172.             percent = 100
  173.             dp.update(percent)
  174.  
  175.         if dp.iscanceled():
  176.             LOG(LOG_INFO, 'Subtitle download cancelled')
  177.             dp.close()
  178.         
  179.  
  180.     
  181.     def download_subtitles(self, pos):
  182.         LOG(LOG_INFO, 'download_subtitles')
  183.         if self.osdb_server.subtitles_list:
  184.             subtitle_set = False
  185.             filename = self.osdb_server.subtitles_list[pos]['filename']
  186.             subtitle_format = self.osdb_server.subtitles_list[pos]['format']
  187.             url = self.osdb_server.subtitles_list[pos]['link']
  188.             zip_filename = filename[0:filename.rfind('.')] + '.zip'
  189.             zip_filename = xbmc.translatePath(os.path.join('special://home/subtitles', zip_filename))
  190.             sub_filename = os.path.basename(self.file_path)
  191.             sub_filename = sub_filename[0:sub_filename.rfind('.')] + '.' + self.osdb_server.subtitles_list[pos]['language_id'] + '.' + self.osdb_server.subtitles_list[pos]['format']
  192.             local_path = 'special://home/subtitles'
  193.             ok = xbmcgui.Dialog().yesno(__scriptname__, _(242), _(243) % (filename,), '', _(260), _(259))
  194.             if not ok:
  195.                 self.getControl(STATUS_LABEL).setLabel(_(645))
  196.                 return None
  197.             
  198.             self.getControl(STATUS_LABEL).setLabel(_(649))
  199.             LOG(LOG_INFO, _(632) % (zip_filename, url))
  200.             (ok, msg) = self.file_download(url, zip_filename)
  201.             if not ok:
  202.                 self.getControl(STATUS_LABEL).setLabel(msg)
  203.                 return None
  204.             else:
  205.                 ok = self.extract_subtitles(filename, sub_filename, subtitle_format, zip_filename, local_path)
  206.         
  207.  
  208.     
  209.     def extract_subtitles(self, filename, sub_filename, subtitle_format, zip_filename, local_path):
  210.         LOG(LOG_INFO, 'extract_subtitles')
  211.         
  212.         try:
  213.             un = unzip.unzip()
  214.             files = un.get_file_list(zip_filename)
  215.             if len(files) == 2:
  216.                 if filename not in files:
  217.                     for item in files:
  218.                         if item.find(subtitle_format) > 0:
  219.                             filename = item
  220.                             break
  221.                             continue
  222.                     
  223.                 
  224.                 self.getControl(STATUS_LABEL).setLabel(_(650))
  225.                 LOG(LOG_INFO, _(631) % (zip_filename, local_path))
  226.                 un.extract_file(zip_filename, filename, sub_filename, local_path)
  227.                 LOG(LOG_INFO, _(644) % local_path)
  228.                 self.getControl(STATUS_LABEL).setLabel(_(651))
  229.                 if xbmc.Player().isPlayingVideo():
  230.                     xbmc.Player().setSubtitles(os.path.join(local_path, sub_filename))
  231.                     subtitle_set = True
  232.                 
  233.             else:
  234.                 self.getControl(STATUS_LABEL).setLabel(_(650))
  235.                 LOG(LOG_INFO, _(631) % (zip_filename, local_path))
  236.                 un.extract(zip_filename, local_path)
  237.                 LOG(LOG_INFO, _(644) % local_path)
  238.                 self.getControl(STATUS_LABEL).setLabel(_(651))
  239.                 if xbmc.Player().isPlayingVideo():
  240.                     for item in files:
  241.                         if item.find(subtitle_format) > 0:
  242.                             xbmc.Player().setSubtitles(os.path.join(local_path, item))
  243.                             subtitle_set = True
  244.                             continue
  245.                     
  246.         except Exception:
  247.             e = None
  248.             error = _(634) % str(e)
  249.             LOG(LOG_ERROR, error)
  250.         
  251.  
  252.         self.getControl(STATUS_LABEL).setLabel(_(652))
  253.         if subtitle_set:
  254.             os.remove(zip_filename)
  255.             xbmc.showNotification(652, '', '')
  256.             self.exit_script()
  257.         
  258.  
  259.     
  260.     def exit_script(self, restart = False):
  261.         self.connThread.join()
  262.         self.close()
  263.  
  264.     
  265.     def onClick(self, controlId):
  266.         if self.controlId == SUBTITLES_LIST:
  267.             self.download_subtitles(self.getControl(SUBTITLES_LIST).getSelectedPosition())
  268.         
  269.  
  270.     
  271.     def onFocus(self, controlId):
  272.         self.controlId = controlId
  273.  
  274.     
  275.     def onAction(self, action):
  276.         if action.getButtonCode() in CANCEL_DIALOG:
  277.             self.exit_script()
  278.         
  279.  
  280.  
  281.