diff --git a/workflows/f/weather/weather_sync.schedule.yaml b/workflows/f/weather/weather_sync.schedule.yaml new file mode 100644 index 0000000..ff6a30c --- /dev/null +++ b/workflows/f/weather/weather_sync.schedule.yaml @@ -0,0 +1,10 @@ +args: {} +cron_version: v2 +email: akiracraftwork@gmail.com +enabled: true +is_flow: true +no_flow_overlap: false +schedule: 0 0 6 * * * +script_path: f/weather/weather_sync +timezone: Asia/Tokyo +ws_error_handler_muted: false diff --git a/workflows/f/weather/weather_sync__flow/flow.yaml b/workflows/f/weather/weather_sync__flow/flow.yaml new file mode 100644 index 0000000..4d4eaa8 --- /dev/null +++ b/workflows/f/weather/weather_sync__flow/flow.yaml @@ -0,0 +1,18 @@ +summary: Weather Sync - 気象データ日次同期 +description: Open-Meteo から昨日の気象データを取得し、Keinasystem DB に保存する。毎朝6時実行。 +value: + modules: + - id: a + summary: 気象データ取得・同期 + value: + type: rawscript + content: '!inline 気象データ取得・同期.py' + input_transforms: {} + lock: '!inline 気象データ取得・同期.lock' + language: python3 +schema: + $schema: 'https://json-schema.org/draft/2020-12/schema' + type: object + order: [] + properties: {} + required: [] diff --git a/workflows/f/weather/weather_sync__flow/気象データ取得・同期.lock b/workflows/f/weather/weather_sync__flow/気象データ取得・同期.lock new file mode 100644 index 0000000..78d89bd --- /dev/null +++ b/workflows/f/weather/weather_sync__flow/気象データ取得・同期.lock @@ -0,0 +1,12 @@ +# py: 3.12 +anyio==4.12.1 +certifi==2026.2.25 +charset-normalizer==3.4.4 +h11==0.16.0 +httpcore==1.0.9 +httpx==0.28.1 +idna==3.11 +requests==2.32.5 +typing-extensions==4.15.0 +urllib3==2.6.3 +wmill==1.646.0 \ No newline at end of file diff --git a/workflows/f/weather/weather_sync__flow/気象データ取得・同期.py b/workflows/f/weather/weather_sync__flow/気象データ取得・同期.py new file mode 100644 index 0000000..9b322e5 --- /dev/null +++ b/workflows/f/weather/weather_sync__flow/気象データ取得・同期.py @@ -0,0 +1,72 @@ +import wmill +import requests +import datetime + +LATITUDE = 33.213 +LONGITUDE = 133.133 +TIMEZONE = "Asia/Tokyo" + +OPEN_METEO_URL = "https://archive-api.open-meteo.com/v1/archive" +DAILY_VARS = [ + "temperature_2m_mean", + "temperature_2m_max", + "temperature_2m_min", + "sunshine_duration", + "precipitation_sum", + "wind_speed_10m_max", + "surface_pressure_min", +] + + +def main(): + api_key = wmill.get_variable("u/admin/KEINASYSTEM_API_KEY") + base_url = wmill.get_variable("u/admin/KEINASYSTEM_API_URL").rstrip("/") + sync_url = f"{base_url}/api/weather/sync/" + + yesterday = (datetime.date.today() - datetime.timedelta(days=1)).isoformat() + print(f"Fetching weather data for {yesterday} ...") + + params = { + "latitude": LATITUDE, + "longitude": LONGITUDE, + "start_date": yesterday, + "end_date": yesterday, + "daily": DAILY_VARS, + "timezone": TIMEZONE, + } + resp = requests.get(OPEN_METEO_URL, params=params, timeout=30) + if resp.status_code != 200: + raise Exception(f"Open-Meteo API error: {resp.status_code} {resp.text[:300]}") + + daily = resp.json().get("daily", {}) + dates = daily.get("time", []) + if not dates: + print("No data returned from Open-Meteo.") + return {"status": "no_data"} + + sunshine_raw = daily.get("sunshine_duration", []) + records = [] + for i, d in enumerate(dates): + sun_sec = sunshine_raw[i] + records.append({ + "date": d, + "temp_mean": daily["temperature_2m_mean"][i], + "temp_max": daily["temperature_2m_max"][i], + "temp_min": daily["temperature_2m_min"][i], + "sunshine_h": round(sun_sec / 3600, 2) if sun_sec is not None else None, + "precip_mm": daily["precipitation_sum"][i], + "wind_max": daily["wind_speed_10m_max"][i], + "pressure_min": daily["surface_pressure_min"][i], + }) + + headers = { + "X-API-Key": api_key, + "Content-Type": "application/json", + } + post_resp = requests.post(sync_url, json=records, headers=headers, timeout=30) + if post_resp.status_code not in (200, 201): + raise Exception(f"Keinasystem sync error: {post_resp.status_code} {post_resp.text[:300]}") + + result = post_resp.json() + print(f"Sync complete: {result}") + return result diff --git a/workflows/wmill-lock.yaml b/workflows/wmill-lock.yaml index a4ca909..518e086 100644 --- a/workflows/wmill-lock.yaml +++ b/workflows/wmill-lock.yaml @@ -9,6 +9,8 @@ locks: f/mail/mail_filter__flow+メール取得・判定・通知.py: 0b9cc3ff72d6f3445d46005a657903ae8f195104d1623b47079d13691811c602 f/shiraou/shiraou_notification__flow+__flow_hash: 94825ff4362b6e4b6d165f8e17a51ebf8e5ef4da3e0ec1407a94b614ecab19dd f/shiraou/shiraou_notification__flow+変更確認・line通知.py: ac80896991cce8132cfbf34d5dae20d3c09de5bc74a55c500e4c8705dd6a9d88 + f/weather/weather_sync__flow+__flow_hash: 8af44676b2a175c1cc105028682f18e4bfbf7bf9de2722263a7d85c13c825f08 + f/weather/weather_sync__flow+気象データ取得・同期.py: 86c9953ec7346601eaa13c681e2db5c01c9a5b4b45a3c47e8667ad3c47557029 g/all/setup_app__app+__app_hash: d71add32e14e552d1a4c861c972a50d9598b07c0af201bbadec5b59bbd99d7e3 g/all/setup_app__app+change_account.deno.ts: 3c592cac27e9cdab0de6ae19270bcb08c7fa54355ad05253a12de2351894346b u/admin/hub_sync: aaf9fd803fa229f3029d1bb02bbe3cc422fce680cad39c4eec8dd1da115de102