{
  "name": "08-A: Google Maps Lead Scraper (DE+TR)",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "hours",
              "hoursInterval": 24,
              "triggerAtHour": 9
            }
          ]
        }
      },
      "id": "schedule-trigger",
      "name": "Daily 09:00",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        100,
        300
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "kw-de",
              "name": "searches",
              "value": "={{ [\n  {keyword: 'KI Beratung', city: 'Berlin', country: 'DE'},\n  {keyword: 'KI Beratung', city: 'M\u00fcnchen', country: 'DE'},\n  {keyword: 'Automatisierung KMU', city: 'Hamburg', country: 'DE'},\n  {keyword: 'AI Agent Agentur', city: 'Frankfurt', country: 'DE'},\n  {keyword: 'KI Kundenservice', city: 'K\u00f6ln', country: 'DE'},\n  {keyword: 'AI ajans', city: 'Istanbul', country: 'TR'},\n  {keyword: 'yapay zeka \u00e7\u00f6z\u00fcmleri', city: 'Ankara', country: 'TR'},\n  {keyword: 'WhatsApp m\u00fc\u015fteri destek', city: 'Izmir', country: 'TR'}\n] }}",
              "type": "array"
            }
          ]
        }
      },
      "id": "set-searches",
      "name": "Define Search Terms",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        300,
        300
      ]
    },
    {
      "parameters": {
        "fieldToSplitOut": "searches",
        "options": {}
      },
      "id": "split-searches",
      "name": "Split Searches",
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "position": [
        500,
        300
      ]
    },
    {
      "parameters": {
        "url": "=http://127.0.0.1:5098/scrape",
        "method": "POST",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "url",
              "value": "=https://www.google.com/maps/search/{{ encodeURIComponent($json.keyword + ' ' + $json.city) }}"
            },
            {
              "name": "extract",
              "value": "leads"
            },
            {
              "name": "country",
              "value": "={{ $json.country }}"
            }
          ]
        },
        "options": {
          "timeout": 30000
        }
      },
      "id": "scrapling-scrape",
      "name": "Scrapling Google Maps",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        700,
        300
      ],
      "continueOnFail": true
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": false
          },
          "conditions": [
            {
              "leftValue": "={{ $json.results }}",
              "rightValue": "",
              "operator": {
                "type": "array",
                "operation": "notEmpty"
              }
            }
          ]
        }
      },
      "id": "check-results",
      "name": "Has Results?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        900,
        300
      ]
    },
    {
      "parameters": {
        "fieldToSplitOut": "results",
        "options": {}
      },
      "id": "split-results",
      "name": "Split Results",
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "position": [
        1100,
        200
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "INSERT INTO leads (company_name, country, website, phone, keyword, source)\nVALUES (\n  '{{ $json.name || $json.company_name || ''Unknown'' }}',\n  '{{ $json.country }}',\n  '{{ $json.website || $json.url || null }}',\n  '{{ $json.phone || null }}',\n  '{{ $json.keyword }}',\n  'google_maps'\n)\nON CONFLICT (company_name, country) DO UPDATE\n  SET updated_at = NOW(), phone = EXCLUDED.phone, website = EXCLUDED.website\nRETURNING id, company_name",
        "additionalFields": {}
      },
      "id": "insert-lead",
      "name": "Insert Lead to DB",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        1300,
        200
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chatId": "ARMANUAL_TELEGRAM_CHAT_ID",
        "text": "=\ud83d\udcca Lead Scraper done: {{ $json.length }} leads from Google Maps today",
        "additionalFields": {}
      },
      "id": "telegram-notify",
      "name": "Telegram Summary",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1500,
        200
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "continueOnFail": true
    }
  ],
  "connections": {
    "Daily 09:00": {
      "main": [
        [
          {
            "node": "Define Search Terms",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Define Search Terms": {
      "main": [
        [
          {
            "node": "Split Searches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Searches": {
      "main": [
        [
          {
            "node": "Scrapling Google Maps",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Scrapling Google Maps": {
      "main": [
        [
          {
            "node": "Has Results?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Has Results?": {
      "main": [
        [
          {
            "node": "Split Results",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Split Results": {
      "main": [
        [
          {
            "node": "Insert Lead to DB",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Insert Lead to DB": {
      "main": [
        [
          {
            "node": "Telegram Summary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "saveExecutionProgress": true,
    "callerPolicy": "workflowsFromSameOwner"
  },
  "tags": [
    "revenue",
    "lead-gen",
    "phase-08"
  ],
  "notes": "ARMANUAL: 1) Add owly-postgres credential (host=127.0.0.1, port=5433, user=postgres, db=owly). 2) Add Telegram credential with bot token. 3) Set ARMANUAL_TELEGRAM_CHAT_ID. Scrapling /lead endpoint has a StealthyFetcher bug \u2014 using /scrape fallback. Monitor and replace with Google Maps API if budget allows."
}