{ "createdAt": "2025-02-13T10:34:59.655Z", "updatedAt": "2025-04-07T07:04:14.000Z", "id": "t9XHS24HKkdH7F7s", "name": "twitter_search", "active": true, "nodes": [ { "parameters": { "jsCode": "// Loop over input items and add a new field called 'myNewField' to the JSON of each one\nlet output_info = {};\nlet text_info = {};\ninfo = JSON.parse($('Webhook').item.json.body.info).full_text.replace(/https:\\/\\/[^\\s]+/g, '').replaceAll(/[ \\t\\n\\r]+/g, ' ').replaceAll(\"\\\"\", '” ');\nconsole.log(info);\nuser_id = JSON.parse($('Webhook').item.json.body.info).id;\ntext_info['full_text'] = info;\noutput_info['text'] = JSON.stringify(text_info);\noutput_info['msg_info'] = info;\noutput_info['user_id'] = user_id;\nreturn output_info;" }, "id": "e829c01b-3921-4eed-98e5-b1dc9436ff03", "name": "get_info", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ -140, 480 ] }, { "parameters": { "operation": "executeQuery", "query": "CALL insert_news_source({{ (JSON.stringify($json.data))}},{{ JSON.stringify(JSON.stringify($json.label_data)) }},{{ JSON.stringify(JSON.stringify($json.artificial_label_data)) }});", "options": {} }, "id": "e01f5445-6df0-4077-8a9e-7ae992795074", "name": "insert_city_news", "type": "n8n-nodes-base.mySql", "typeVersion": 2.4, "position": [ 1000, 940 ], "credentials": { "mySql": { "id": "YpVIiGwzmmkf7vR7", "name": "SL-DB-PRD" } } }, { "parameters": { "jsCode": "// Loop over input items and add a new field called 'myNewField' to the JSON of each one\nresult_data = $input.all();\ncontent_all = {};\nfor(let item_index =0; item_index < result_data.length;item_index++){\n item_data = result_data[item_index].json;\n if('label_data' in item_data){\n content_all['label'] = item_data['label_data'];\n }else if('artificial_label_data' in item_data){\n content_all['artificial_label'] = item_data['artificial_label_data'];\n }\n};\n// console.log(content_all);\ninfo_data_list = JSON.parse($('Webhook').all()[0].json.body.info);\nschedule_id = $('Webhook').all()[0].json.body.schedule_id;\nlabel_data_list = content_all.label;\nartificial_label_data_list = content_all.artificial_label;\nif(artificial_label_data_list.length>0){\n artificial_label_data_list[0]['city_code'] = info_data_list.city_code;;\n}\ninfo_data_list['full_text'] = info_data_list.full_text.replace(/https:\\/\\/[^\\s]+/g, '');\ntranster_answer_data = $('get_transter_answer').all()[0].json.answer.full_text;\ninfo_data_list['full_text_ch'] = transter_answer_data;\noutput_all = [];\noutput_all_data = {};\noutput_news_data_str = \"\";\noutput_news_url_data_str = \"\";\noutput_news_label_data_str = \"\";\nfull_text_new = info_data_list.full_text.replace(/'/g, \"’\").replace(/\"/g, \"”\");\nfull_text_ch_new = info_data_list.full_text_ch.replace(/'/g, \"’\").replace(/\"/g, \"”\");\nnews_item_data = \"\";\nnews_item_data = news_item_data+\"(\";\nnews_item_data += \"'\"+full_text_new+\"'\";\nnews_item_data += ',';\nnews_item_data += \"'\"+full_text_ch_new+\"'\";\nnews_item_data += ',';\nnews_item_data += \"'\"+info_data_list.url+\"'\";\nnews_item_data += ',';\nnews_item_data += \"'\"+info_data_list.name+\"'\";\nnews_item_data += ',';\nnews_item_data += \"'\"+info_data_list.screen_name+\"'\";\nnews_item_data += ',';\nnews_item_data += \"'\"+info_data_list.profile_image_url+\"'\";\nnews_item_data += ',';\nnews_item_data += \"'0'\";\nnews_item_data += ',';\nnews_item_data += \"'\"+info_data_list.favourite_count+\"'\";\nnews_item_data += ',';\nnews_item_data += \"'\"+info_data_list.retweet_count+\"'\";\nnews_item_data += ',';\nnews_item_data += \"'\"+info_data_list.view_count+\"'\";\nnews_item_data += ',';\nnews_item_data += \"'zh_cn'\";\nnews_item_data += ',';\nnews_item_data += 'STR_TO_DATE(\\''+info_data_list.created_at+'\\',\\'%Y-%m-%d %H:%i:%s\\')';\nnews_item_data += ',';\nnews_item_data += \"'\"+info_data_list.id+\"'\";\nnews_item_data += ',';\nnews_item_data += \"'\"+schedule_id+\"'\";\nnews_item_data += ',';\nnews_item_data += \"'twitter_search'\";\nnews_item_data += ',';\nnews_item_data += \"'1'\";\nnews_item_data += ')';\ncity_code = info_data_list.city_code;\nitem_data = \"(\";\nitem_data += \"'\"+city_code+\"'\";\nitem_data += ',';\nitem_data += '@last_insert_id';\nitem_data += '),';\noutput_news_data_str += item_data;\nimages_url = info_data_list.images_url;\nvideos_url = info_data_list.videos_url;\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};\nfor (let i = 0; i < images_url.length; i++) {\n image_url_data = images_url[i];\n original_url = image_url_data.original_url;\n thumbnail_url = image_url_data.thumbnail_url;\n if(isEmptyString(original_url) && isEmptyString(thumbnail_url)){\n continue;\n }else{\n image_item_data = \"(\";\n image_item_data += \"'\"+original_url+\"'\";\n image_item_data += ',';\n if(isEmptyString(thumbnail_url)){\n image_item_data += null;\n }else{\n image_item_data += \"'\"+thumbnail_url+\"'\";\n }\n image_item_data += ',';\n image_item_data += '0';\n image_item_data += ',';\n image_item_data += '@last_insert_id';\n image_item_data += '),';\n output_news_url_data_str += image_item_data;\n }\n};\nfor (let i = 0; i < videos_url.length; i++) {\n video_url = videos_url[i];\n video_item_data = \"(\";\n video_item_data += \"'\"+video_url+\"'\";\n video_item_data += ',';\n video_item_data += null;\n video_item_data += ',';\n video_item_data += '2';\n video_item_data += ',';\n video_item_data += '@last_insert_id';\n video_item_data += '),';\n output_news_url_data_str += video_item_data;\n};\nif(output_news_url_data_str.length == 0){\n output_news_url_data_str = \"('','','',''),\"\n}\noutput_all_data['city_data'] = output_news_data_str.slice(0,output_news_data_str.length-1);\noutput_all_data['urls_data'] = output_news_url_data_str.slice(0,output_news_url_data_str.length-1);\noutput_all_data['news_data'] = news_item_data;\noutput_all_data['news_id'] = info_data_list.id;\noutput_all_data_temp = {};\noutput_all_data_temp['data'] = JSON.stringify(output_all_data);\noutput_all_data_temp['label_data'] = label_data_list;\noutput_all_data_temp['artificial_label_data'] = artificial_label_data_list;\noutput_all.push(output_all_data_temp);\nreturn output_all;" }, "id": "4423a7a8-0a70-4995-b5a5-a1e51e88ebbe", "name": "get_city_news_sql", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 740, 940 ] }, { "parameters": { "operation": "executeQuery", "query": "update news_source set likes = {{ JSON.parse( $('Webhook').item.json.body.info).favourite_count }} ,forward = {{ JSON.parse( $('Webhook').item.json.body.info).retweet_count }},hot = {{ JSON.parse( $('Webhook').item.json.body.info).view_count }} where news_uid = {{ JSON.stringify(JSON.parse( $('Webhook').item.json.body.info).id) }}\n", "options": {} }, "id": "6f2c43b2-39ea-4d24-b04f-6e7ba8e295a0", "name": "update_city_news", "type": "n8n-nodes-base.mySql", "typeVersion": 2.4, "position": [ 480, 1480 ], "credentials": { "mySql": { "id": "YpVIiGwzmmkf7vR7", "name": "SL-DB-PRD" } } }, { "parameters": { "conditions": { "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict" }, "conditions": [ { "id": "a1541edc-9637-4ac2-83a6-902ee9d29af6", "leftValue": "={{ $json.count.toString() }}", "rightValue": "=0", "operator": { "type": "string", "operation": "equals" } } ], "combinator": "and" }, "options": {} }, "id": "e4a2dadb-65d8-45f6-b473-ac28ab739de9", "name": "insert_or_update", "type": "n8n-nodes-base.if", "typeVersion": 2, "position": [ -420, 980 ] }, { "parameters": { "operation": "executeQuery", "query": "select IFNULL(count(id), 0) as count from news_source where news_uid ={{JSON.stringify(JSON.parse($json.body.info).id) }};\n", "options": {} }, "id": "70250832-140c-466f-9c00-64f63c9accde", "name": "is_exists_news", "type": "n8n-nodes-base.mySql", "typeVersion": 2.4, "position": [ -660, 980 ], "alwaysOutputData": false, "credentials": { "mySql": { "id": "YpVIiGwzmmkf7vR7", "name": "SL-DB-PRD" } } }, { "parameters": { "method": "POST", "url": "http://172.31.0.160/v1/completion-messages", "sendHeaders": true, "specifyHeaders": "json", "jsonHeaders": "{\n \"Authorization\": \"Bearer app-KRXKS2lXohYjnIKmuYxSncem\",\n \"Content-Type\": \"application/json\"\n}", "sendBody": true, "specifyBody": "json", "jsonBody": "={\n \"inputs\": {\"record_json\":\"{{$json.text.replaceAll(/\"/g, '\\\\\"')}}\",\"language\":\"中文\"},\n \"response_mode\": \"blocking\",\n \"user\": \"sl_backend_twitter\"\n}", "options": {} }, "id": "f5e0c3a1-71dc-4ea3-b103-3f2d10471c93", "name": "translate_dify", "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [ 40, 480 ] }, { "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 isEmptyString(value) {\n // 方法1: 直接检查是否为空字符串或null\n if (!value || value.trim() === '') {\n return true;\n }\n return false;\n};\nfunction isEmpty(value) {\n // 方法1: 直接检查是否为空字符串或null\n if (!value) {\n return true;\n }\n return false;\n};\nlabel_data_list = [];\nartificial_label_data_list = [];\nlabel_data_arr = $input.all();\nlabel_result_list = [];\nartificial_label_result_list = [];\nfor(var label_index=0;label_index<label_data_arr.length;label_index++){\n label_data_temp = label_data_arr[label_index];\n workflow_id = label_data_temp.json.data.workflow_id;\n label_data_output_list = label_data_temp.json.data.outputs;\n if(workflow_id == '767511d7-ccf6-42c1-a42e-b2c14959c147'){\n if(!isEmpty(label_data_output_list)){\n label_result = label_data_output_list.result;\n for (let i = 0; i < label_result.length; i++) {\n label_result_list.push(label_result[i]);\n }\n }\n }else{\n if(!isEmpty(label_data_output_list)){\n label_result = label_data_output_list.result;\n let label_index_temp = 0;\n for (let i = 0; i < label_result.length&&label_index_temp<1; i++) {\n artificial_label_result_list.push(label_result[i]);\n label_index_temp += 1;\n }\n } \n }\n};\n// console.log(label_result_list);\nfor (let i = 0; i < label_result_list.length; i++) {\n label_code_score = {};\n label_code = label_result_list[i].code;\n label_score = label_result_list[i].score;\n if(!isEmptyString(label_code)){\n label_code_score['code'] = label_code;\n label_code_score['score'] = label_score;\n label_data_list.push(label_code_score);\n }\n};\nfor (let i = 0; i < artificial_label_result_list.length; i++) {\n label_code_score = {};\n label_code = artificial_label_result_list[i].code;\n label_name = artificial_label_result_list[i].name;\n label_score = artificial_label_result_list[i].score;\n if(!isEmptyString(label_code)){\n label_code_score['code'] = label_code;\n label_code_score['name'] = label_name;\n label_code_score['score'] = label_score;\n artificial_label_data_list.push(label_code_score);\n }\n};\nlabel_data_list_2 = [];\nlabel_data_json = {};\nartificial_label_data_json = {};\nlabel_data_json[\"label_data\"] = label_data_list;\nartificial_label_data_json[\"artificial_label_data\"] = artificial_label_data_list;\n// console.log(label_data_json);\nlabel_data_list_2.push(label_data_json);\nlabel_data_list_2.push(artificial_label_data_json);\nreturn label_data_list_2;" }, "id": "00345540-52ea-4185-baa8-9de02ecf0aaf", "name": "get_label_answer", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 1000, 600 ] }, { "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 if (!str.includes('\"}') || (str.includes('\"}') && !str.endsWith('\"}'))) {\n str = str.replaceAll(\"}\",\"\");\n }\n str = retainSingleOccurrence(str,'}');\n // console.log(str);\n let missing = '';\n if (!str.endsWith('\"}')) {\n if (str.endsWith('\"')) {\n missing += '}'\n }else{\n missing += '\"}'\n }\n }\n str = str+missing;\n // console.log(str);\n return str;\n}\nfunction retainSingleOccurrence(str, subStr) {\n // 检查字符串是否包含子字符串\n if (!str.includes(subStr)) {\n return str;\n }\n // 使用正则表达式删除多余的子字符串\n // 使用全局匹配标志 g 和正则表达式来匹配所有子字符串\n // 注意:这里假设子字符串不会与其他文本重叠\n const regex = new RegExp(`(${subStr})(?=.*${subStr})`, 'g');\n let result = str.replace(regex, '');\n return result;\n}\nanswer =$input.all()[0].json.answer;\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": "2ce1807e-dd97-45af-91d7-15c5310f4a7f", "name": "get_transter_answer", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 220, 480 ] }, { "parameters": { "method": "POST", "url": "http://172.31.0.160/v1/workflows/run", "sendHeaders": true, "specifyHeaders": "json", "jsonHeaders": "{\n \"Authorization\": \"Bearer app-KNq0O8kENP4ITqSmqHQ0IzAt\",\n \"Content-Type\": \"application/json\"\n}", "sendBody": true, "specifyBody": "json", "jsonBody": "={\n \"inputs\": {\"msg_info\":\"{{$('get_info').item.json.msg_info.replaceAll(/\"/g, '\\\\\"')}}\"},\n \"response_mode\": \"blocking\",\n \"user\": \"sl_backend_twitter\"\n}", "options": {} }, "id": "779e5be9-c03f-41ab-bb8b-787746c9d15c", "name": "natural_label_dify", "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [ 480, 600 ] }, { "parameters": { "method": "POST", "url": "http://172.31.0.160/v1/workflows/run", "sendHeaders": true, "specifyHeaders": "json", "jsonHeaders": "{\n \"Authorization\": \"Bearer app-HCbsiZmUCzP8Vdqb2meKXQSb\",\n \"Content-Type\": \"application/json\"\n}", "sendBody": true, "specifyBody": "json", "jsonBody": "={\n \"inputs\": {\"msg_info\":\"{{$json.answer.full_text.replaceAll(/\"/g, '\\\\\"')}}\"},\n \"response_mode\": \"blocking\",\n \"user\": \"sl_backend_twitter\"\n}", "options": {} }, "id": "39348acd-530f-47cc-acae-cab6a1793038", "name": "artificial_label_dify", "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [ 480, 400 ] }, { "parameters": {}, "id": "b614c512-d925-4f6b-befb-6b3959c01377", "name": "merge_label", "type": "n8n-nodes-base.merge", "typeVersion": 3, "position": [ 700, 500 ] }, { "parameters": { "jsCode": "// Loop over input items and add a new field called 'myNewField' to the JSON of each one\nresult_data = $input.all();\ninfo_data_list = JSON.parse($('Webhook').all()[0].json.body.info);\noutput_news_data_str = '';\noutput_all_data = {};\noutput_all = [];\noutput_city_code_list = [];\ncity_code = info_data_list.city_code;\noutput_city_code_list.push(city_code);\noutput_all_data['city_code'] = output_city_code_list;\noutput_all_data['news_id'] = info_data_list.id;\noutput_all.push(output_all_data);\nconsole.log(output_all);\nreturn output_all;" }, "id": "6cb86ad1-2b0e-4b4f-9d0a-09851419fc72", "name": "get_city_code_info", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 480, 1200 ] }, { "parameters": { "operation": "executeQuery", "query": "CALL insert_news_city({{ JSON.stringify(JSON.stringify($json.city_code))}},{{ JSON.stringify(JSON.stringify($json.news_id)) }});", "options": {} }, "id": "03c554a7-ffb4-4f54-b95b-3ed071e79645", "name": "insert_city_data", "type": "n8n-nodes-base.mySql", "typeVersion": 2.4, "position": [ 740, 1200 ], "credentials": { "mySql": { "id": "YpVIiGwzmmkf7vR7", "name": "SL-DB-PRD" } } }, { "parameters": {}, "id": "764927cc-b964-4fd8-bf0b-0ac66f131972", "name": "get_answer_location", "type": "n8n-nodes-base.merge", "typeVersion": 3, "position": [ 480, 940 ] }, { "parameters": { "httpMethod": "POST", "path": "twitter/search", "authentication": "headerAuth", "options": { "allowedOrigins": "*", "rawBody": false } }, "id": "1ed229b2-7a41-4ac8-baf9-6f27b8667350", "name": "Webhook", "type": "n8n-nodes-base.webhook", "typeVersion": 2, "position": [ -920, 980 ], "webhookId": "e495a29c-8f43-4175-bdb8-277e03b60b07", "credentials": { "httpHeaderAuth": { "id": "U87Wvnvu9oQ5Zapo", "name": "Header Auth account" } } }, { "parameters": { "operation": "executeQuery", "query": "CALL record_craw_zh_counts({{JSON.stringify(($json.body.info.parseJson().removeField(\"full_text\").toJsonString()))}},{{JSON.stringify(JSON.stringify($json.body.schedule_id)) }});", "options": {} }, "id": "3c92c7f7-c7f4-4258-a5aa-536fd69eaf15", "name": "record_zh_info", "type": "n8n-nodes-base.mySql", "typeVersion": 2.4, "position": [ -660, 720 ], "credentials": { "mySql": { "id": "YpVIiGwzmmkf7vR7", "name": "SL-DB-PRD" } } } ], "connections": { "get_info": { "main": [ [ { "node": "translate_dify", "type": "main", "index": 0 } ] ] }, "get_city_news_sql": { "main": [ [ { "node": "insert_city_news", "type": "main", "index": 0 } ] ] }, "insert_or_update": { "main": [ [ { "node": "get_info", "type": "main", "index": 0 }, { "node": "get_answer_location", "type": "main", "index": 1 } ], [ { "node": "update_city_news", "type": "main", "index": 0 }, { "node": "get_city_code_info", "type": "main", "index": 0 } ] ] }, "is_exists_news": { "main": [ [ { "node": "insert_or_update", "type": "main", "index": 0 } ] ] }, "translate_dify": { "main": [ [ { "node": "get_transter_answer", "type": "main", "index": 0 } ] ] }, "get_label_answer": { "main": [ [ { "node": "get_answer_location", "type": "main", "index": 0 } ] ] }, "get_transter_answer": { "main": [ [ { "node": "artificial_label_dify", "type": "main", "index": 0 }, { "node": "natural_label_dify", "type": "main", "index": 0 } ] ] }, "natural_label_dify": { "main": [ [ { "node": "merge_label", "type": "main", "index": 1 } ] ] }, "artificial_label_dify": { "main": [ [ { "node": "merge_label", "type": "main", "index": 0 } ] ] }, "merge_label": { "main": [ [ { "node": "get_label_answer", "type": "main", "index": 0 } ] ] }, "get_city_code_info": { "main": [ [ { "node": "insert_city_data", "type": "main", "index": 0 } ] ] }, "get_answer_location": { "main": [ [ { "node": "get_city_news_sql", "type": "main", "index": 0 } ] ] }, "Webhook": { "main": [ [ { "node": "is_exists_news", "type": "main", "index": 0 }, { "node": "record_zh_info", "type": "main", "index": 0 } ] ] } }, "settings": { "executionOrder": "v1" }, "staticData": null, "meta": { "templateCredsSetupCompleted": true }, "pinData": {}, "versionId": "154c2c09-0457-4642-82a6-d1a60126ff23", "triggerCount": 1, "shared": [ { "createdAt": "2025-02-13T10:34:59.666Z", "updatedAt": "2025-02-13T10:34:59.666Z", "role": "workflow:owner", "workflowId": "t9XHS24HKkdH7F7s", "projectId": "C7zKoCDdmGgzUqSW", "project": { "createdAt": "2024-08-23T06:31:21.275Z", "updatedAt": "2024-08-23T06:41:43.608Z", "id": "C7zKoCDdmGgzUqSW", "name": "周 卫 <zhouwei@ssish.com>", "type": "personal", "projectRelations": [ { "createdAt": "2024-08-23T06:31:21.275Z", "updatedAt": "2024-08-23T06:31:21.275Z", "role": "project:personalOwner", "userId": "3f0021b4-d0a7-4db6-af21-5ef829c47882", "projectId": "C7zKoCDdmGgzUqSW", "user": { "createdAt": "2024-08-23T06:31:16.119Z", "updatedAt": "2025-03-19T06:19:26.514Z", "id": "3f0021b4-d0a7-4db6-af21-5ef829c47882", "email": "zhouwei@ssish.com", "firstName": "周", "lastName": "卫", "personalizationAnswers": { "companyType": "education", "role": "business-owner", "automationBeneficiary": "my-team", "companySize": "20-99", "reportedSource": "friend", "version": "v4", "personalization_survey_submitted_at": "2024-08-23T06:57:52.856Z", "personalization_survey_n8n_version": "1.55.3" }, "settings": { "userActivated": true, "firstSuccessfulWorkflowId": "s6zsznwVs6N4L2Hx", "userActivatedAt": 1725957335278, "npsSurvey": { "responded": true, "lastShownAt": 1742365155724 }, "isOnboarded": true }, "role": "global:owner", "disabled": false, "mfaEnabled": false, "isPending": false, "isOwner": true } } ] } } ], "tags": [] }