Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket, sys, time,hashlib,os
- from os import path
- from time import sleep
- ##############
- ### README ###
- ##############
- ## After editing the prefs below to suit your needs, simply drag and drop
- ## your files onto this script to add them to your anidb mylist.
- ## (or supply their full paths as arguments.)
- ## This script recursively adds any files in folders and subfolders
- ## that you drag + drop. You can also drop multiple files/folders on it at once.
- ##############
- ### PREFS ####
- ##############
- USERNAME = 'username'
- PASSWORD = 'password'
- LOCALPORT = 1234
- STATE = 1 # on hdd
- ## 0 - unknown - state is unknown or the user doesn't want to provide this information
- ## 1 - on hdd - the file is stored on hdd (but is not shared)
- ## 2 - on cd - the file is stored on cd
- ## 3 - deleted - the file has been deleted or is not available for other reasons (i.e. reencoded)
- VIEWED = 1
- OUTPUT = 1 # if set to 0, this script will not create the text files it normally uses for outputting results.
- ###############
- ## hash code ##
- ###############
- direc = path.split(path.abspath(sys.argv[0]))[0]
- def hash_file(file_path):
- """ Returns the ed2k hash of a given file. """
- md4 = hashlib.new('md4').copy
- def gen(f):
- while True:
- x = f.read(9728000)
- if x: 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:
- md4 = hashes[0].encode("hex")
- else: md4 = md4_hash(reduce(lambda a,d: a + d, hashes, "")).hexdigest()
- f.close()
- return 'ed2k://|file|'+path.split(file_path)[1]+'|'\
- +str(path.getsize(file_path))+'|'+str(md4)+'|'
- ##############
- ### SETUP ####
- ##############
- def setup():
- global s
- host = 'api.anidb.net'
- textport = "9000"
- s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- port = int(textport)
- s.bind(('',LOCALPORT))#bind to local port
- s.connect((host, port))
- ##############
- #### AUTH ####
- ##############
- # returns session key
- def auth():
- data = 'AUTH user='+USERNAME+'&pass='+PASSWORD+'&protover=3&client=pyquickadd&clientver=1'
- s.sendall(data)
- SK=None
- while 1:
- buf = s.recv(2048)
- if not len(buf):
- break
- print "Received: %s" % buf
- if str(buf).split(' ')[0] != '200':
- if OUTPUT:
- f=open(path.join(direc,'anidb_debug.txt'),'a')
- f.write('AUTH ERROR: '+str(buf))
- f.close()
- return None
- SK=str(buf).split(' ')[1]
- break
- return SK
- ##############
- #### ADD #####
- ##############
- def add(full_ed2k,SK):
- ed2k=full_ed2k.split('|')[4]
- filesize=full_ed2k.split('|')[3]
- data = 'MYLISTADD size='+str(filesize)+'&ed2k='+ed2k+'&state='+str(STATE)+'&viewed='+str(VIEWED)+'&s='+SK
- s.sendall(data)
- SK='' #session key
- while 1:
- buf = s.recv(2048)
- if not len(buf):
- break
- print "Received: %s" % buf
- if str(buf).split(' ')[0] !='210':
- return buf
- break
- return None
- ##############
- #### EDIT ####
- ##############
- def edit(full_ed2k,SK):
- ed2k=full_ed2k.split('|')[4]
- filesize=full_ed2k.split('|')[3]
- data = 'MYLISTADD size='+str(filesize)+'&ed2k='+ed2k+'&state='+str(STATE)+'&viewed='+str(VIEWED)+'&edit=1&s='+SK
- s.sendall(data)
- SK='' #session key
- while 1:
- buf = s.recv(2048)
- if not len(buf):
- break
- print "Received: %s" % buf
- if str(buf).split(' ')[0] !='311':
- return buf
- break
- return None
- ##############
- ### LOGOUT ###
- ##############
- def logout(SK):
- data = 'LOGOUT s='+SK
- s.sendall(data)
- while 1:
- buf = s.recv(2048)
- if not len(buf):
- break
- print "Received: %s" % buf
- break
- ## end ##
- s.close()
- ###################
- ## recurse files ##
- ###################
- def recursefiles(ifiles):
- files=[]
- for f in ifiles:
- if path.isdir(f):
- subs = os.listdir(f)
- newFiles=[]
- for a in zip([f]*len(subs),subs):
- newFiles.append(path.join(a[0],a[1]))
- files+=recursefiles(newFiles)
- else:
- files.append(f)
- return files
- ##################
- ## actual stuff ##
- ##################
- def addfiles(ifiles):
- # if OUTPUT:
- # f=open(path.join(direc,'critial error in pyquickadd code'),'w')
- # f.close()
- files=recursefiles(ifiles)
- ed2ks = []
- for arg in files:
- ed2ks.append(hash_file(arg))
- for i in range(4):
- try:
- setup()
- break
- except:
- sleep(2)
- SK = auth()
- if SK==None:
- if OUTPUT:
- f=open(path.join(direc,'anidb_failed_adds.txt'),'a')
- for full_ed2k in ed2ks:
- f.write('AUTH ERROR\n'+full_ed2k+'\n')
- f.close()
- else:
- for full_ed2k in ed2ks:
- print full_ed2k
- sleep(3)
- result = add(full_ed2k,SK)
- if (result != None) and (result.split(' ')[0] == '310'):
- sleep(3)
- result = edit(full_ed2k,SK)
- #try editing then
- if OUTPUT:
- if result!=None:
- f=open(path.join(direc,'anidb_failed_adds.txt'),'a')
- f.write(str(result)+full_ed2k+'\n')
- f.close()
- else:
- f=open(path.join(direc,'anidb_successful_adds.txt'),'a')
- f.write(full_ed2k+'\n')
- f.close()
- sleep(3)
- logout(SK)
- # if OUTPUT:
- # os.remove(path.join(direc,'critial error in pyquickadd code'))
- if __name__ == '__main__':
- addfiles(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement