{
  "name": "My workflow",
  "nodes": [
    {
      "parameters": {},
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        0
      ],
      "id": "313f17cc-10a9-4aad-af47-96095495e651",
      "name": "When clicking \u2018Execute workflow\u2019"
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "value": "1nD6W0V0eCp9PcVuyycp3fjPMmdfFSzTd0_OlX2sVqZc",
          "mode": "list",
          "cachedResultName": "competitors",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1nD6W0V0eCp9PcVuyycp3fjPMmdfFSzTd0_OlX2sVqZc/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "\u041b\u0438\u0441\u04421",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1nD6W0V0eCp9PcVuyycp3fjPMmdfFSzTd0_OlX2sVqZc/edit#gid=0"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.6,
      "position": [
        220,
        0
      ],
      "id": "cccaeb82-ecde-4807-b089-20d871c0b8ba",
      "name": "ListOfStores",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const input = $input.all();\nconst result = {};\nfor (const item of input){\n  const name = item.json[\"Brand name\"];\n  const inst = item.json[\"instagram\"];\n  if(inst && inst.trim() !== \"\"){\n    result[name] = inst;\n  }\n}\nreturn [{ json: result }];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        440,
        0
      ],
      "id": "5478e341-50a9-4d83-8163-40cf062ddfa6",
      "name": "MapStoresInstagram"
    },
    {
      "parameters": {
        "jsCode": "const input = items[0].json;\nconst result = [];\n\nfor (const [brand, instagram] of Object.entries(input)) {\n  result.push({ json: { brand, instagram } });\n}\n\nreturn result;"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        660,
        0
      ],
      "id": "54c5206c-b38d-44d8-b882-8c39b36e3e13",
      "name": "ArrayToShopItems"
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        880,
        0
      ],
      "id": "a96f236b-6cdc-448a-a209-a43aaa8f4699",
      "name": "LoopOverBrands"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://puppeteer:3000/run",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "username",
              "value": "shmyrovyury"
            },
            {
              "name": "password",
              "value": "jjj2FGRretail@"
            },
            {
              "name": "profileUrl",
              "value": "https://www.instagram.com/zestypaws/"
            }
          ]
        },
        "options": {
          "timeout": 1800000
        }
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1120,
        100
      ],
      "id": "39628687-b078-4368-97fb-806df89434ff",
      "name": "Calling Instagram-parser"
    },
    {
      "parameters": {
        "jsCode": "const item = items[0].json;\n\nconst reelsMap = new Map(\n  item.reels.map(r => [`https://www.instagram.com${r.href}`, r.views])\n);\n\nconst updatedPostInfo = {};\n\nfor (const [url, info] of Object.entries(item.postInfo)) {\n  updatedPostInfo[url] = {\n    ...info,\n    views: reelsMap.get(url) || '\u043d\u0435 \u0440\u0438\u043b\u0441'\n  };\n}\n\nreturn [\n  {\n    json: {\n      ...item,\n      postInfo: updatedPostInfo\n    }\n  }\n];\n"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1460,
        100
      ],
      "id": "8f707204-34c8-460f-aa29-0f02f26deac3",
      "name": "PostInfoEnricher"
    },
    {
      "parameters": {
        "batchSize": "=1",
        "options": {}
      },
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        3380,
        160
      ],
      "id": "279fb4e4-b5b6-4bf1-be56-7a8a9a4a7d8d",
      "name": "Loop Over Items"
    },
    {
      "parameters": {
        "jsCode": "const postInfo = $input.first().json.postInfo;\nconst output = [];\n\nfor (const [url, data] of Object.entries(postInfo)) {\n  output.push({\n    json: {\n      url,\n      description: data.description,\n      datetime: data.datetime,\n      views: data.views\n    }\n  });\n}\n\nreturn output;"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        2000,
        100
      ],
      "id": "ca5a4c9e-d4cc-45ee-a025-c0332e0055c2",
      "name": "SplitPostInfoToSingle"
    },
    {
      "parameters": {
        "modelName": "models/gemini-2.0-flash",
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "typeVersion": 1,
      "position": [
        3860,
        480
      ],
      "id": "3c4d5740-4c52-4930-9126-7f59c7e5c0ef",
      "name": "Google Gemini Chat Model",
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=\u0421\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c summery \u0434\u043b\u044f \u043f\u043e\u043b\u044f {{ $json.description }}. \u041d\u0435 \u0431\u043e\u043b\u0435\u0435 10 \u0441\u043b\u043e\u0432 \u043e \u0447\u0435\u043c \u043f\u043e\u0441\u0442. \u041d\u0430 \u0442\u043e\u043c \u0436\u0435 \u044f\u0437\u044b\u043a\u0435 \u0447\u0442\u043e \u0438 \u0441\u0430\u043c \u043f\u043e\u0441\u0442.",
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 2,
      "position": [
        3860,
        240
      ],
      "id": "4b3a192b-76c4-4f2f-abe3-77355af7f63a",
      "name": "SummeryAgent",
      "notes": "\u0412\u0435\u0440\u043d\u0443\u0442\u044c summery"
    },
    {
      "parameters": {},
      "type": "n8n-nodes-base.merge",
      "typeVersion": 3.2,
      "position": [
        1780,
        380
      ],
      "id": "821f8b17-b95b-491d-8ba2-3c281d86a799",
      "name": "Merge"
    },
    {
      "parameters": {},
      "type": "n8n-nodes-base.merge",
      "typeVersion": 3.2,
      "position": [
        4000,
        620
      ],
      "id": "2d7fb9ae-1259-4d47-8918-51fefa6884a1",
      "name": "MergeAiAnswerToItem"
    },
    {
      "parameters": {
        "jsCode": "const [item1, item2] = items;\n\nlet outputItem = item1.json.output ? item1 : item2;\nlet dataItem = item1.json.output ? item2 : item1;\n\nreturn [\n  {\n    json: {\n      summary: outputItem.json.output?.trim(),\n      url: dataItem.json.url,\n      description: dataItem.json.description,\n      datetime: dataItem.json.datetime,\n      views: dataItem.json.views\n    }\n  }\n];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        4220,
        620
      ],
      "id": "73cf2c26-7dc8-4839-9bdd-d984a86f2c68",
      "name": "MergeToOneItem"
    },
    {
      "parameters": {
        "jsCode": "return [\n  {\n    json: {\n      allPosts: items.map(item => item.json)\n    }\n  }\n];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        2640,
        380
      ],
      "id": "e033bf0f-09d1-4efa-9d62-53b0a2638eb0",
      "name": "Code"
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "1nD6W0V0eCp9PcVuyycp3fjPMmdfFSzTd0_OlX2sVqZc",
          "mode": "list",
          "cachedResultName": "competitors",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1nD6W0V0eCp9PcVuyycp3fjPMmdfFSzTd0_OlX2sVqZc/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": "={{ $json.url.split('/').slice(0, 4).join('/') }}",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "summary": "={{ $json.summary }}",
            "url": "={{ $json.url }}",
            "description": "={{ $json.description }}",
            "datetime": "={{ $json.datetime }}",
            "views": "={{ $json.views }}",
            "colab": "={{ $json.colab }}",
            "tegs": "={{ $json.tegs }}"
          },
          "matchingColumns": [],
          "schema": [
            {
              "id": "summary",
              "displayName": "summary",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "url",
              "displayName": "url",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "description",
              "displayName": "description",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "datetime",
              "displayName": "datetime",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "views",
              "displayName": "views",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "colab",
              "displayName": "colab",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "tegs",
              "displayName": "tegs",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.6,
      "position": [
        5320,
        620
      ],
      "id": "d49c8039-bdef-48e0-a8bd-006f7e4b3b3a",
      "name": "Google Sheets1",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "create",
        "documentId": {
          "__rl": true,
          "value": "1nD6W0V0eCp9PcVuyycp3fjPMmdfFSzTd0_OlX2sVqZc",
          "mode": "list",
          "cachedResultName": "competitors",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1nD6W0V0eCp9PcVuyycp3fjPMmdfFSzTd0_OlX2sVqZc/edit?usp=drivesdk"
        },
        "title": "={{ $json.posts[0].split('/').slice(0, 4).join('/') }}",
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.6,
      "position": [
        1900,
        -340
      ],
      "id": "4e514012-822b-4d92-8fc6-6bbd5fc1fee9",
      "name": "AddCurrentCompanyList",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {},
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        2580,
        -320
      ],
      "id": "0cd17867-945c-49bd-9f7a-9a73aee32a0a",
      "name": "No Operation, do nothing"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "5743f0a1-3212-4242-8481-ab3512936517",
              "leftValue": "={{ $json.datetime }}",
              "rightValue": "={{ $today.minus(7, 'days') }}",
              "operator": {
                "type": "dateTime",
                "operation": "after"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        2220,
        100
      ],
      "id": "9960d853-dff9-4e72-a1a4-f7d3aba08f04",
      "name": "If"
    },
    {
      "parameters": {
        "jsCode": "return items.map(item => {\n  const url = item.json.url;\n  const description = item.json.description;\n\n  // 1. \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0438\u043c\u044f \u0438\u0437 URL\n  const matchUrl = url.match(/instagram\\.com\\/([^\\/]+)/);\n  const accountFromUrl = matchUrl ? matchUrl[1] : '';\n\n  // 2. \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0438\u043c\u044f \u0438\u0437 description \u043f\u043e\u0441\u043b\u0435 \"comments -\"\n  const matchDesc = description.match(/comments\\s*-\\s*([^\\s]+)/);\n  const accountFromDesc = matchDesc ? matchDesc[1] : '';\n\n  // 3. \u0421\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u043c \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \"colab\"\n  const colab = accountFromDesc && accountFromDesc !== accountFromUrl ? accountFromDesc : '';\n\n  item.json.colab = colab;\n  return item;\n});"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        4440,
        620
      ],
      "id": "3b2461fd-f69a-4f40-b3c5-b23a7c5e1122",
      "name": "CollabSetter"
    },
    {
      "parameters": {
        "jsCode": "return items.map(item => {\n  const description = item.json.description;\n\n  // \u041d\u0430\u0439\u0442\u0438 \u0432\u0441\u0435 \u0445\u044d\u0448\u0442\u0435\u0433\u0438 \u2014 \u0441\u043b\u043e\u0432\u0430, \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u0435\u0441\u044f \u0441 #\n  const hashtags = description.match(/#[\\w\u0430-\u044f\u0410-\u042f\u0451\u0401]+/g) || [];\n\n  // \u041e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0432 \u043e\u0434\u043d\u0443 \u0441\u0442\u0440\u043e\u043a\u0443 \u0441 \u043f\u0440\u043e\u0431\u0435\u043b\u0430\u043c\u0438\n  item.json.tegs = hashtags.join(' ');\n\n  return item;\n});"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        4660,
        620
      ],
      "id": "12f0fea0-e738-4b6c-afeb-f55419cb6fc7",
      "name": "HashTagsSetter"
    },
    {
      "parameters": {
        "jsCode": "return items.map(item => {\n  const date = new Date(item.json.datetime);\n\n  const day = date.getUTCDate();\n  const year = date.getUTCFullYear();\n  const monthNames = [\n    '\u044f\u043d\u0432\u0430\u0440\u044f', '\u0444\u0435\u0432\u0440\u0430\u043b\u044f', '\u043c\u0430\u0440\u0442\u0430', '\u0430\u043f\u0440\u0435\u043b\u044f', '\u043c\u0430\u044f', '\u0438\u044e\u043d\u044f',\n    '\u0438\u044e\u043b\u044f', '\u0430\u0432\u0433\u0443\u0441\u0442\u0430', '\u0441\u0435\u043d\u0442\u044f\u0431\u0440\u044f', '\u043e\u043a\u0442\u044f\u0431\u0440\u044f', '\u043d\u043e\u044f\u0431\u0440\u044f', '\u0434\u0435\u043a\u0430\u0431\u0440\u044f'\n  ];\n  const month = monthNames[date.getUTCMonth()];\n\n  // \u0424\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u043c \u0441\u0442\u0440\u043e\u043a\u0443, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440: \"17 \u0438\u044e\u043d\u044f 2025\"\n  item.json.datetime = `${day} ${month} ${year}`;\n\n  return item;\n});"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        4880,
        620
      ],
      "id": "6948378f-7f69-4e4e-81bb-71003440258a",
      "name": "DateChangerToRussianFormat"
    },
    {
      "parameters": {
        "jsCode": "return items.map(item => {\n  const rawViews = item.json.views.toString().trim().toLowerCase();\n\n  let views = 0;\n\n  if (rawViews.endsWith('k')) {\n    // \u041f\u0440\u0438\u043c\u0435\u0440: \"14k\" \u2192 14000\n    const numberPart = parseFloat(rawViews.replace('k', ''));\n    views = Math.round(numberPart * 1000);\n  } else {\n    // \u0423\u0431\u0438\u0440\u0430\u0435\u043c \u0437\u0430\u043f\u044f\u0442\u044b\u0435, \u043f\u0440\u043e\u0431\u0435\u043b\u044b \u0438 \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0432 \u0447\u0438\u0441\u043b\u043e\n    views = parseInt(rawViews.replace(/[, ]/g, ''), 10);\n  }\n\n  item.json.views = views;\n\n  return item;\n});"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        5100,
        620
      ],
      "id": "6273b478-98d8-41ed-bd5d-28bb0668f9ed",
      "name": "ViewsToNumber"
    }
  ],
  "connections": {
    "When clicking \u2018Execute workflow\u2019": {
      "main": [
        [
          {
            "node": "ListOfStores",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "ListOfStores": {
      "main": [
        [
          {
            "node": "MapStoresInstagram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "MapStoresInstagram": {
      "main": [
        [
          {
            "node": "ArrayToShopItems",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "ArrayToShopItems": {
      "main": [
        [
          {
            "node": "LoopOverBrands",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "LoopOverBrands": {
      "main": [
        [],
        [
          {
            "node": "Calling Instagram-parser",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Calling Instagram-parser": {
      "main": [
        [
          {
            "node": "PostInfoEnricher",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "PostInfoEnricher": {
      "main": [
        [
          {
            "node": "AddCurrentCompanyList",
            "type": "main",
            "index": 0
          },
          {
            "node": "SplitPostInfoToSingle",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "SummeryAgent",
            "type": "main",
            "index": 0
          },
          {
            "node": "MergeAiAnswerToItem",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "SplitPostInfoToSingle": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "SummeryAgent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "SummeryAgent": {
      "main": [
        [
          {
            "node": "MergeAiAnswerToItem",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "MergeAiAnswerToItem": {
      "main": [
        [
          {
            "node": "MergeToOneItem",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "MergeToOneItem": {
      "main": [
        [
          {
            "node": "CollabSetter",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code": {
      "main": [
        [
          {
            "node": "LoopOverBrands",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Sheets1": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AddCurrentCompanyList": {
      "main": [
        [
          {
            "node": "No Operation, do nothing",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "No Operation, do nothing": {
      "main": [
        []
      ]
    },
    "If": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "CollabSetter": {
      "main": [
        [
          {
            "node": "HashTagsSetter",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HashTagsSetter": {
      "main": [
        [
          {
            "node": "DateChangerToRussianFormat",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "DateChangerToRussianFormat": {
      "main": [
        [
          {
            "node": "ViewsToNumber",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "ViewsToNumber": {
      "main": [
        [
          {
            "node": "Google Sheets1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "dcab7c61-c8ca-4f98-ab15-3945f926aa94",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "sYsYPfHbZeqMET1u",
  "tags": []
}