stweath.c (4776B) - View raw
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201/* Copyright (C) 2025 awy <awy@awy.one> This file is part of stbar. stbar is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. stbar is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with stbar. If not, see <https://www.gnu.org/licenses/>. */ #include <sys/stat.h> #include <curl/curl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <unistd.h> #include "../lib/cjson/cJSON.h" #include "../lib/util.h" typedef struct { int code; const char *icon; } WeatherIcon; const char *geticon(int code) { static const WeatherIcon icons[] = { {113, ""}, {116, ""}, {119, ""}, {122, ""}, {143, ""}, {248, ""}, {260, ""}, {176, ""}, {179, ""}, {182, ""}, {185, ""}, {263, ""}, {266, ""}, {281, ""}, {284, ""}, {293, ""}, {296, ""}, {299, ""}, {302, ""}, {305, ""}, {308, ""}, {311, ""}, {314, ""}, {317, ""}, {350, ""}, {353, ""}, {356, ""}, {359, ""}, {362, ""}, {365, ""}, {368, ""}, {392, ""}, {200, ""}, {227, ""}, {230, ""}, {320, ""}, {323, ""}, {326, ""}, {374, ""}, {377, ""}, {386, ""}, {389, ""}, {329, ""}, {332, ""}, {335, ""}, {338, ""}, {371, ""}, {395, ""}}; size_t n = sizeof(icons) / sizeof(icons[0]); for (size_t i = 0; i < n; i++) { if (icons[i].code == code) return icons[i].icon; } return "unknown"; } int getforecast(const char *path) { FILE *fp; CURL *handle; CURLcode res; if (!(fp = fopen(path, "w"))) die("failed to open report:"); handle = curl_easy_init(); if (!handle) { fclose(fp); die("failed to create curl handle"); } curl_easy_setopt(handle, CURLOPT_URL, "https://wttr.in/?format=j1"); curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, NULL); curl_easy_setopt(handle, CURLOPT_WRITEDATA, fp); res = curl_easy_perform(handle); if (res != CURLE_OK) { fclose(fp); die("curl failed"); } curl_easy_cleanup(handle); fclose(fp); return 0; } int showforecast(const char *path) { FILE *fp; long size; char *data; const char *icon; cJSON *root, *temp, *wcode, *rain; if (!(fp = fopen(path, "rb"))) die("fopen:"); fseek(fp, 0, SEEK_END); size = ftell(fp); fseek(fp, 0, SEEK_SET); data = malloc(size + 1); if (!(data = malloc(size + 1))) { fclose(fp); die("failed to allocate memory"); } fread(data, 1, size, fp); data[size] = '\0'; fclose(fp); if (!(root = cJSON_Parse(data))) { free(data); die("failed to parse json"); } rain = cJSON_GetArrayItem(cJSON_GetObjectItem(root, "weather"), 0); rain = cJSON_GetArrayItem(cJSON_GetObjectItem(rain, "hourly"), 0); rain = cJSON_GetObjectItem(rain, "chanceofrain"); temp = cJSON_GetObjectItem(root, "current_condition"); temp = cJSON_GetArrayItem(temp, 0); wcode = cJSON_GetObjectItem(temp, "weatherCode"); temp = cJSON_GetObjectItem(temp, "temp_C"); icon = geticon(atoi(wcode->valuestring)); if (atoi(rain->valuestring) != 0) printf(" %s%% ", rain->valuestring); printf("%s %s°C", icon, temp->valuestring); cJSON_Delete(root); return 0; } int checkforecast(const char *path) { time_t now; char *buff; struct stat st; int tries, force; /* check if there is WEATHERFORCEUPDATE environment variable */ force = 0; buff = getenv("WEATHERFORCEUPDATE"); if (buff && buff[0] == '1') { force = 1; } if (access(path, F_OK) != -1) { if (stat(path, &st) != 0) die("failed to get modification time"); } else { force = 1; } now = time(NULL); if (now == ((time_t)-1)) { return 1; } /* check if 30 mins passed then get new forecast */ if (((now - st.st_mtime) >= 1800) || force) { tries = 0; /* try 100 fast retries */ while (tries < 100) { if (getforecast(path) == 0) { break; } tries++; sleep(1); } } showforecast(path); return 0; } int stweath(void) { char *env; char path[1024]; if (!(env = getenv("XDG_CACHE_HOME"))) die("XDG_CACHE_HOME is not set"); strcpy(path, env); strcat(path, "/weatherreport.json"); curl_global_init(CURL_GLOBAL_ALL); checkforecast(path); curl_global_cleanup(); return 0; }