{
  "name": "Reddit Collector to Sheets (RSS + JSON scoring)",
  "nodes": [
    {
      "parameters": {
        "triggerTimes": {
          "item": [
            {
              "mode": "everyDay",
              "hour": 9,
              "minute": 5
            },
            {
              "mode": "everyDay",
              "hour": 13,
              "minute": 5
            },
            {
              "mode": "everyDay",
              "hour": 18,
              "minute": 5
            }
          ]
        }
      },
      "name": "Cron",
      "type": "n8n-nodes-base.cron",
      "typeVersion": 1,
      "position": [
        240,
        260
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "rssUrls",
              "value": "https://www.reddit.com/r/Moving/new.rss,https://www.reddit.com/r/FirstTimeHomeBuyer/new.rss,https://www.reddit.com/r/SameGrassButGreener/new.rss,https://www.reddit.com/r/florida/new.rss,https://www.reddit.com/r/bocaraton/new.rss,https://www.reddit.com/r/fortlauderdale/new.rss,https://www.reddit.com/r/delraybeach/new.rss,https://www.reddit.com/r/broward/new.rss,https://www.reddit.com/r/palmbeach/new.rss"
            },
            {
              "name": "keywords",
              "value": "moving,relocating,relocation,neighborhood,neighbourhood,rent vs buy,first time buyer,hoa,insurance,flood,schools,commute,property tax,boca raton,delray,parkland,coral springs,fort lauderdale,broward,palm beach"
            }
          ],
          "number": [
            {
              "name": "minComments",
              "value": 2
            },
            {
              "name": "minScore",
              "value": 3
            }
          ]
        }
      },
      "name": "Config",
      "type": "n8n-nodes-base.set",
      "typeVersion": 2,
      "position": [
        440,
        260
      ]
    },
    {
      "parameters": {
        "functionCode": "const rssUrls = $json.rssUrls.split(',').map(s => s.trim()).filter(Boolean);\nconst keywords = $json.keywords.split(',').map(s => s.trim().toLowerCase()).filter(Boolean);\nreturn rssUrls.map(u => ({ json: { rssUrl: u, keywords, minComments: $json.minComments, minScore: $json.minScore } }));"
      },
      "name": "Split RSS URLs",
      "type": "n8n-nodes-base.function",
      "typeVersion": 2,
      "position": [
        640,
        260
      ]
    },
    {
      "parameters": {
        "url": "={{$json.rssUrl}}",
        "options": {}
      },
      "name": "Fetch RSS",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        840,
        260
      ]
    },
    {
      "parameters": {
        "options": {}
      },
      "name": "Parse XML",
      "type": "n8n-nodes-base.xml",
      "typeVersion": 2,
      "position": [
        1040,
        260
      ]
    },
    {
      "parameters": {
        "functionCode": "const items = $json.rss?.channel?.[0]?.item || [];\nconst { keywords, minComments, minScore } = $input.first().json;\n\nfunction getPostId(link) {\n  const m = link.match(/\\/comments\\/([a-z0-9]+)\\//i);\n  return m ? m[1] : '';\n}\n\nfunction getSubreddit(link) {\n  const m = link.match(/reddit\\.com\\/r\\/([^\\/]+)\\//i);\n  return m ? m[1] : '';\n}\n\nconst out = [];\nfor (const it of items) {\n  const title = (it.title?.[0] || '').toString();\n  const link = (it.link?.[0] || '').toString();\n  const desc = (it.description?.[0] || '').toString();\n  const allText = (title + ' ' + desc).toLowerCase();\n\n  const match = keywords.find(k => k && allText.includes(k));\n  if (!match) continue;\n\n  const post_id = getPostId(link);\n  if (!post_id) continue;\n\n  out.push({\n    json: {\n      post_id,\n      date_found: new Date().toISOString(),\n      subreddit: getSubreddit(link),\n      title,\n      post_url: link,\n      keyword_matched: match,\n      minComments,\n      minScore\n    }\n  });\n}\nreturn out;"
      },
      "name": "Extract RSS Items",
      "type": "n8n-nodes-base.function",
      "typeVersion": 2,
      "position": [
        1240,
        260
      ]
    },
    {
      "parameters": {
        "url": "={{$json.post_url.endsWith('/') ? $json.post_url + '.json' : $json.post_url + '/.json'}}",
        "options": {
          "responseFormat": "json"
        }
      },
      "name": "Fetch Thread JSON",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        1440,
        260
      ]
    },
    {
      "parameters": {
        "functionCode": "const body = $json;\nconst post = body?.[0]?.data?.children?.[0]?.data || {};\n\nconst upvotes = Number(post.ups || 0);\nconst comment_count = Number(post.num_comments || 0);\nconst score = (comment_count * 2) + upvotes;\n\nconst minComments = Number($input.first().json.minComments || 0);\nconst minScore = Number($input.first().json.minScore || 0);\n\nif (comment_count < minComments) return [];\nif (score < minScore) return [];\n\nreturn [{\n  json: {\n    post_id: post.id || $input.first().json.post_id,\n    date_found: $input.first().json.date_found,\n    subreddit: $input.first().json.subreddit,\n    title: post.title || $input.first().json.title,\n    post_url: post.permalink ? 'https://www.reddit.com' + post.permalink : $input.first().json.post_url,\n    keyword_matched: $input.first().json.keyword_matched,\n    comment_count,\n    upvotes,\n    score,\n    status: 'new'\n  }\n}];"
      },
      "name": "Score + Filter",
      "type": "n8n-nodes-base.function",
      "typeVersion": 2,
      "position": [
        1640,
        260
      ]
    },
    {
      "parameters": {
        "operation": "lookup",
        "documentId": "1n8TfxlswtIVqHgRg7JOkUri6BQa9NYrTTGqDZXOmGxY",
        "sheetName": "Reddit_Inbox",
        "lookupColumn": "post_id",
        "lookupValue": "={{$json.post_id}}"
      },
      "name": "Dedup Lookup",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [
        1840,
        260
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "functionCode": "const found = $json?.matchedRow || $json?.data || null;\nif (found && Array.isArray(found) && found.length > 0) return [];\nreturn [{ json: $input.first().json }];"
      },
      "name": "If Not Found",
      "type": "n8n-nodes-base.function",
      "typeVersion": 2,
      "position": [
        2040,
        260
      ]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": "1n8TfxlswtIVqHgRg7JOkUri6BQa9NYrTTGqDZXOmGxY",
        "sheetName": "Reddit_Inbox",
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "post_id": "={{$json.post_id}}",
            "date_found": "={{$json.date_found}}",
            "subreddit": "={{$json.subreddit}}",
            "title": "={{$json.title}}",
            "post_url": "={{$json.post_url}}",
            "keyword_matched": "={{$json.keyword_matched}}",
            "comment_count": "={{$json.comment_count}}",
            "upvotes": "={{$json.upvotes}}",
            "score": "={{$json.score}}",
            "status": "={{$json.status}}"
          }
        }
      },
      "name": "Append to Reddit_Inbox",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [
        2240,
        260
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Cron": {
      "main": [
        [
          {
            "node": "Config",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Config": {
      "main": [
        [
          {
            "node": "Split RSS URLs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split RSS URLs": {
      "main": [
        [
          {
            "node": "Fetch RSS",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch RSS": {
      "main": [
        [
          {
            "node": "Parse XML",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse XML": {
      "main": [
        [
          {
            "node": "Extract RSS Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract RSS Items": {
      "main": [
        [
          {
            "node": "Fetch Thread JSON",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Thread JSON": {
      "main": [
        [
          {
            "node": "Score + Filter",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Score + Filter": {
      "main": [
        [
          {
            "node": "Dedup Lookup",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Dedup Lookup": {
      "main": [
        [
          {
            "node": "If Not Found",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If Not Found": {
      "main": [
        [
          {
            "node": "Append to Reddit_Inbox",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false
}