{
  "name": "HN digest",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 8 * * *"
            }
          ]
        }
      },
      "id": "e8c46924-d7d0-4916-a2d7-c51170430a85",
      "name": "Every morning",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.4,
      "position": [
        -800,
        0
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "https://hacker-news.firebaseio.com/v0/topstories.json",
        "authentication": "none",
        "options": {}
      },
      "id": "e7777bf0-de28-4a0e-a695-1061aa0d67ab",
      "name": "Fetch top story IDs",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.5,
      "position": [
        -560,
        0
      ]
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "// topstories.json returns an array of story ids, and the HTTP Request node\n// turns each array element into its own item. Keep the first five.\nreturn $input.all().slice(0, 5).map(({ json }) => ({ json: { id: json } }));"
      },
      "id": "38f69db8-8263-4cc5-97e7-bbf6bdc4bbc9",
      "name": "Take first five",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -320,
        0
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "=https://hacker-news.firebaseio.com/v0/item/{{ $json.id }}.json",
        "authentication": "none",
        "options": {}
      },
      "id": "5abb6134-fe2e-4342-8bf8-a3a9f55cc61b",
      "name": "Fetch each story",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.5,
      "position": [
        -80,
        0
      ]
    },
    {
      "parameters": {
        "mode": "manual",
        "assignments": {
          "assignments": [
            {
              "id": "baa0bd44-4a07-447d-8c28-34a5abef8ae1",
              "name": "id",
              "type": "number",
              "value": "={{ $json.id }}"
            },
            {
              "id": "e1cfe76a-2a9a-4b1b-8f1e-2960ffa68785",
              "name": "title",
              "type": "string",
              "value": "={{ $json.title }}"
            },
            {
              "id": "d253ce7c-eb76-40ac-8c2f-0dbb1aadaab8",
              "name": "url",
              "type": "string",
              "value": "={{ $json.url || 'https://news.ycombinator.com/item?id=' + $json.id }}"
            },
            {
              "id": "5d80982e-acd6-4456-8e44-efb9791c5a13",
              "name": "score",
              "type": "number",
              "value": "={{ $json.score }}"
            }
          ]
        },
        "includeOtherFields": false,
        "options": {}
      },
      "id": "11563aab-a0a9-4894-89c0-f651baf9906a",
      "name": "Pick fields",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.5,
      "position": [
        160,
        0
      ]
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "// One CSV line per story, quoted where needed, ending with a newline.\n// HN titles arrive HTML-escaped, so decode the common entities first.\n// Convert to File's CSV mode adds a byte-order mark per chunk and no trailing\n// newline, so a hand-built line is the reliable way to append rows.\nconst decode = (s) => String(s ?? '')\n  .replace(/&amp;/g, '&')\n  .replace(/&lt;/g, '<')\n  .replace(/&gt;/g, '>')\n  .replace(/&quot;/g, '\"')\n  .replace(/&#x27;/g, \"'\")\n  .replace(/&#39;/g, \"'\");\nconst esc = (v) => {\n  const s = String(v ?? '');\n  return /[\",\\n]/.test(s) ? '\"' + s.replace(/\"/g, '\"\"') + '\"' : s;\n};\nconst lines = $input.all().map(({ json }) =>\n  [json.id, decode(json.title), json.url, json.score].map(esc).join(','),\n);\nreturn [{ json: { line: lines.join('\\n') + '\\n' } }];"
      },
      "id": "f9401d10-77c9-4c5c-bffe-09aec3b4231a",
      "name": "Build CSV lines",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        400,
        0
      ]
    },
    {
      "parameters": {
        "operation": "toText",
        "sourceProperty": "line",
        "binaryPropertyName": "data",
        "options": {
          "addBOM": false,
          "fileName": "hn-digest.csv"
        }
      },
      "id": "61055a0d-f0fb-400a-9a3d-d79b0950955d",
      "name": "To text file",
      "type": "n8n-nodes-base.convertToFile",
      "typeVersion": 1.2,
      "position": [
        640,
        0
      ]
    },
    {
      "parameters": {
        "operation": "write",
        "fileName": "/home/hatch/.n8n-files/hn-digest.csv",
        "dataPropertyName": "data",
        "options": {
          "append": true
        }
      },
      "id": "7a644b42-cdce-4910-b218-948d2928ca7d",
      "name": "Append to file",
      "type": "n8n-nodes-base.readWriteFile",
      "typeVersion": 1.1,
      "position": [
        880,
        0
      ]
    }
  ],
  "connections": {
    "Every morning": {
      "main": [
        [
          {
            "node": "Fetch top story IDs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch top story IDs": {
      "main": [
        [
          {
            "node": "Take first five",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Take first five": {
      "main": [
        [
          {
            "node": "Fetch each story",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch each story": {
      "main": [
        [
          {
            "node": "Pick fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Pick fields": {
      "main": [
        [
          {
            "node": "Build CSV lines",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build CSV lines": {
      "main": [
        [
          {
            "node": "To text file",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "To text file": {
      "main": [
        [
          {
            "node": "Append to file",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}