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 / globals.py next >
Encoding:
Python Source  |  2009-07-20  |  7.0 KB  |  253 lines

  1. from xmlrpclib import Server,Transport
  2. from httplib import HTTP
  3. import locale
  4. import os
  5. from urlparse import urlparse
  6. from types import UnicodeType
  7. import re
  8. import struct
  9. import base64
  10. import pickle
  11. ##import imdb
  12. import urllib
  13.  
  14. #TESTS
  15. defaultavi = "" 
  16. disable_osdb = True
  17. debugmode = True
  18. use_threads = True
  19. check_update = True
  20. doing_login = False
  21.  
  22. #CONFIGS
  23. videos_ext = ["avi","mpg","mpeg","wmv","divx","mkv","ogm","asf", "mov", "rm", "vob", "dv", "3ivx"]
  24. subs_ext = ["srt","sub","txt","ssa"]
  25. videos_wildcards = "All videos|*.avi;*.mpg;*.mpeg;*.wmv;*.asf;*.divx;*.mov;*.m2p;*.moov;*.omf;*.qt;*.rm;*.vob;*.dat;*.dv;*.3ivx;*.mkv;*.ogm|ALL files (*.*)|*.*"
  26.     
  27. LOCAL_ENCODING = locale.getpreferredencoding();
  28. if not LOCAL_ENCODING or LOCAL_ENCODING == "ANSI_X3.4-1968":
  29.     LOCAL_ENCODING = 'latin1';
  30.  
  31. version = "1.2.9"
  32. date_released = "16-04-07"
  33.  
  34.  
  35. update_address = "http://subdownloader.sourceforge.net/subdownloader.update"
  36.  
  37.  
  38. preferences_list = {}
  39. sublanguages = {}
  40. update_list = {}
  41.  
  42. ##imdbserver = imdb.IMDb()
  43.  
  44. #GLOBAL NEEDED(don't change)
  45. cookiefile = "conf/.cookie"
  46. sourcefolder = ""
  47. app_parameteres = ""
  48.  
  49. param_function = ""
  50. param_files = []
  51.  
  52.  
  53. osdb_token = ""
  54. logged_as = ""
  55. user_has_uploaded = False
  56. xmlrpc_server = ""
  57. proxy_address = None
  58. text_log = False
  59.  
  60. subdownloader_pointer = None
  61.  
  62. class GtkTransport (Transport):
  63.     user_agent = "Subdownloader " + version 
  64.     
  65. class ProxiedTransport(Transport):
  66.     user_agent = "Subdownloader " + version 
  67.     def set_proxy(self, proxy):
  68.         self.proxy = proxy
  69.     def make_connection(self, host):
  70.         self.realhost = host
  71.     h = HTTP(self.proxy)
  72.     return h
  73.     def send_request(self, connection, handler, request_body):
  74.         connection.putrequest("POST", 'http://%s%s' % (self.realhost, handler))
  75.     def send_host(self, connection, host):
  76.         connection.putheader('Host', self.realhost)
  77.     
  78. def MakeOSLink(pagelink,sid=True):
  79.     if preferences_list["server_osdb"].endswith("/"):
  80.     serverosdb = preferences_list["server_osdb"][:-1]
  81.     else:
  82.     serverosdb = preferences_list["server_osdb"]
  83.     
  84.     parsed = urlparse(serverosdb)
  85.     internallink = ""
  86.     sidlink = ""
  87.     if pagelink:
  88.     internallink = "/" + pagelink
  89.     if sid and logged_as:
  90.     sidlink = "/sid-" + osdb_token
  91.     link = parsed[0] + "://" + parsed[1] + internallink + sidlink
  92.     return link 
  93.  
  94. def CleanString(s):
  95.     garbage = ['_',' ','.',',','(',')']
  96.     for char in garbage:
  97.     s = s.replace(char,'')
  98.     return s
  99.  
  100. def CleanTagsFile(texto):
  101.     p = re.compile( '<.*?>')
  102.     return p.sub('',texto)
  103.  
  104. def DeleteExtension(file):
  105.     lastpointposition = file.rfind(".")
  106.     return os.path.basename(file[:lastpointposition])
  107.         
  108. def GetFpsAndTimeMs(path):
  109.     fps = ""
  110.     time_ms = ""
  111.     try: 
  112.         avi_info = parse(path)
  113.         if avi_info.video[0].fps > 0:
  114.            fps = avi_info.video[0].fps
  115.  
  116.         if avi_info.length > 0:
  117.             time_ms = avi_info.length * 1000
  118.             
  119.         return fps,time_ms 
  120.     except:
  121.        return fps,time_ms 
  122.         
  123.  
  124.     
  125. def Log(message):
  126.     string_message = str(message)
  127.     subdownloader_pointer.log_memory.append(string_message)
  128.         
  129.     if text_log:
  130.     try:
  131.         for line in subdownloader_pointer.log_memory:
  132.         text_log.AppendText("\n" + line)
  133.     except:
  134.         pass
  135.     
  136.     subdownloader_pointer.log_memory = []
  137.     
  138.     
  139. def hashFile(name):
  140.     try:
  141.         
  142.         longlongformat = 'LL'  # signed long, unsigned long
  143.         bytesize = struct.calcsize(longlongformat)
  144.         
  145. ##        f = file(name, "rb")
  146.         f = open(name, "rb")
  147.         
  148.         filesize = os.path.getsize(name)
  149.         hash = filesize
  150.         
  151.         #print struct.calcsize(longlongformat)
  152.         if filesize < 65536 * 2:
  153.             return "SizeError"
  154.         
  155.         for x in range(65536/bytesize):
  156.             buffer = f.read(bytesize)
  157.             (l2, l1)= struct.unpack(longlongformat, buffer)
  158.             l_value = (long(l1) << 32) | long(l2)
  159.             hash += l_value
  160.             hash = hash & 0xFFFFFFFFFFFFFFFF #to remain as 64bit number
  161.             #if x < 20 : print "%016x" % hash
  162.             
  163.         
  164.         f.seek(max(0,filesize-65536),0)
  165.         for x in range(65536/bytesize):
  166.             buffer = f.read(bytesize)
  167.             (l2, l1) = struct.unpack(longlongformat, buffer)
  168.             l_value = (long(l1) << 32) | long(l2)
  169.             hash += l_value
  170.             hash = hash & 0xFFFFFFFFFFFFFFFF
  171.     
  172.         f.close()
  173.         returnedhash =  "%016x" % hash
  174.         return returnedhash
  175.         
  176.     except(IOError):
  177.         return "IOError"
  178.     
  179.  
  180. def EncodeLocale(phrase):
  181.     if not isinstance(phrase, UnicodeType):
  182.             phrase = unicode(phrase.decode("latin1","ignore"))
  183.     return phrase
  184.  
  185. def ConvertLang_xxx2xx(str):
  186.     return sublanguages["languages_id_xxx"][str][0]
  187. def ConvertLang_xxx2name(str):
  188.     return sublanguages["languages_id_xxx"][str][1]
  189. def ConvertLang_xx2name(str):
  190.     return sublanguages["languages_id_xxx"][sublanguages["languages_xx2xxx"][str]][1]
  191. def ConvertLang_xx2xxx(str):
  192.     return sublanguages["languages_xx2xxx"][str]
  193. def ConvertLang_name2xxx(str):
  194.     return sublanguages["languages_name2xxx"][str]
  195. def ConvertLang_name2xx(str):
  196.     return sublanguages["languages_id_xxx"][sublanguages["languages_name2xxx"][str]][0]
  197.     
  198. def DownloadNewTranslations(lang_app_xx):
  199.     try:
  200.         mo_translation_coded = xmlrpc_server.GetTranslation(osdb_token, lang_app_xx, "mo" )["data"]
  201.         po_translation_coded = xmlrpc_server.GetTranslation(osdb_token, lang_app_xx, "po" )["data"]
  202.         po_translation = base64.decodestring(po_translation_coded)
  203.         mo_translation = base64.decodestring(mo_translation_coded)
  204.         if not os.path.exists(os.path.join(sourcefolder,"locale",lang_app_xx)):
  205.         os.mkdir(os.path.join(sourcefolder,"locale",lang_app_xx))
  206.         os.mkdir(os.path.join(sourcefolder,"locale",lang_app_xx,"LC_MESSAGES"))
  207.                   
  208.         mofile = file(os.path.join(sourcefolder,"locale",lang_app_xx,"LC_MESSAGES","subdownloader.mo"),'wb')
  209.         mofile.write(mo_translation)
  210.         mofile.close()
  211.         pofile = file(os.path.join(sourcefolder,"locale",lang_app_xx,"LC_MESSAGES","subdownloader.po"),'wb')
  212.         pofile.write(po_translation)
  213.         pofile.close()
  214.         #current_translations_dates[lang_app_xx] = new_translations_dates[lang_app_xx]
  215.         #pickle.dump(current_translations_dates,file(os.path.join(sourcefolder,"conf/.translations"),"wb"))
  216.         return True
  217.     except:
  218.         return False
  219.     
  220. def getAddress(file):
  221.     filename = EncodeLocale(os.path.basename(file))
  222.     filenameurl = (filename) #Corrects the accent characters.
  223.     
  224.     if not os.path.exists(file):
  225.         return "NotFoundError"
  226.     else:
  227.         hash = hashFile(file)
  228.         if hash == "IOError" or hash== "SizeError":
  229.             return hash
  230.         
  231.         #We keep going if there was no error.
  232.         
  233.         videofilesize = os.path.getsize(file)
  234.         linkhtml_index =  "search/moviebytesize-"+str(videofilesize)+"/moviehash-"+hash
  235.         videofilename = filename
  236.         pathvideofilename = file
  237.         videohash = hash
  238.         return {"hash":hash, "filename":filename, "pathvideofilename":file, "filesize":str(videofilesize)
  239.             , "linkhtml_index":linkhtml_index}
  240.  
  241. ##def DownloadCover(imdb_id):
  242. ##    try:
  243. ##        movie = imdbserver.get_movie(imdb_id)
  244. ##        cover_file = os.path.join(sourcefolder,"images","covers",imdb_id + ".gif")
  245. ##        if not os.path.exists(cover_file):
  246. ##        cover_url = movie['cover url']
  247. ##        urllib.urlretrieve(cover_url,cover_file)
  248. ##        Log("Image dowloaded from imdb : %s" %cover_url)
  249. ##        else:
  250. ##        Log("Image already found in : %s" %cover_file)
  251. ##    except:
  252. ##        Log("Error loading the cover IMDB image")
  253.