Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- from __future__ import print_function
- __author__ = 'Gweana'
- import socket
- import sys
- import os
- import getpass
- import hashlib
- import time
- def hash_file(file_path, size):
- """ Returns the ed2k hash of a given file. """
- md4 = hashlib.new('md4').copy
- global cnt
- global total
- def gen(f):
- while True:
- x = f.read(9728000)
- print("[%3d/%3d] %3d%% Hashing %s" % (cnt, total, int((float(f.tell())/float(size))*100.0), os.path.basename(file_path)), end="")
- sys.stdout.flush()
- if x:
- print("\r", end="")
- yield x
- else:
- return
- def md4_hash(data):
- m = md4()
- m.update(data)
- return m
- with open(file_path, 'rb') as f:
- a = gen(f)
- hashes = [md4_hash(data).digest() for data in a]
- if len(hashes) == 1:
- return hashes[0].encode("hex")
- else: return md4_hash(reduce(lambda a,d: a + d, hashes, "")).hexdigest()
- username = raw_input("Enter AniDB username: ")
- password = getpass.getpass("Password: ")
- sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- sock.connect((socket.gethostbyname_ex("api.anidb.net")[2][0], 9000))
- sock.settimeout(3)
- sleepTime = 10
- while 1:
- try:
- sock.send("AUTH user=%s&pass=%s&protover=3&client=gweana&clientver=1" % (username, password))
- ret = sock.recv(1024).split(" ")
- break
- except:
- print ("Timed out! Retry in %d seconds" % sleepTime)
- time.sleep(sleepTime)
- sleepTime *= 2
- if ret[0] == "200":
- session = ret[1]
- else:
- print (" ".join(ret))
- sock.close()
- exit(1)
- files = []
- for item in sys.argv[1:]:
- if not os.path.isdir(item):
- files.append(item)
- continue
- for root, dirnames, filenames in os.walk(os.path.abspath(item)):
- for filename in filenames:
- if filename.endswith((".mkv", ".mp4", ".avi")):
- files.append(os.path.join(root, filename))
- ok = 0
- cnt = 1
- total = len(files)
- try:
- for i in files:
- size = os.path.getsize(i)
- ed2k = hash_file(i, size)
- print ("\n Adding to MyList")
- while 1:
- try:
- sock.send("MYLISTADD size=%d&ed2k=%s&viewed=1&s=%s" % (size, ed2k, session))
- ret = sock.recv(1024).split(" ")
- break
- except: pass
- if ret[0] == "210":
- print (" Successfully added to MyList")
- ok += 1
- else:
- print (" ".join(ret).split("\n")[0])
- cnt += 1
- except:
- sock.send("LOGOUT s=%s" % session)
- print (sock.recv(1024))
- sock.close()
- exit(1)
- print ("%d out of %d added successfully" % (ok, total))
- sock.send("LOGOUT s=%s" % session)
- print (sock.recv(1024))
- sock.close()
- exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement