標籤:

創建 ed2k link

創建 ed2k link

#!/usr/bin/pythonimport osimport hashlibdef create_hash(fin, sz): m = hashlib.new("md4") while sz > 0: data = fin.read(min(8192, sz)) m.update(data) sz -= len(data) return m.digest()def create_link(fname): try: fin = open(fname, "rb") fsize = os.stat(fname).st_size if fsize == 0: raise ValueError("Empty File: " + fname) fsz = fsize; hashlist = [] while fsz > 0: sz = min(fsz, 9728000) hashlist.append(create_hash(fin, sz)); fsz -= sz if len(hashlist) > 1: m = hashlib.new("md4") for h in hashlist: m.update(h) fhash = m.hexdigest() else: fhash = hashlist[0].encode("hex") fname = os.path.basename(fname) return "ed2k://|file|%s|%s|%s|/" % (fname, fsize, fhash) except Exception as e: sys.stderr.write(e.message + "
") return ""if __name__ == "__main__": import sys if len(sys.argv) < 2: sys.stderr.write("No argument
") exit(1) for f in sys.argv[1:]: link = create_link(f) if len(link): print(link) exit(0)

輸出如下格式:

ed2k://|file|FileName|FileSize|FileHash|/

不包含 AICH,因為不知道如何優雅地實現~


推薦閱讀:

為什麼都說迅雷吸血?迅雷是怎麼吸血的?
如何將ed2k鏈接轉換為bt種子文件或者http鏈接?
很好奇 ed2k與bt還有magnet他們的區別,他們會不會失效?

TAG:ed2k | Python |