import hashlib import time # -*- coding: utf-8 -*- def tranlate(source): import requests import json url = "https://www.fanyigou.com/TranslateApi/api/trans" content = 'appid=1709180548417&from=cht&from=cht&nonce_str=ibuaiVcKdpRxkhJA&privatekey=2c46cc266d4909a6a30527f93c424f186d5ba90e&text=' + source + '&to=en' print(content) token = md5_encrypt(content).upper() print(token) response = requests.request("POST", url, data={ 'appid': '1709180548417', 'token': token, 'nonce_str': 'ibuaiVcKdpRxkhJA', 'from': 'cht', 'text': source, 'to': 'en', }) print(response.text) return json.loads(response.text)['data'] def md5_encrypt(text): # 创建一个MD5对象 md5 = hashlib.md5() # 将文本转换为字节类型并更新到MD5对象中 md5.update(text.encode('utf-8')) # 获取加密后的结果(16位十六进制表示) encrypted_result = md5.hexdigest() return encrypted_result def spider(filepath, suffix): import os code_files = [] for root, dirs, files in os.walk(filepath, topdown=False): for f in files: file = os.path.join(root, f) if file.endswith(suffix): code_files.append(file.replace("\\", "/")) return code_files def message(code_files): msgs = [] for i in code_files: fp = open(i, 'r', encoding='utf-8') data = fp.readlines() for line in data: curr_code = line.strip('\n') if curr_code is None: curr_code = line.strip('\r\n') msgs.append(curr_code) return msgs if __name__ == '__main__': code_files = spider('/Users/liaozetao/Downloads/message', '.txt') msgs = message(code_files) for m in msgs: try: print(m) if len(m) == 0: continue arr = m.split(', ') str1: str = '' if len(arr) > 0: str1 = arr[0] str2: str = '' if len(arr) > 1: str2 = arr[1] if len(str2) == 0: continue else: str2 = str2.split(')')[0] str2 = str2.replace('"', '') content = tranlate(str2) print(str1, ',', content) time.sleep(1) break except Exception as e: print(e) continue