"jsCode":"// Loop over input items and add a new field called 'myNewField' to the JSON of each one\nlet output_info = {};\nlet text_info = {};\ntitle = JSON.parse($input.item.json.body.info).title;\ncontent = JSON.parse($input.item.json.body.info).content;\ntext_info['title'] = title;\n//text_info['content'] = content;\noutput_info['text'] = JSON.stringify(text_info);\nreturn output_info;"
},
"id":"9584a3ec-017f-4f42-adb5-ecdfaa9cead6",
"name":"get_info",
"type":"n8n-nodes-base.code",
"typeVersion":2,
"position":[
140,
280
]
},
{
"parameters":{
"mode":"runOnceForEachItem",
"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}\n// answer = $input.item.json.data.outputs.result.answer;\n// answer = $input.item.json.data.answer;\nanswer = $input.item.json.answer;\nanswer = answer.replaceAll(/\\n/g, '').replaceAll(/%s| /g, '');\nindexes = findAllIndexes(answer,\"```\");\nanswer_str = '';\nif(indexes.length>0){\n answer_str = answer.slice(indexes[0]+7, indexes[1]);\n}else{\n answer_str = answer;\n}\nanswer_str = answer_str.replace(/\\s+/g, '');\nanswer_data_json = JSON.parse(autoCompleteJSON(answer_str));\nanswer_data = {};\nanswer_data[\"answer\"] = answer_data_json;\nreturn answer_data;"
},
"id":"2d1dc138-7daf-4ed5-8d42-d5cd9ca46c1e",
"name":"get_answer",
"type":"n8n-nodes-base.code",
"typeVersion":2,
"position":[
680,
280
]
},
{
"parameters":{
"operation":"executeQuery",
"query":"START TRANSACTION;\n\nINSERT INTO news_source (topic,content,source_url,news_uid,status,language,snapshot,news_web,news_type) VALUES {{ $json.news_data }};\n\nSET @last_insert_id = LAST_INSERT_ID();\n\nINSERT INTO news_url (url, type,news_id)\nVALUES {{ $json.urls_data }};\n\nCOMMIT;",