Weather: add new module

This commit is contained in:
李通洲
2022-09-29 17:05:01 +08:00
parent c72c6c4a27
commit 9cf95620a9
13 changed files with 113 additions and 9 deletions
+2 -2
View File
@@ -10,7 +10,7 @@ static int sockfd;
void ffPreparePublicIp(FFinstance* instance)
{
if(instance->config.publicIpUrl.length == 0)
sockfd = ffNetworkingSendHttpRequest("ipinfo.io", "/ip", instance->config.publicIpTimeout);
sockfd = ffNetworkingSendHttpRequest("ipinfo.io", "/ip", NULL, instance->config.publicIpTimeout);
else
{
FFstrbuf host;
@@ -27,7 +27,7 @@ void ffPreparePublicIp(FFinstance* instance)
host.chars[pathStartIndex] = '\0';
}
sockfd = ffNetworkingSendHttpRequest(host.chars, path.length == 0 ? "/" : path.chars, instance->config.publicIpTimeout);
sockfd = ffNetworkingSendHttpRequest(host.chars, path.length == 0 ? "/" : path.chars, NULL, instance->config.publicIpTimeout);
ffStrbufDestroy(&path);
ffStrbufDestroy(&host);
+55
View File
@@ -0,0 +1,55 @@
#include "fastfetch.h"
#include "common/printing.h"
#include "common/networking.h"
#define FF_WEATHER_MODULE_NAME "Weather"
#define FF_WEATHER_NUM_FORMAT_ARGS 1
static int sockfd;
void ffPrepareWeather(FFinstance* instance)
{
FFstrbuf path;
ffStrbufInitS(&path, "/?format=");
ffStrbufAppend(&path, &instance->config.weatherOutputFormat);
sockfd = ffNetworkingSendHttpRequest("wttr.in", path.chars, "User-Agent: curl/0.0.0\r\n", instance->config.weatherTimeout);
ffStrbufDestroy(&path);
}
void ffPrintWeather(FFinstance* instance)
{
if(sockfd == 0)
ffPrepareWeather(instance);
if(sockfd < 0)
{
ffPrintError(instance, FF_WEATHER_MODULE_NAME, 0, &instance->config.weather, "Failed to connect to 'wttr.in'");
return;
}
FFstrbuf result;
ffStrbufInitA(&result, 4096);
ffNetworkingRecvHttpResponse(sockfd, &result);
ffStrbufSubstrAfterFirstS(&result, "\r\n\r\n");
if(result.length == 0)
{
ffPrintError(instance, FF_WEATHER_MODULE_NAME, 0, &instance->config.weather, "Failed to receive the server response");
ffStrbufDestroy(&result);
return;
}
if(instance->config.weather.outputFormat.length == 0)
{
ffPrintLogoAndKey(instance, FF_WEATHER_MODULE_NAME, 0, &instance->config.weather.key);
ffStrbufPutTo(&result, stdout);
}
else
{
ffPrintFormat(instance, FF_WEATHER_MODULE_NAME, 0, &instance->config.weather, FF_WEATHER_NUM_FORMAT_ARGS, (FFformatarg[]) {
{FF_FORMAT_ARG_TYPE_STRBUF, &result}
});
}
ffStrbufDestroy(&result);
}