SHARE
TWEET

gweana anidb client

a guest Jul 6th, 2015 530 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2. from __future__ import print_function
  3. __author__ = 'Gweana'
  4.  
  5. import socket
  6. import sys
  7. import os
  8. import getpass
  9. import hashlib
  10. import time
  11.  
  12. def hash_file(file_path, size):
  13.     """ Returns the ed2k hash of a given file. """
  14.  
  15.     md4 = hashlib.new('md4').copy
  16.     global cnt
  17.     global total
  18.     def gen(f):
  19.         while True:
  20.             x = f.read(9728000)
  21.             print("[%3d/%3d] %3d%% Hashing %s" % (cnt, total, int((float(f.tell())/float(size))*100.0), os.path.basename(file_path)), end="")
  22.             sys.stdout.flush()
  23.             if x:
  24.                 print("\r", end="")
  25.                 yield x
  26.             else:
  27.                 return
  28.  
  29.     def md4_hash(data):
  30.         m = md4()
  31.         m.update(data)
  32.         return m
  33.  
  34.     with open(file_path, 'rb') as f:
  35.         a = gen(f)
  36.         hashes = [md4_hash(data).digest() for data in a]
  37.         if len(hashes) == 1:
  38.             return hashes[0].encode("hex")
  39.         else: return md4_hash(reduce(lambda a,d: a + d, hashes, "")).hexdigest()
  40.  
  41. username = raw_input("Enter AniDB username: ")
  42. password = getpass.getpass("Password: ")
  43. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  44. sock.connect((socket.gethostbyname_ex("api.anidb.net")[2][0], 9000))
  45. sock.settimeout(3)
  46. sleepTime = 10
  47.  
  48. while 1:
  49.     try:
  50.         sock.send("AUTH user=%s&pass=%s&protover=3&client=gweana&clientver=1" % (username, password))
  51.         ret = sock.recv(1024).split(" ")
  52.         break
  53.     except:
  54.         print ("Timed out! Retry in %d seconds" % sleepTime)
  55.         time.sleep(sleepTime)
  56.         sleepTime *= 2
  57. if ret[0] == "200":
  58.     session = ret[1]
  59. else:
  60.     print (" ".join(ret))
  61.     sock.close()
  62.     exit(1)
  63.  
  64. files = []
  65.  
  66. for item in sys.argv[1:]:
  67.     if not os.path.isdir(item):
  68.         files.append(item)
  69.         continue
  70.     for root, dirnames, filenames in os.walk(os.path.abspath(item)):
  71.         for filename in filenames:
  72.             if filename.endswith((".mkv", ".mp4", ".avi")):
  73.                 files.append(os.path.join(root, filename))
  74.  
  75. ok = 0
  76. cnt = 1
  77. total = len(files)
  78. try:
  79.         for i in files:
  80.                 size = os.path.getsize(i)
  81.                 ed2k = hash_file(i, size)
  82.                 print ("\n               Adding to MyList")
  83.                 while 1:
  84.                         try:
  85.                                 sock.send("MYLISTADD size=%d&ed2k=%s&viewed=1&s=%s" % (size, ed2k, session))
  86.                                 ret = sock.recv(1024).split(" ")
  87.                                 break
  88.                         except: pass
  89.                 if ret[0] == "210":
  90.                         print ("               Successfully added to MyList")
  91.                         ok += 1
  92.                 else:
  93.                         print (" ".join(ret).split("\n")[0])
  94.                 cnt += 1
  95. except:
  96.         sock.send("LOGOUT s=%s" % session)
  97.         print (sock.recv(1024))
  98.         sock.close()
  99.         exit(1)
  100. print ("%d out of %d added successfully" % (ok, total))
  101. sock.send("LOGOUT s=%s" % session)
  102. print (sock.recv(1024))
  103. sock.close()
  104. exit(0)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top