{
  "name": "AI Pulse \u2014 Weekly Trends",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "weeks",
              "triggerAtHour": 20
            }
          ]
        }
      },
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.3,
      "position": [
        0,
        0
      ],
      "id": "5ddac95a-d2c3-459c-9267-55070123b74a",
      "name": "Schedule Trigger"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://host.docker.internal:11434/api/generate",
        "sendBody": true,
        "contentType": "raw",
        "rawContentType": "application/json",
        "body": "{\"model\": \"mistral\", \"prompt\": \"\", \"stream\": false, \"keep_alive\": 0}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        208,
        0
      ],
      "id": "da7d3ddf-46c8-4f9e-9e57-ece27804fabc",
      "name": "Free Memory",
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "language": "pythonNative",
        "pythonCode": "import sqlite3\nimport json as json_lib\n\ndb_path = \"/data/ai-pulse/ai-pulse.db\"\nconn = sqlite3.connect(db_path)\nconn.row_factory = sqlite3.Row\ncursor = conn.cursor()\n\ncursor.execute(\"\"\"\n    SELECT id, title, source_name, category, summary, relevance_score, importance\n    FROM articles\n    WHERE importance >= 4\n    AND ingested_at >= datetime('now', '-7 days')\n    ORDER BY importance DESC, relevance_score DESC\n    LIMIT 40\n\"\"\")\nrows = cursor.fetchall()\nconn.close()\n\nif not rows:\n    return [{\"json\": {\"status\": \"no_articles\", \"count\": 0, \"prompt_body\": \"\"}}]\n\narticles_text = \"\"\narticle_ids = []\nfor row in rows:\n    r = dict(row)\n    article_ids.append(r['id'])\n    summary = r.get('summary') or r.get('title', '')\n    articles_text += (\n        f\"- [{r.get('category', 'general')}] {r.get('title', 'Untitled')} \"\n        f\"(Source: {r.get('source_name', 'Unknown')}, \"\n        f\"Importance: {r.get('importance', 0)}/5)\\n\"\n        f\"  Summary: {summary}\\n\\n\"\n    )\n\nprompt = (\n    \"You are a technology trend analyst. Below are the top-rated AI articles \"\n    \"from this week, curated for a Software Engineer interested in AI dev tools, \"\n    \"agentic workflows, context engineering, and SDLC automation.\\n\\n\"\n    f\"{articles_text}\\n\"\n    \"Identify 3-5 emerging patterns or trends from these articles. \"\n    \"For each trend, explain what is happening, cite which articles support it, \"\n    \"and predict where this trend is heading.\\n\\n\"\n    \"Return ONLY valid JSON, no other text:\\n\"\n    \"{\\\"trends\\\": [{\\\"trend_name\\\": \\\"<short name>\\\", \\\"description\\\": \\\"<2-3 sentences>\\\", \"\n    \"\\\"evidence_article_ids\\\": [<list of article IDs>], \"\n    \"\\\"prediction\\\": \\\"<where this is heading>\\\"}]}\"\n)\n\nbody = json_lib.dumps({\n    \"model\": \"llama3:8b\",\n    \"prompt\": prompt,\n    \"stream\": False,\n    \"keep_alive\": \"5m\"\n})\n\nreturn [{\"json\": {\"status\": \"ready\", \"count\": len(rows), \"article_ids\": json_lib.dumps(article_ids), \"request_body\": body}}]"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        416,
        0
      ],
      "id": "b1295411-3b3a-4845-b930-0f14f994af84",
      "name": "Fetch Top Articles"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "38f6f3a2-37b8-4ca4-823e-2b288050e348",
              "leftValue": "={{ $json.status }}",
              "rightValue": "ready",
              "operator": {
                "type": "string",
                "operation": "equals",
                "name": "filter.operator.equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        624,
        0
      ],
      "id": "83f717f2-2524-4a59-b31e-c8cb7f512eae",
      "name": "Has Articles?"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://host.docker.internal:11434/api/generate",
        "sendBody": true,
        "contentType": "raw",
        "rawContentType": "application/json",
        "body": "={{ $('Fetch Top Articles').item.json.request_body }}",
        "options": {
          "timeout": 300000
        }
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        832,
        -96
      ],
      "id": "8fb4cab8-aa47-4186-a509-d0d59c10c11e",
      "name": "Ollama Trend Analysis",
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "language": "pythonNative",
        "pythonCode": "import sqlite3\nimport json as json_lib\nfrom datetime import datetime, timedelta\n\ndb_path = \"/data/ai-pulse/ai-pulse.db\"\n\nitems = _items\nstored = 0\nstatus = \"failed\"\n\nfor item in items:\n    data = item['json'] if 'json' in item else item\n    response_text = str(data.get('response', ''))\n\n    try:\n        clean = response_text.strip()\n        start = clean.find('{')\n        end = clean.rfind('}') + 1\n        if start >= 0 and end > start:\n            parsed = json_lib.loads(clean[start:end])\n            trends = parsed.get('trends', [])\n\n            today = datetime.now()\n            monday = today - timedelta(days=today.weekday())\n            week_start = monday.strftime('%Y-%m-%d')\n\n            conn = sqlite3.connect(db_path)\n            cursor = conn.cursor()\n\n            cursor.execute(\"DELETE FROM weekly_trends WHERE week_start = ?\", (week_start,))\n\n            for trend in trends:\n                trend_name = str(trend.get('trend_name', 'Unknown'))\n                description = str(trend.get('description', ''))\n                evidence = json_lib.dumps(trend.get('evidence_article_ids', []))\n                prediction = str(trend.get('prediction', ''))\n\n                cursor.execute(\"\"\"\n                    INSERT INTO weekly_trends (week_start, trend_name, description, evidence, prediction)\n                    VALUES (?, ?, ?, ?, ?)\n                \"\"\", (week_start, trend_name, description, evidence, prediction))\n                stored += 1\n\n            conn.commit()\n            conn.close()\n            status = \"success\"\n    except:\n        status = \"parse_error\"\n\nreturn [{\"json\": {\"status\": status, \"trends_stored\": stored}}]"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1040,
        -96
      ],
      "id": "58382d3d-9c92-49e7-8a04-28d83492b871",
      "name": "Store Weekly Trends"
    }
  ],
  "connections": {
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Free Memory",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Free Memory": {
      "main": [
        [
          {
            "node": "Fetch Top Articles",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Top Articles": {
      "main": [
        [
          {
            "node": "Has Articles?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Has Articles?": {
      "main": [
        [
          {
            "node": "Ollama Trend Analysis",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ollama Trend Analysis": {
      "main": [
        [
          {
            "node": "Store Weekly Trends",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate",
    "availableInMCP": false
  },
  "versionId": "7e68774b-f7c5-4565-9eb1-7672bb9c8e61",
  "id": "y4vQDeivtAk1BLML",
  "tags": []
}