From d6230d1c8cafce2b85d539f729519bfb2bc92931 Mon Sep 17 00:00:00 2001 From: Ching Date: Thu, 28 Mar 2024 11:41:58 +0800 Subject: [PATCH] refactor: Fix file path issue in mas2ff2.py --- mas2ff2.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/mas2ff2.py b/mas2ff2.py index ec87b88..355551a 100644 --- a/mas2ff2.py +++ b/mas2ff2.py @@ -15,6 +15,9 @@ sentry_sdk.init( # cookie = "u=%7EBVoC1S9tgw4; uuid=24edfe42401cbb03cf1f.1676446846.20; PHPSESSID=4ev7ef0f9cim18lrest8l52kn6; m=looo.ching%40gmail.com; tgw_l7_route=f174d6f255742a6ee2e9a07cf9cca0fa" # token = "9927e23b" logger.add('/root/develop/log/mas2ff-hegui.log', level='INFO') + +file_path = os.path.dirname(os.path.abspath(__file__)) + def post_fanfou(cookie, token, fanfou_status, images=None): if not images: images = [] @@ -94,15 +97,16 @@ def post_fanfou(cookie, token, fanfou_status, images=None): status = fanfou_status + ' ' + image_count % (x+1) if image_resp.status_code == 200: # 创建一个临时文件 - with open('temp.jpg', 'wb') as file: + temp_file = file_path + '/temp.jpg' + with open(temp_file, 'wb') as file: file.write(response.content) # 确保文件存在 - if os.path.exists('temp.jpg'): - with open('temp.jpg', 'rb') as file: + if os.path.exists(temp_file): + with open(temp_file, 'rb') as file: # 以下是您的multipart/form-data请求部分 files = { - 'picture': ('temp.jpg', file, 'image/jpeg') # 注意这里仍然需要指定MIME类型 + 'picture': (temp_file, file, 'image/jpeg') # 注意这里仍然需要指定MIME类型 } data = { 'desc': status, @@ -134,7 +138,8 @@ def run(instance, access_token, fanfou_cookie, fanfou_token): # first time run posted_ids = [] # if file not exist, create it - with open('last_id.txt', 'r') as f: + last_id_file = file_path + '/last_id.txt' + with open(last_id_file, 'r') as f: # read lines posted_ids = f.readlines() # remove \n @@ -159,7 +164,7 @@ def run(instance, access_token, fanfou_cookie, fanfou_token): content = re.sub('<.*?>', '', content) logger.info(status['id'] + ' ' + content) post_fanfou(fanfou_cookie, fanfou_token, content, status['media_attachments']) - with open('last_id.txt', 'a') as f: + with open(last_id_file, 'a') as f: f.write(str(status['id']) + '\n') posted_ids.append(status['id']) logger.info('write toot %s', status['content']) @@ -169,7 +174,7 @@ def run(instance, access_token, fanfou_cookie, fanfou_token): min_id = statuses[-1]['id'] else: if min_id: - with open('last_id.txt', 'a') as f: + with open(last_id_file, 'a') as f: f.write(str(min_id) + '\n') min_id = None