{
  "name": "wf_scraper_facebook_events",
  "nodes": [
    {
      "parameters": {
        "jsCode": "// n8n Code node \u2014 Run Once for All Items\n// Takes an array of inputs like:\n// [\n//   { \"intent\": \"music\", \"Location\": \"Atlanta, GA\" },\n//   { \"intent\": \"outdoors\", \"Location\": \"Chicago, IL\" }\n// ]\n// Returns an array of objects with Facebook event search URLs\n\nconst results = $input.all().map(item => {\n  const escapedIntent = encodeURIComponent(`${item.json.intent}`.replace(/\\s+/g, ' '));\n  const escapedLocation = encodeURIComponent(`${item.json.location}`.replace(/\\s+/g, ' '));\n  return {\n      url: `https://www.facebook.com/events/search/?q=${escapedIntent}&location=${escapedLocation}`\n    };\n});\n\nreturn {\n  \"input\": results,\n  \"custom_output_fields\": [\"url\",\"main_image\",\"event_date\",\"title\",\"people_responded\",\"location\",\"description\",\"longitude\",\"latitude\",\"event_timezone\"]\n};"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -192,
        304
      ],
      "id": "073c05ed-e8dc-4ed8-9ba1-9cba1d89ee5e",
      "name": "generate_search_urls",
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "operation": "appendOrUpdate",
        "documentId": {
          "__rl": true,
          "value": "1-Mmc5mC8CWyocEdRLPffSgJeA-ve4nkX2ol76f8o-ZQ",
          "mode": "list",
          "cachedResultName": "underfoot_facebook_events_cache",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1-Mmc5mC8CWyocEdRLPffSgJeA-ve4nkX2ol76f8o-ZQ/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Sheet1",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1-Mmc5mC8CWyocEdRLPffSgJeA-ve4nkX2ol76f8o-ZQ/edit#gid=0"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {
            "url": "="
          },
          "matchingColumns": [
            "url"
          ],
          "schema": [
            {
              "id": "url",
              "displayName": "url",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "title",
              "displayName": "title",
              "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": "main_image",
              "displayName": "main_image",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "event_date",
              "displayName": "event_date",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "location",
              "displayName": "location",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "longitude",
              "displayName": "longitude",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "latitude",
              "displayName": "latitude",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "people_responded",
              "displayName": "people_responded",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "event_timezone",
              "displayName": "event_timezone",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "cached_at",
              "displayName": "cached_at",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "user_intent",
              "displayName": "user_intent",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "user_location",
              "displayName": "user_location",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {
          "handlingExtraData": "insertInNewColumn"
        }
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        1152,
        656
      ],
      "id": "803759e3-816d-4009-8667-536213eae5f7",
      "name": "update_fb_events_cache",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "workflowId": {
          "__rl": true,
          "value": "wRCvCmRGcILIoLjy",
          "mode": "list",
          "cachedResultName": "Underfoot \u2014 wf_error_notifications"
        },
        "workflowInputs": {
          "mappingMode": "defineBelow",
          "value": {
            "json": "={{ $json }}",
            "callingFlow": "={{ $workflow.name }}"
          },
          "matchingColumns": [
            "json"
          ],
          "schema": [
            {
              "id": "json",
              "displayName": "json",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "canBeUsedToMatch": true,
              "type": "object",
              "removed": false
            },
            {
              "id": "callingFlow",
              "displayName": "callingFlow",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "canBeUsedToMatch": true,
              "type": "string",
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": true
        },
        "mode": "each",
        "options": {
          "waitForSubWorkflow": false
        }
      },
      "type": "n8n-nodes-base.executeWorkflow",
      "typeVersion": 1.2,
      "position": [
        1376,
        224
      ],
      "id": "d6e3d5e0-8486-43ed-b2cc-97897c0ee518",
      "name": "call_error_notifications",
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "inputSource": "jsonExample",
        "jsonExample": "{\n  \"intent\": \"an interest\",\n  \"location\": \"a place to look for events\"\n}"
      },
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "typeVersion": 1.1,
      "position": [
        -416,
        304
      ],
      "id": "0cc09d5b-1f04-4f61-9e57-04e50b61c1b9",
      "name": "workflow_trigger",
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://api.brightdata.com/datasets/v3/trigger",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "brightdataApi",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "=dataset_id",
              "value": "={{$vars.FACEBOOK_EVENTS_DATASET_ID}}"
            },
            {
              "name": "include_errors",
              "value": "true"
            },
            {
              "name": "type",
              "value": "discover_new"
            },
            {
              "name": "discover_by",
              "value": "url"
            },
            {
              "name": "limit_per_input",
              "value": "20"
            }
          ]
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ $json }}",
        "options": {
          "timeout": 12000
        }
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        32,
        368
      ],
      "id": "5b943e47-6bed-4d78-bb5d-bae168500df8",
      "name": "trigger_fb_events_collection",
      "alwaysOutputData": false,
      "credentials": {
        "httpBearerAuth": {
          "name": "<your credential>"
        },
        "brightdataApi": {
          "name": "<your credential>"
        }
      },
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "resource": "webScrapper",
        "operation": "downloadSnapshot",
        "snapshot_id": "={{ $json.snapshot_id }}",
        "requestOptions": {
          "timeout": 12000
        }
      },
      "type": "@brightdata/n8n-nodes-brightdata.brightData",
      "typeVersion": 1,
      "position": [
        704,
        560
      ],
      "id": "2bf5da51-fcdb-456e-878d-f0d811c6b1e0",
      "name": "download_snapshot",
      "credentials": {
        "brightdataApi": {
          "name": "<your credential>"
        }
      },
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "resource": "webScrapper",
        "operation": "monitorProgressSnapshot",
        "snapshot_id": "={{ $json.snapshot_id }}",
        "requestOptions": {}
      },
      "type": "@brightdata/n8n-nodes-brightdata.brightData",
      "typeVersion": 1,
      "position": [
        256,
        544
      ],
      "id": "f69b8f12-cfc3-4f13-a5ad-23ee740a6a0b",
      "name": "check_snapshot_status",
      "credentials": {
        "brightdataApi": {
          "name": "<your credential>"
        }
      },
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "jsCode": "// Facebook Events \u2192 Cache rows (key = url)\n// Input: either one item whose .json is an array (your sample), or one item per event.\n// Output: one row per event with fields ready for your cache/table.\n\nconst events = $input.all();\nconst userInput = $(\"workflow_trigger\").first().json;\n\nconst toNum = (v) => {\n  if (v === null || v === undefined || v === \"\") return null;\n  const n = Number(v);\n  return Number.isFinite(n) ? n : null;\n};\n\nconst nowISO = new Date().toISOString();\n\nconst rows = events\n  .map(e => {\n    const event = e.json;\n\n    return {\n      url: event.url,\n      title: event.title,\n      description: event.description.text,\n      main_image: event.main_image,\n      event_date: event.event_date,\n      location: event.location?.address,\n      latitude: event.latitude,\n      longitude: event.longitude,\n      people_respondeed: event.people_responded,\n      event_timezone: event.event_timezone,\n      cached_at: nowISO,\n      user_intent: userInput.intent,\n      user_location: userInput.location\n    };\n  });\n\nreturn rows.map(r => ({ json: r }));"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        928,
        656
      ],
      "id": "6eadca21-71b7-4178-b446-2cad5ec3f2dd",
      "name": "normalize_fb_events",
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.status }}",
                    "rightValue": "running",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "id": "5a6f220a-b5c7-483b-8f3d-35ec72223efd"
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "running"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "f4e2e7a4-9694-43cb-b743-f28c07ffbd91",
                    "leftValue": "={{ $json.status }}",
                    "rightValue": "ready",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "ready"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "d3467027-28f4-41e3-8048-3e47a0ac632b",
                    "leftValue": "={{ $json.status }}",
                    "rightValue": "failed",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "failed"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.2,
      "position": [
        480,
        528
      ],
      "id": "9e901a94-8d56-4e14-ac80-1d873a4243e7",
      "name": "check_current_status"
    },
    {
      "parameters": {
        "amount": 25
      },
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        704,
        784
      ],
      "id": "b441b49d-8581-454d-a201-29aded13e2c0",
      "name": "wait_25s",
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://uqvwaiexsgprdbdecoxx.supabase.co/functions/v1/merge-cache",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBearerAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"title\": {{ $json.title }},\n  \"description\": {{ $json.description }},\n  \"source_type\": {{ $workflow.name }},\n  \"external_id\": {{ $execution.id }},\n  \"url\": {{ $json.url }},\n  \"image_url\": {{ $json.image_url }},\n  \"data\": {{ $json }}\n}",
        "options": {
          "timeout": 120000
        }
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1152,
        848
      ],
      "id": "a421f436-1f5d-4d15-a8c9-5d761ee78754",
      "name": "update_cache_supabase",
      "credentials": {
        "httpBearerAuth": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "generate_search_urls": {
      "main": [
        [
          {
            "node": "trigger_fb_events_collection",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "call_error_notifications",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "update_fb_events_cache": {
      "main": [
        [],
        [
          {
            "node": "call_error_notifications",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "workflow_trigger": {
      "main": [
        [
          {
            "node": "generate_search_urls",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "trigger_fb_events_collection": {
      "main": [
        [
          {
            "node": "check_snapshot_status",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "call_error_notifications",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "download_snapshot": {
      "main": [
        [
          {
            "node": "normalize_fb_events",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "call_error_notifications",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "check_snapshot_status": {
      "main": [
        [
          {
            "node": "check_current_status",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "call_error_notifications",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "normalize_fb_events": {
      "main": [
        [
          {
            "node": "update_fb_events_cache",
            "type": "main",
            "index": 0
          },
          {
            "node": "update_cache_supabase",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "call_error_notifications",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "check_current_status": {
      "main": [
        [
          {
            "node": "wait_25s",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "download_snapshot",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "call_error_notifications",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "wait_25s": {
      "main": [
        [
          {
            "node": "check_snapshot_status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "callerPolicy": "workflowsFromSameOwner",
    "errorWorkflow": "g0RA1ILyuSMHbt6q"
  },
  "versionId": "f802630a-9e11-4a4d-b30c-83c9395dcd8e",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "r9kt1jHGjG91iPuQ",
  "tags": []
}