{
 "name": "[노드 한 장] Code · 내일 달리기 좋은 시간 찾기",
 "nodes": [
  {
   "id": "c1",
   "name": "Run manually",
   "type": "n8n-nodes-base.manualTrigger",
   "typeVersion": 1,
   "position": [
    0,
    0
   ],
   "parameters": {}
  },
  {
   "id": "c2",
   "name": "Get Seoul hourly forecast",
   "type": "n8n-nodes-base.httpRequest",
   "typeVersion": 4.2,
   "position": [
    260,
    0
   ],
   "parameters": {
    "url": "https://api.open-meteo.com/v1/forecast?latitude=37.5665&longitude=126.978&hourly=temperature_2m,precipitation_probability&timezone=Asia%2FSeoul&forecast_days=2",
    "options": {}
   }
  },
  {
   "id": "c3",
   "name": "Find best running time",
   "type": "n8n-nodes-base.code",
   "typeVersion": 2,
   "position": [
    520,
    0
   ],
   "parameters": {
    "jsCode": "// 내일 서울 06~21시 예보에서 달리기 제일 좋은 2시간 찾기\nconst h = $input.first().json.hourly;\nconst day = h.time[24].slice(0, 10); // 내일 날짜\nconst hours = h.time\n  .map((t, i) => ({ hour: +t.slice(11, 13), day: t.slice(0, 10),\n    temp: h.temperature_2m[i], rain: h.precipitation_probability[i] }))\n  .filter(x => x.day === day && x.hour >= 6 && x.hour <= 21);\nlet best;\nfor (let i = 0; i < hours.length - 1; i++) {\n  const a = hours[i], b = hours[i + 1];\n  const score = a.rain + b.rain + Math.abs((a.temp + b.temp) / 2 - 15) * 3;\n  if (!best || score < best.score) best = { score, a, b };\n}\nconst temps = hours.map(x => x.temp);\nreturn [{ json: {\n  date: day,\n  best_time: `${best.a.hour}시~${best.b.hour + 1}시`,\n  temp_c: +((best.a.temp + best.b.temp) / 2).toFixed(1),\n  rain_pct: Math.max(best.a.rain, best.b.rain),\n  min_c: Math.min(...temps), max_c: Math.max(...temps),\n  summary: `내일 ${best.a.hour}시~${best.b.hour + 1}시에 뛰세요 (${((best.a.temp + best.b.temp) / 2).toFixed(1)}°C, 비 ${Math.max(best.a.rain, best.b.rain)}%)`\n} }];\n"
   }
  }
 ],
 "connections": {
  "Run manually": {
   "main": [
    [
     {
      "node": "Get Seoul hourly forecast",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Get Seoul hourly forecast": {
   "main": [
    [
     {
      "node": "Find best running time",
      "type": "main",
      "index": 0
     }
    ]
   ]
  }
 },
 "settings": {
  "timezone": "Asia/Seoul",
  "executionOrder": "v1"
 },
 "meta": {
  "templateCredsSetupCompleted": false,
  "source": "datapopcorn.ai/workflow/nodes"
 }
}