From 2e4f930004dceedad1dc2ec0f2b49018daf9cedd Mon Sep 17 00:00:00 2001 From: jinye_huang Date: Thu, 8 May 2025 23:35:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BA=86=E6=8E=A8=E6=96=87?= =?UTF-8?q?=E7=BB=93=E6=9E=9C=E7=9A=84=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/tweet_generator.py | 35 +++++++++++------------------------ 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/utils/tweet_generator.py b/utils/tweet_generator.py index d4befeb..aa2bef8 100644 --- a/utils/tweet_generator.py +++ b/utils/tweet_generator.py @@ -58,14 +58,12 @@ class tweetContent: self.variant_index = variant_index try: - self.title, self.content, self.tag = self.split_content(result) - self.json_data = self.gen_result_json() + self.json_data = self.split_content(result) + except Exception as e: logging.error(f"Failed to parse AI result for {article_index}_{variant_index}: {e}") logging.debug(f"Raw result: {result[:500]}...") # Log partial raw result - self.title = "" # 改为空字符串,而不是 "[Parsing Error]" - self.content = "" # 改为空字符串,而不是 "[Failed to parse AI content]" - self.json_data = {"title": self.title, "content": self.content, "error": True} # 不再包含raw_result + self.json_data = {"title": "", "content": "", "tag": "", "error": True, "raw_result": e} # 不再包含raw_result def split_content(self, result): # Assuming split logic might still fail, keep it simple or improve with regex/json @@ -83,31 +81,20 @@ class tweetContent: try: processed_result = result if "" in result: - processed_result = result.split("", 1)[1] # Take part after + processed_result = result.split("")[1] # Take part after # 以json 格式输出 json_data = json.loads(processed_result) - title = json_data.get("title") - content = json_data.get("content") - tag = json_data.get("tag") + json_data["error"] = False + json_data["raw_result"] = None + return json_data # --- End Existing Logic --- - return title.strip(), content.strip(), tag.strip() + except Exception as e: logging.warning(f"解析内容时出错: {e}, 返回空字符串") - return "", "" - - def gen_result_json(self): - json_file = { - "title": self.title, - "content": self.content, - "tag": self.tag - } - # Add error flag if it exists - if hasattr(self, 'json_data') and self.json_data.get('error'): - json_file['error'] = True - json_file['raw_result'] = self.json_data.get('raw_result') - logging.info(f"Generated JSON: {json_file}") - return json_file + json_data["error"] = True + json_data["raw_result"] = e + return json_data def get_json_data(self): """Returns the generated JSON data dictionary."""