Advertisement
Guest User

Untitled

a guest
Jul 7th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.93 KB | None | 0 0
  1. import socket, sys, time,hashlib,os
  2. from os import path
  3. from time import sleep
  4. ##############
  5. ### README ###
  6. ##############
  7. ## After editing the prefs below to suit your needs, simply drag and drop
  8. ## your files onto this script to add them to your anidb mylist.
  9. ##  (or supply their full paths as arguments.)
  10. ## This script recursively adds any files in folders and subfolders
  11. ## that you drag + drop. You can also drop multiple files/folders on it at once.
  12. ##############
  13. ### PREFS ####
  14. ##############
  15. USERNAME = 'username'
  16. PASSWORD = 'password'
  17. LOCALPORT = 1234
  18. STATE = 1 # on hdd
  19. ## 0 - unknown - state is unknown or the user doesn't want to provide this information
  20. ## 1 - on hdd - the file is stored on hdd (but is not shared)
  21. ## 2 - on cd - the file is stored on cd
  22. ## 3 - deleted - the file has been deleted or is not available for other reasons (i.e. reencoded)
  23. VIEWED = 1
  24. OUTPUT = 1 # if set to 0, this script will not create the text files it normally uses for outputting results.
  25. ###############
  26. ## hash code ##
  27. ###############
  28. direc = path.split(path.abspath(sys.argv[0]))[0]
  29. def hash_file(file_path):
  30.     """ Returns the ed2k hash of a given file. """
  31.  
  32.     md4 = hashlib.new('md4').copy
  33.  
  34.     def gen(f):
  35.         while True:
  36.             x = f.read(9728000)
  37.             if x: yield x
  38.             else: return
  39.  
  40.     def md4_hash(data):
  41.         m = md4()
  42.         m.update(data)
  43.         return m
  44.     with open(file_path, 'rb') as f:
  45.         a = gen(f)
  46.         hashes = [md4_hash(data).digest() for data in a]
  47.         if len(hashes) == 1:
  48.             md4 = hashes[0].encode("hex")
  49.         else: md4 = md4_hash(reduce(lambda a,d: a + d, hashes, "")).hexdigest()
  50.     f.close()
  51.     return 'ed2k://|file|'+path.split(file_path)[1]+'|'\
  52.         +str(path.getsize(file_path))+'|'+str(md4)+'|'
  53. ##############
  54. ### SETUP ####
  55. ##############
  56. def setup():
  57.     global s
  58.     host = 'api.anidb.net'
  59.     textport = "9000"
  60.  
  61.     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  62.     port = int(textport)
  63.     s.bind(('',LOCALPORT))#bind to local port
  64.     s.connect((host, port))
  65.  
  66. ##############
  67. #### AUTH ####
  68. ##############
  69. # returns session key
  70. def auth():
  71.     data = 'AUTH user='+USERNAME+'&pass='+PASSWORD+'&protover=3&client=pyquickadd&clientver=1'
  72.     s.sendall(data)
  73.     SK=None
  74.     while 1:
  75.         buf = s.recv(2048)
  76.         if not len(buf):
  77.             break
  78.         print "Received: %s" % buf
  79.         if str(buf).split(' ')[0] != '200':
  80.             if OUTPUT:
  81.                 f=open(path.join(direc,'anidb_debug.txt'),'a')
  82.                 f.write('AUTH ERROR: '+str(buf))
  83.                 f.close()
  84.             return None
  85.         SK=str(buf).split(' ')[1]
  86.         break
  87.     return SK
  88. ##############
  89. #### ADD #####
  90. ##############
  91. def add(full_ed2k,SK):
  92.     ed2k=full_ed2k.split('|')[4]
  93.     filesize=full_ed2k.split('|')[3]
  94.     data = 'MYLISTADD size='+str(filesize)+'&ed2k='+ed2k+'&state='+str(STATE)+'&viewed='+str(VIEWED)+'&s='+SK
  95.     s.sendall(data)
  96.     SK='' #session key
  97.     while 1:
  98.         buf = s.recv(2048)
  99.         if not len(buf):
  100.             break
  101.         print "Received: %s" % buf
  102.         if str(buf).split(' ')[0] !='210':
  103.             return buf
  104.         break
  105.     return None
  106. ##############
  107. #### EDIT ####
  108. ##############
  109. def edit(full_ed2k,SK):
  110.     ed2k=full_ed2k.split('|')[4]
  111.     filesize=full_ed2k.split('|')[3]
  112.     data = 'MYLISTADD size='+str(filesize)+'&ed2k='+ed2k+'&state='+str(STATE)+'&viewed='+str(VIEWED)+'&edit=1&s='+SK
  113.     s.sendall(data)
  114.     SK='' #session key
  115.     while 1:
  116.         buf = s.recv(2048)
  117.         if not len(buf):
  118.             break
  119.         print "Received: %s" % buf
  120.         if str(buf).split(' ')[0] !='311':
  121.             return buf
  122.         break
  123.     return None
  124. ##############
  125. ### LOGOUT ###
  126. ##############
  127. def logout(SK):
  128.     data = 'LOGOUT s='+SK
  129.     s.sendall(data)
  130.     while 1:
  131.         buf = s.recv(2048)
  132.         if not len(buf):
  133.             break
  134.         print "Received: %s" % buf
  135.         break
  136.     ## end ##
  137.     s.close()
  138. ###################
  139. ## recurse files ##
  140. ###################
  141. def recursefiles(ifiles):
  142.     files=[]
  143.     for f in ifiles:
  144.         if path.isdir(f):
  145.             subs = os.listdir(f)
  146.             newFiles=[]
  147.             for a in zip([f]*len(subs),subs):
  148.                 newFiles.append(path.join(a[0],a[1]))
  149.             files+=recursefiles(newFiles)
  150.         else:
  151.             files.append(f)
  152.     return files
  153. ##################
  154. ## actual stuff ##
  155. ##################
  156. def addfiles(ifiles):
  157. #    if OUTPUT:
  158. #        f=open(path.join(direc,'critial error in pyquickadd code'),'w')
  159. #        f.close()
  160.     files=recursefiles(ifiles)
  161.     ed2ks = []
  162.     for arg in files:
  163.         ed2ks.append(hash_file(arg))
  164.     for i in range(4):
  165.         try:
  166.             setup()
  167.             break
  168.         except:
  169.             sleep(2)
  170.     SK = auth()
  171.     if SK==None:
  172.         if OUTPUT:
  173.             f=open(path.join(direc,'anidb_failed_adds.txt'),'a')
  174.             for full_ed2k in ed2ks:
  175.                 f.write('AUTH ERROR\n'+full_ed2k+'\n')
  176.             f.close()
  177.     else:
  178.         for full_ed2k in ed2ks:
  179.             print full_ed2k
  180.             sleep(3)
  181.             result = add(full_ed2k,SK)
  182.             if (result != None) and (result.split(' ')[0] == '310'):
  183.                 sleep(3)
  184.                 result = edit(full_ed2k,SK)
  185.                 #try editing then
  186.             if OUTPUT:
  187.                 if result!=None:
  188.                     f=open(path.join(direc,'anidb_failed_adds.txt'),'a')
  189.                     f.write(str(result)+full_ed2k+'\n')
  190.                     f.close()
  191.                 else:
  192.                     f=open(path.join(direc,'anidb_successful_adds.txt'),'a')
  193.                     f.write(full_ed2k+'\n')
  194.                     f.close()
  195.         sleep(3)
  196.         logout(SK)
  197. #    if OUTPUT:
  198. #        os.remove(path.join(direc,'critial error in pyquickadd code'))
  199.  
  200. if __name__ == '__main__':
  201.     addfiles(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement