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.py < prev    next >
Encoding:
Python Source  |  2009-11-25  |  10.2 KB  |  249 lines

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