summaryrefslogtreecommitdiffstats
path: root/src/detection/weather/weather.c
blob: 69df6429007505462811c50a0179ce4bba5cd6b2 (plain) (blame)
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
#include "weather.h"
#include "common/networking.h"

#define FF_UNITIALIZED ((const char*) (uintptr_t) -1)
static FFNetworkingState state;
static const char* status = FF_UNITIALIZED;

void ffPrepareWeather(FFWeatherOptions* options) {
    if (status != FF_UNITIALIZED) {
        fputs("Error: Weather module can only be used once due to internal limitations\n", stderr);
        exit(1);
    }

    state.timeout = options->timeout;

    FF_STRBUF_AUTO_DESTROY path = ffStrbufCreateS("/");
    if (options->location.length) {
        ffStrbufAppend(&path, &options->location);
    }
    ffStrbufAppendS(&path, "?format=");
    ffStrbufAppend(&path, &options->outputFormat);
    switch (instance.config.display.tempUnit) {
        case FF_TEMPERATURE_UNIT_CELSIUS:
            ffStrbufAppendS(&path, "&m");
            break;
        case FF_TEMPERATURE_UNIT_FAHRENHEIT:
            ffStrbufAppendS(&path, "&u");
            break;
        default:
            break;
    }
    status = ffNetworkingSendHttpRequest(&state, "wttr.in", path.chars, "User-Agent: curl/0.0.0\r\n");
}

const char* ffDetectWeather(FFWeatherOptions* options, FFstrbuf* result) {
    if (status == FF_UNITIALIZED) {
        ffPrepareWeather(options);
    }

    if (status != NULL) {
        return status;
    }

    ffStrbufEnsureFree(result, 4095);
    const char* error = ffNetworkingRecvHttpResponse(&state, result);

    state = (FFNetworkingState) {};
    status = FF_UNITIALIZED;

    if (error == NULL) {
        ffStrbufSubstrAfterFirstS(result, "\r\n\r\n");
        ffStrbufTrimRightSpace(result);
    } else {
        return error;
    }

    if (result->length == 0) {
        return "Empty server response received";
    }

    return NULL;
}