"jsCode":"// Loop over input items and add a new field called 'myNewField' to the JSON of each one\nlet output_info = {};\nlet title_info = {};\nlet summary_info = {};\ntitle = JSON.parse($input.item.json.body.info).title;\nsummary = JSON.parse($input.item.json.body.info).summary;\n// content = JSON.parse($input.item.json.body.info).content;\ntitle_info['title'] = title;\nsummary_info['summary'] = summary;\n//text_info['content'] = content;\noutput_info['title_text'] = JSON.stringify(title_info);\noutput_info['summary_text'] = JSON.stringify(summary_info);\nreturn output_info;"
},
"id":"6408b6a8-b646-4a3b-aa21-6c5336c6725a",
"name":"get_info",
"type":"n8n-nodes-base.code",
"typeVersion":2,
"position":[
0,
520
]
},
{
"parameters":{
"jsCode":"// Loop over input items and add a new field called 'myNewField' to the JSON of each one\n//answer = \"```json{\\\"test1\\\":\\\"test\\\"}```\";\nfunction findAllIndexes(str, charToFind) {\n const regex = new RegExp(charToFind, 'g');\n let indexes = [];\n let result;\n while ((result = regex.exec(str)) !== null) {\n indexes.push(result.index);\n }\n return indexes;\n}\nfunction isValidJSON(str) {\n try {\n JSON.parse(str);\n return true;\n } catch (e) {\n return false;\n }\n}\nfunction autoCompleteJSON(str) {\n if (isValidJSON(str)) {\n return str;\n }\n let missing = '';\n if (!str.endsWith('\"}')) {\n missing += '\"}'\n }\n str = str+missing;\n return str;\n};\nfunction isEmptyString(value) {\n // 方法1: 直接检查是否为空字符串或null\n if (!value || value.trim() == '' || value.trim() == 'null' || value.trim() == 'None') {\n return true;\n }\n return false;\n};\nresult_data = $input.all();\ncontent_all = {};\nanswer_data = {};\nanswer_data_list = {};\nfor(let item_index =0; item_index < result_data.length;item_index++){\n item_data = result_data[item_index].json;\n if('answer' in item_data){\n answer_data_str = item_data['answer'];\n answer_json_data = get_answer_data(answer_data_str);\n title = answer_json_data.title;\n summary = answer_json_data.summary;\n if(!isEmptyString(title)){\n answer_data_list['title'] = title;\n }else if(!isEmptyString(summary)){\n answer_data_list['summary'] = summary;\n }\n }\n};\nfunction get_answer_data(answer){\n answer = answer.replaceAll(/\\n/g, '').replaceAll(/%s| /g, '');\n indexes = findAllIndexes(answer,\"```\");\n answer_str = '';\n if(indexes.length>0){\n answer_str = answer.slice(indexes[0]+7, indexes[1]);\n }else{\n answer_str = answer;\n }\n answer_str = answer_str.replace(/\\s+/g, '');\n answer_data_json = JSON.parse(autoCompleteJSON(answer_str));\n return answer_data_json;\n};\nanswer_data[\"answer\"] = answer_data_list;\nreturn answer_data;"
},
"id":"ca7c4b97-d310-44c8-a480-7c2498c114ea",
"name":"get_answer",
"type":"n8n-nodes-base.code",
"typeVersion":2,
"position":[
720,
480
]
},
{
"parameters":{
"operation":"executeQuery",
"query":"START TRANSACTION;\n\nINSERT INTO news_source (topic,summary,source_url,author,publish_time,news_uid,status,language,news_web,news_type) VALUES {{ $json.news_data }};\n\nSET @last_insert_id = LAST_INSERT_ID();\n\nINSERT INTO news_url (url,thumbnail_url, type,news_id)\nVALUES {{ $json.urls_data }};\n\nCOMMIT;",