SHARE
TWEET

Untitled

a guest Jan 12th, 2013 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from xmlrpclib import ServerProxy, Error
  2. import argparse
  3. import urllib2
  4. import struct
  5. import os
  6.  
  7. opensub_domain = "http://api.opensubtitles.org/xml-rpc"
  8. default_useragent = "OS Test User Agent"
  9.  
  10. server = ServerProxy(opensub_domain)
  11.  
  12. def get_hash(name):
  13.     try:
  14.         longlongformat = 'q'  # long long
  15.         bytesize = struct.calcsize(longlongformat)
  16.  
  17.         f = open(name, "rb")
  18.  
  19.         filesize = os.path.getsize(name)
  20.         hash = filesize
  21.  
  22.         if filesize < 65536 * 2:
  23.             return "SizeError"
  24.  
  25.         for x in range(65536 / bytesize):
  26.             buffer = f.read(bytesize)
  27.             (l_value,) = struct.unpack(longlongformat, buffer)
  28.             hash += l_value
  29.             hash = hash & 0xFFFFFFFFFFFFFFFF  # to remain as 64bit number
  30.  
  31.         f.seek(max(0, filesize - 65536), 0)
  32.         for x in range(65536 / bytesize):
  33.             buffer = f.read(bytesize)
  34.             (l_value,) = struct.unpack(longlongformat, buffer)
  35.             hash += l_value
  36.             hash = hash & 0xFFFFFFFFFFFFFFFF
  37.  
  38.         f.close()
  39.         returnedhash = "%016x" % hash
  40.         return returnedhash
  41.  
  42.     except(IOError):
  43.         return None
  44.  
  45.  
  46. if __name__ == '__main__':
  47.     parser = argparse.ArgumentParser()
  48.     parser.add_argument("file", type=str,
  49.                         help="input video file")
  50.     args = parser.parse_args()
  51.     #args.file
  52.     current_hash = get_hash(args.file)
  53.     current_size = os.path.getsize(args.file)
  54.     session = server.LogIn("", "", "en", default_useragent)
  55.     token = session["token"]
  56.     searchlist = []
  57.     searchlist.append({'moviehash': current_hash, 'moviebytesize': str(current_size)})
  58.     moviesList = server.SearchSubtitles(token, searchlist)
  59.     if moviesList['data']:
  60.         data = moviesList['data']
  61.         print data[0]['MovieName']
  62.     else:
  63.         print 'no info'
RAW Paste Data
Top