Python批量生成ED2K链接脚本
import os import hashlib
def calculate_ed2k(): directory = input('请输入目录路径:') if not os.path.isdir(directory): print('目录不存在!') return for filename in os.listdir(directory): if not os.path.isfile(os.path.join(directory, filename)): continue filesize = os.path.getsize(os.path.join(directory, filename)) ed2k_hash = calculate_hash(os.path.join(directory, filename)) ed2k_link = 'ed2k://|file|{}|{}|{}|h={}|/'.format(filename, filesize, ed2k_hash, calculate_aich(ed2k_hash)) print(ed2k_link)
def calculate_hash(filepath): BLOCKSIZE = 9728000 hasher = hashlib.md5() with open(filepath, 'rb') as file: buf = file.read(BLOCKSIZE) while len(buf) > 0: hasher.update(buf) buf = file.read(BLOCKSIZE) return hasher.hexdigest()
def calculate_aich(ed2k_hash): BLOCKSIZE = 9728000 hasher = hashlib.sha1() hasher.update(ed2k_hash.encode('utf-8')) buf = hasher.digest() while len(buf) > 16: hasher = hashlib.sha1() hasher.update(buf) buf = hasher.digest() return buf.hex()
if name == 'main': calculate_ed2k()
原文地址: https://www.cveoy.top/t/topic/mCab 著作权归作者所有。请勿转载和采集!