{
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 6 * * *"
            }
          ]
        }
      },
      "id": "trigger-daily",
      "name": "\u23f0 Daily Trigger (6AM)",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        100,
        300
      ],
      "typeVersion": 1.1
    },
    {
      "parameters": {
        "jsCode": "return [{ json: { runDate: new Date().toISOString().split('T')[0] } }];"
      },
      "id": "init",
      "name": "\ud83d\ude80 Initialize Run",
      "type": "n8n-nodes-base.code",
      "position": [
        320,
        300
      ],
      "typeVersion": 2
    },
    {
      "parameters": {
        "url": "https://api.apify.com/v2/acts/bernardo~youtube-scraper/runs",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "token",
              "value": "={{$vars.APIFY_TOKEN}}"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "searchKeywords",
              "value": "dental implants,All-on-4,All-on-X,full arch implants,same day teeth,zirconia teeth,cosmetic dentistry Miami"
            },
            {
              "name": "maxResults",
              "value": "30"
            },
            {
              "name": "sortBy",
              "value": "viewCount"
            },
            {
              "name": "uploadedAfter",
              "value": "7daysAgo"
            }
          ]
        },
        "options": {}
      },
      "id": "scrape-youtube",
      "name": "\ud83c\udfac Scrape YouTube Trends",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        540,
        160
      ],
      "typeVersion": 4.1
    },
    {
      "parameters": {
        "url": "https://api.apify.com/v2/acts/clockworks~tiktok-scraper/runs",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "token",
              "value": "={{$vars.APIFY_TOKEN}}"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "hashtags",
              "value": "dentalimplants,allon4,cosmeticdentistry,dentist"
            },
            {
              "name": "resultsPerPage",
              "value": "30"
            }
          ]
        },
        "options": {}
      },
      "id": "scrape-tiktok",
      "name": "\ud83c\udfb5 Scrape TikTok Trends",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        540,
        300
      ],
      "typeVersion": 4.1
    },
    {
      "parameters": {
        "url": "https://api.apify.com/v2/acts/bernardo~youtube-scraper/runs",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "token",
              "value": "={{$vars.APIFY_TOKEN}}"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "searchKeywords",
              "value": "dental implants,All-on-4,same day teeth"
            },
            {
              "name": "maxResults",
              "value": "50"
            },
            {
              "name": "sortBy",
              "value": "viewCount"
            },
            {
              "name": "uploadedAfter",
              "value": "30daysAgo"
            },
            {
              "name": "channelMaxSubscribers",
              "value": "100000"
            }
          ]
        },
        "options": {}
      },
      "id": "outlier-hunter",
      "name": "\ud83c\udfaf Outlier Hunter (Small Creators)",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        540,
        440
      ],
      "typeVersion": 4.1
    },
    {
      "parameters": {
        "amount": 45,
        "unit": "seconds"
      },
      "id": "wait-for-scrapes",
      "name": "\u23f3 Wait for Apify Runs",
      "type": "n8n-nodes-base.wait",
      "position": [
        760,
        300
      ],
      "typeVersion": 1.1
    },
    {
      "parameters": {
        "url": "https://api.apify.com/v2/acts/bernardo~youtube-scraper/runs/last/dataset/items",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "token",
              "value": "={{$vars.APIFY_TOKEN}}"
            },
            {
              "name": "limit",
              "value": "30"
            }
          ]
        }
      },
      "id": "fetch-youtube-results",
      "name": "\ud83d\udce5 Fetch YouTube Results",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        980,
        160
      ],
      "typeVersion": 4.1
    },
    {
      "parameters": {
        "url": "https://api.apify.com/v2/acts/clockworks~tiktok-scraper/runs/last/dataset/items",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "token",
              "value": "={{$vars.APIFY_TOKEN}}"
            },
            {
              "name": "limit",
              "value": "30"
            }
          ]
        }
      },
      "id": "fetch-tiktok-results",
      "name": "\ud83d\udce5 Fetch TikTok Results",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        980,
        300
      ],
      "typeVersion": 4.1
    },
    {
      "parameters": {
        "url": "https://api.apify.com/v2/acts/bernardo~youtube-scraper/runs/last/dataset/items",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "token",
              "value": "={{$vars.APIFY_TOKEN}}"
            },
            {
              "name": "limit",
              "value": "50"
            }
          ]
        }
      },
      "id": "fetch-outlier-results",
      "name": "\ud83d\udce5 Fetch Outlier Results",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        980,
        440
      ],
      "typeVersion": 4.1
    },
    {
      "parameters": {
        "jsCode": "const yt=$('Fetch YouTube Results').all().map(i=>i.json);\nconst tt=$('Fetch TikTok Results').all().map(i=>i.json);\nconst out=$('Fetch Outlier Results').all().map(i=>i.json);\nconst outliers=out.filter(v=>v.subscriberCount&&v.viewCount&&v.subscriberCount<100000).map(v=>({...v,ratio:v.viewCount/v.subscriberCount})).filter(v=>v.ratio>0.5).sort((a,b)=>b.ratio-a.ratio).slice(0,10);\nreturn [{json:{date:new Date().toISOString().split('T')[0],topYouTube:yt.slice(0,5),topTikTok:tt.slice(0,5),outliers}}];"
      },
      "id": "process-data",
      "name": "\ud83e\udde0 Process & Rank Content",
      "type": "n8n-nodes-base.code",
      "position": [
        1200,
        300
      ],
      "typeVersion": 2
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.anthropic.com/v1/messages",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "x-api-key",
              "value": "={{$vars.ANTHROPIC_API_KEY}}"
            },
            {
              "name": "anthropic-version",
              "value": "2023-06-01"
            },
            {
              "name": "content-type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\"model\":\"claude-sonnet-4-20250514\",\"max_tokens\":4000,\"system\":\"You are the content strategist for Dr. Aakash Rana (@doctorrana.dds), a Miami implant surgeon. Brand voice: confident, warm, direct, playful, Miami energy. Hook formula: 1) HOOK under 15 words 2) MISSED OPPORTUNITY 3) VALUE GUARANTEE. Write in first person as Dr. Rana.\",\"messages\":[{\"role\":\"user\",\"content\":\"Analyze these videos and write Dr. Rana remixed scripts.\\nYT: \"+JSON.stringify($json.topYouTube.slice(0,3))+\"\\nTT: \"+JSON.stringify($json.topTikTok.slice(0,3))+\"\\nOUTLIERS: \"+JSON.stringify($json.outliers.slice(0,3))+\"\\nRespond ONLY with JSON array. Each object needs: originalTitle,originalCreator,platform,views,introFormula,drRanaTitle1,drRanaTitle2,drRanaHook,drRanaMissedOpp,drRanaGuarantee,drRanaFullIntro30sec,thumbnailDirection,whyItWorks,isOutlier\"}]}",
        "options": {}
      },
      "id": "claude-scriptwriter",
      "name": "\u270d\ufe0f Claude Script Writer Agent",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1420,
        300
      ],
      "typeVersion": 4.1
    },
    {
      "parameters": {
        "jsCode": "const raw=$json.content[0].text;\nlet c=raw.replace(/```json\\n?/g,'').replace(/```/g,'').trim();\ntry{return JSON.parse(c).map(i=>({json:i}));}catch(e){return [{json:{error:'fail',raw:raw.substring(0,300)}}];"
      },
      "id": "parse-claude-output",
      "name": "\ud83d\udd27 Parse Claude Output",
      "type": "n8n-nodes-base.code",
      "position": [
        1640,
        300
      ],
      "typeVersion": 2
    },
    {
      "parameters": {
        "jsCode": "const items=$('Parse Claude Output').all();\nconst date=new Date().toISOString().split('T')[0];\nconst rows=items.map((item,i)=>{const d=item.json;return `<tr style=\"background:${i%2===0?'#f9f9f9':'#fff'}\"><td style=\"padding:10px;border:1px solid #ddd\">${d.platform||''}</td><td style=\"padding:10px;border:1px solid #ddd\">${d.originalTitle||''}</td><td style=\"padding:10px;border:1px solid #ddd;color:#1a56db;font-weight:bold\">${d.drRanaTitle1||''}</td><td style=\"padding:10px;border:1px solid #ddd;font-style:italic\">${d.drRanaHook||''}</td><td style=\"padding:10px;border:1px solid #ddd;font-size:12px\">${(d.drRanaFullIntro30sec||'').substring(0,120)}...</td><td style=\"padding:10px;border:1px solid #ddd;text-align:center\">${d.isOutlier?'\ud83c\udfaf':'\u2014'}</td></tr>`;}).join('');\nconst html=`<html><body style=\"font-family:Arial,sans-serif;background:#f5f5f5;padding:20px\"><div style=\"max-width:900px;margin:0 auto;background:#fff;border-radius:12px;overflow:hidden\"><div style=\"background:#1a1a2e;padding:28px;text-align:center\"><h1 style=\"color:#FFD700;margin:0\">PRANA CONTENT TEAM</h1><p style=\"color:#888;margin:8px 0 0\">Daily Briefing \u00b7 ${date}</p></div><div style=\"padding:24px\"><p style=\"background:#f0f7ff;border-left:4px solid #1a56db;padding:14px;border-radius:0 8px 8px 0\">\ud83c\udfaf <strong>Today's scripts are ready.</strong> Pick a topic, hit record.</p><table style=\"width:100%;border-collapse:collapse;font-size:13px\"><thead><tr style=\"background:#1a1a2e\"><th style=\"color:#FFD700;padding:10px;text-align:left\">Platform</th><th style=\"color:#FFD700;padding:10px;text-align:left\">Original</th><th style=\"color:#FFD700;padding:10px;text-align:left\">Your Title</th><th style=\"color:#FFD700;padding:10px;text-align:left\">Your Hook</th><th style=\"color:#FFD700;padding:10px;text-align:left\">Intro Preview</th><th style=\"color:#FFD700;padding:10px;text-align:left\">Outlier</th></tr></thead><tbody>${rows}</tbody></table></div><div style=\"background:#f9f9f9;padding:12px;text-align:center;font-size:11px;color:#999\">Prana Content Team \u00b7 Auto-generated daily</div></div></body></html>`;\nreturn [{json:{subject:`\ud83c\udfac Prana Brief \u2014 ${date} | ${items.length} scripts ready`,html}}];"
      },
      "id": "build-email",
      "name": "\ud83d\udce7 Build Email Brief",
      "type": "n8n-nodes-base.code",
      "position": [
        1860,
        300
      ],
      "typeVersion": 2
    },
    {
      "parameters": {
        "sendTo": "kashranadds@gmail.com",
        "subject": "={{$json.subject}}",
        "message": "={{$json.html}}",
        "options": {
          "appendAttribution": false
        }
      },
      "id": "send-email",
      "name": "\ud83d\udcec Send Daily Brief Email",
      "type": "n8n-nodes-base.gmail",
      "position": [
        2080,
        300
      ],
      "typeVersion": 2.1
    }
  ],
  "connections": {
    "\u23f0 Daily Trigger (6AM)": {
      "main": [
        [
          {
            "node": "\ud83d\ude80 Initialize Run",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "\ud83d\ude80 Initialize Run": {
      "main": [
        [
          {
            "node": "\ud83c\udfac Scrape YouTube Trends",
            "type": "main",
            "index": 0
          },
          {
            "node": "\ud83c\udfb5 Scrape TikTok Trends",
            "type": "main",
            "index": 0
          },
          {
            "node": "\ud83c\udfaf Outlier Hunter (Small Creators)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "\ud83c\udfac Scrape YouTube Trends": {
      "main": [
        [
          {
            "node": "\u23f3 Wait for Apify Runs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "\ud83c\udfb5 Scrape TikTok Trends": {
      "main": [
        [
          {
            "node": "\u23f3 Wait for Apify Runs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "\ud83c\udfaf Outlier Hunter (Small Creators)": {
      "main": [
        [
          {
            "node": "\u23f3 Wait for Apify Runs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "\u23f3 Wait for Apify Runs": {
      "main": [
        [
          {
            "node": "\ud83d\udce5 Fetch YouTube Results",
            "type": "main",
            "index": 0
          },
          {
            "node": "\ud83d\udce5 Fetch TikTok Results",
            "type": "main",
            "index": 0
          },
          {
            "node": "\ud83d\udce5 Fetch Outlier Results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "\ud83d\udce5 Fetch YouTube Results": {
      "main": [
        [
          {
            "node": "\ud83e\udde0 Process & Rank Content",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "\ud83d\udce5 Fetch TikTok Results": {
      "main": [
        [
          {
            "node": "\ud83e\udde0 Process & Rank Content",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "\ud83d\udce5 Fetch Outlier Results": {
      "main": [
        [
          {
            "node": "\ud83e\udde0 Process & Rank Content",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "\ud83e\udde0 Process & Rank Content": {
      "main": [
        [
          {
            "node": "\u270d\ufe0f Claude Script Writer Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "\u270d\ufe0f Claude Script Writer Agent": {
      "main": [
        [
          {
            "node": "\ud83d\udd27 Parse Claude Output",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "\ud83d\udd27 Parse Claude Output": {
      "main": [
        [
          {
            "node": "\ud83d\udce7 Build Email Brief",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "\ud83d\udce7 Build Email Brief": {
      "main": [
        [
          {
            "node": "\ud83d\udcec Send Daily Brief Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}