1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-12-26 04:17:33 +00:00

weather objects: move init and print code to weather.c

This commit is contained in:
Phil Sutter 2009-10-04 19:42:01 +02:00
parent 6a2f58a25c
commit ad8dd36cb7
4 changed files with 180 additions and 163 deletions

View File

@ -1847,20 +1847,12 @@ static void generate_text_internal(char *p, int p_max_size,
#endif #endif
#ifdef WEATHER #ifdef WEATHER
OBJ(weather) { OBJ(weather) {
if (obj->data.weather.uri != NULL) { print_weather(obj, p, p_max_size);
weather_process_info(p, p_max_size, obj->data.weather.uri, obj->data.weather.data_type, obj->data.weather.interval);
} else {
NORM_ERR("error processing weather data, check that you have a valid XOAP key if using XOAP.");
}
} }
#endif #endif
#ifdef XOAP #ifdef XOAP
OBJ(weather_forecast) { OBJ(weather_forecast) {
if (obj->data.weather_forecast.uri != NULL) { print_weather_forecast(obj, p, p_max_size);
weather_forecast_process_info(p, p_max_size, obj->data.weather_forecast.uri, obj->data.weather_forecast.day, obj->data.weather_forecast.data_type, obj->data.weather_forecast.interval);
} else {
NORM_ERR("error processing weather forecast data, check that you have a valid XOAP key if using XOAP.");
}
} }
#endif #endif
#ifdef HAVE_LUA #ifdef HAVE_LUA

View File

@ -1467,90 +1467,11 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
#endif #endif
#ifdef WEATHER #ifdef WEATHER
END OBJ_ARG(weather, 0, "weather needs arguments: <uri> <locID> <data_type> [interval in minutes]") END OBJ_ARG(weather, 0, "weather needs arguments: <uri> <locID> <data_type> [interval in minutes]")
int argc; scan_weather_arg(obj, arg, free_at_crash);
float interval = 0;
char *locID = (char *) malloc(9 * sizeof(char));
char *uri = (char *) malloc(128 * sizeof(char));
char *data_type = (char *) malloc(32 * sizeof(char));
argc = sscanf(arg, "%119s %8s %31s %f", uri, locID, data_type, &interval);
if (argc >= 3) {
if (process_weather_uri(uri, locID, 0)) {
free(data_type);
free(uri);
free(locID);
CRIT_ERR(obj, free_at_crash, \
"could not recognize the weather uri");
}
obj->data.weather.uri = uri;
obj->data.weather.data_type = data_type;
/* Limit the data retrieval interval to half hour min */
if (interval < 30) {
interval = 30;
}
/* Convert to seconds */
obj->data.weather.interval = interval * 60;
free(locID);
DBGP("weather: fetching %s from %s every %d seconds", \
data_type, uri, obj->data.weather.interval);
} else {
free(data_type);
free(uri);
free(locID);
CRIT_ERR(obj, free_at_crash, "wrong number of arguments for $weather");
}
#endif #endif
#ifdef XOAP #ifdef XOAP
END OBJ_ARG(weather_forecast, 0, "weather_forecast needs arguments: <uri> <locID> <day> <data_type> [interval in minutes]") END OBJ_ARG(weather_forecast, 0, "weather_forecast needs arguments: <uri> <locID> <day> <data_type> [interval in minutes]")
int argc; scan_weather_forecast_arg(obj, arg, free_at_crash);
unsigned int day;
float interval = 0;
char *locID = (char *) malloc(9 * sizeof(char));
char *uri = (char *) malloc(128 * sizeof(char));
char *data_type = (char *) malloc(32 * sizeof(char));
argc = sscanf(arg, "%119s %8s %1u %31s %f", uri, locID, &day, data_type, &interval);
if (argc >= 4) {
if (process_weather_uri(uri, locID, 1)) {
free(data_type);
free(uri);
free(locID);
CRIT_ERR(obj, free_at_crash, \
"could not recognize the weather forecast uri");
}
obj->data.weather_forecast.uri = uri;
obj->data.weather_forecast.data_type = data_type;
/* Limit the day between 0 (today) and FORECAST_DAYS */
if (day >= FORECAST_DAYS) {
day = FORECAST_DAYS-1;
}
obj->data.weather_forecast.day = day;
/* Limit the data retrieval interval to 3 hours and an half */
if (interval < 210) {
interval = 210;
}
/* Convert to seconds */
obj->data.weather_forecast.interval = interval * 60;
free(locID);
DBGP("weather_forecast: fetching %s for day %d from %s every %d seconds", \
data_type, day, uri, obj->data.weather_forecast.interval);
} else {
free(data_type);
free(uri);
free(locID);
CRIT_ERR(obj, free_at_crash, "wrong number of arguments for $weather_forecast");
}
#endif #endif
#ifdef HAVE_LUA #ifdef HAVE_LUA
END OBJ_ARG(lua, 0, "lua needs arguments: <function name> [function parameters]") END OBJ_ARG(lua, 0, "lua needs arguments: <function name> [function parameters]")

View File

@ -23,10 +23,12 @@
* *
*/ */
#include "config.h"
#include "conky.h" #include "conky.h"
#include "logging.h" #include "logging.h"
#include "weather.h" #include "weather.h"
#include "temphelper.h" #include "temphelper.h"
#include "text_object.h"
#include "ccurl_thread.h" #include "ccurl_thread.h"
#include <time.h> #include <time.h>
#include <ctype.h> #include <ctype.h>
@ -37,6 +39,39 @@
#include <libxml/parser.h> #include <libxml/parser.h>
#include <libxml/xpath.h> #include <libxml/xpath.h>
/* WEATHER data */
typedef struct PWEATHER_ {
char lastupd[32];
#ifdef XOAP
char xoap_t[32];
char icon[3];
#endif /* XOAP */
int temp;
int dew;
int cc;
int bar;
int wind_s;
int wind_d;
int hmid;
int wc;
} PWEATHER;
#ifdef XOAP
#define FORECAST_DAYS 5
typedef struct PWEATHER_FORECAST_ {
int hi[FORECAST_DAYS];
int low[FORECAST_DAYS];
char icon[FORECAST_DAYS][3];
char xoap_t[FORECAST_DAYS][32];
char day[FORECAST_DAYS][9];
char date[FORECAST_DAYS][7];
int wind_s[FORECAST_DAYS];
int wind_d[FORECAST_DAYS];
int hmid[FORECAST_DAYS];
int ppcp[FORECAST_DAYS];
} PWEATHER_FORECAST;
#endif /* XOAP */
/* Xpath expressions for XOAP xml parsing */ /* Xpath expressions for XOAP xml parsing */
#define NUM_XPATH_EXPRESSIONS_CC 8 #define NUM_XPATH_EXPRESSIONS_CC 8
const char *xpath_expression_cc[NUM_XPATH_EXPRESSIONS_CC] = { const char *xpath_expression_cc[NUM_XPATH_EXPRESSIONS_CC] = {
@ -657,7 +692,7 @@ void wind_deg_to_dir(char *p, int p_max_size, int wind_deg) {
} }
#ifdef XOAP #ifdef XOAP
void weather_forecast_process_info(char *p, int p_max_size, char *uri, unsigned int day, char *data_type, int interval) static void weather_forecast_process_info(char *p, int p_max_size, char *uri, unsigned int day, char *data_type, int interval)
{ {
PWEATHER_FORECAST *data; PWEATHER_FORECAST *data;
@ -702,7 +737,7 @@ void weather_forecast_process_info(char *p, int p_max_size, char *uri, unsigned
} }
#endif /* XOAP */ #endif /* XOAP */
void weather_process_info(char *p, int p_max_size, char *uri, char *data_type, int interval) static void weather_process_info(char *p, int p_max_size, char *uri, char *data_type, int interval)
{ {
static const char *wc[] = { static const char *wc[] = {
"", "drizzle", "rain", "hail", "soft hail", "", "drizzle", "rain", "hail", "soft hail",
@ -777,12 +812,46 @@ void weather_process_info(char *p, int p_max_size, char *uri, char *data_type, i
timed_thread_unlock(curloc->p_timed_thread); timed_thread_unlock(curloc->p_timed_thread);
} }
#ifdef XOAP
/* xoap suffix for weather from weather.com */ /* xoap suffix for weather from weather.com */
static char *xoap_cc = NULL; static char *xoap_cc = NULL;
static char *xoap_df = NULL; static char *xoap_df = NULL;
static int process_weather_uri(char *uri, char *locID, int dayf UNUSED_ATTR)
{
/* locID MUST BE upper-case */
char *tmp_p = locID;
while (*tmp_p) {
*tmp_p = toupper(*tmp_p);
tmp_p++;
}
/* Construct complete uri */
#ifdef XOAP
if (strstr(uri, "xoap.weather.com")) {
if ((dayf == 0) && (xoap_cc != NULL)) {
strcat(uri, locID);
strcat(uri, xoap_cc);
} else if ((dayf == 1) && (xoap_df != NULL)) {
strcat(uri, locID);
strcat(uri, xoap_df);
} else {
free(uri);
uri = NULL;
}
} else
#endif /* XOAP */
if (strstr(uri, "weather.noaa.gov")) {
strcat(uri, locID);
strcat(uri, ".TXT");
} else if (!strstr(uri, "localhost") && !strstr(uri, "127.0.0.1")) {
return -1;
}
return 0;
}
#ifdef XOAP
/* /*
* TODO: make the xoap keys file readable from the config file * TODO: make the xoap keys file readable from the config file
* make the keys directly readable from the config file * make the keys directly readable from the config file
@ -821,38 +890,109 @@ void load_xoap_keys(void)
free(key); free(key);
free(xoap); free(xoap);
} }
#endif /* XOAP */
int process_weather_uri(char *uri, char *locID, int dayf UNUSED_ATTR) void scan_weather_forecast_arg(struct text_object *obj, const char *arg, void *free_at_crash)
{ {
/* locID MUST BE upper-case */ int argc;
char *tmp_p = locID; unsigned int day;
float interval = 0;
char *locID = (char *) malloc(9 * sizeof(char));
char *uri = (char *) malloc(128 * sizeof(char));
char *data_type = (char *) malloc(32 * sizeof(char));
while (*tmp_p) { argc = sscanf(arg, "%119s %8s %1u %31s %f", uri, locID, &day, data_type, &interval);
*tmp_p = toupper(*tmp_p);
tmp_p++; if (argc < 4) {
free(data_type);
free(uri);
free(locID);
CRIT_ERR(obj, free_at_crash, "wrong number of arguments for $weather_forecast");
}
if (process_weather_uri(uri, locID, 1)) {
free(data_type);
free(uri);
free(locID);
CRIT_ERR(obj, free_at_crash, \
"could not recognize the weather forecast uri");
} }
/* Construct complete uri */ obj->data.weather_forecast.uri = uri;
#ifdef XOAP obj->data.weather_forecast.data_type = data_type;
if (strstr(uri, "xoap.weather.com")) {
if ((dayf == 0) && (xoap_cc != NULL)) { /* Limit the day between 0 (today) and FORECAST_DAYS */
strcat(uri, locID); if (day >= FORECAST_DAYS) {
strcat(uri, xoap_cc); day = FORECAST_DAYS-1;
} else if ((dayf == 1) && (xoap_df != NULL)) {
strcat(uri, locID);
strcat(uri, xoap_df);
} else {
free(uri);
uri = NULL;
}
} else
#endif /* XOAP */
if (strstr(uri, "weather.noaa.gov")) {
strcat(uri, locID);
strcat(uri, ".TXT");
} else if (!strstr(uri, "localhost") && !strstr(uri, "127.0.0.1")) {
return -1;
} }
return 0; obj->data.weather_forecast.day = day;
/* Limit the data retrieval interval to 3 hours and an half */
if (interval < 210) {
interval = 210;
}
/* Convert to seconds */
obj->data.weather_forecast.interval = interval * 60;
free(locID);
DBGP("weather_forecast: fetching %s for day %d from %s every %d seconds", \
data_type, day, uri, obj->data.weather_forecast.interval);
}
void print_weather_forecast(struct text_object *obj, char *p, int p_max_size)
{
if (!obj->data.weather_forecast.uri) {
NORM_ERR("error processing weather forecast data, check that you have a valid XOAP key if using XOAP.");
return;
}
weather_forecast_process_info(p, p_max_size, obj->data.weather_forecast.uri, obj->data.weather_forecast.day, obj->data.weather_forecast.data_type, obj->data.weather_forecast.interval);
}
#endif /* XOAP */
void scan_weather_arg(struct text_object *obj, const char *arg, void *free_at_crash)
{
int argc;
float interval = 0;
char *locID = (char *) malloc(9 * sizeof(char));
char *uri = (char *) malloc(128 * sizeof(char));
char *data_type = (char *) malloc(32 * sizeof(char));
argc = sscanf(arg, "%119s %8s %31s %f", uri, locID, data_type, &interval);
if (argc < 3) {
free(data_type);
free(uri);
free(locID);
CRIT_ERR(obj, free_at_crash, "wrong number of arguments for $weather");
}
if (process_weather_uri(uri, locID, 0)) {
free(data_type);
free(uri);
free(locID);
CRIT_ERR(obj, free_at_crash, \
"could not recognize the weather uri");
}
obj->data.weather.uri = uri;
obj->data.weather.data_type = data_type;
/* Limit the data retrieval interval to half hour min */
if (interval < 30) {
interval = 30;
}
/* Convert to seconds */
obj->data.weather.interval = interval * 60;
free(locID);
DBGP("weather: fetching %s from %s every %d seconds", \
data_type, uri, obj->data.weather.interval);
}
void print_weather(struct text_object *obj, char *p, int p_max_size)
{
if (!obj->data.weather.uri) {
NORM_ERR("error processing weather data, check that you have a valid XOAP key if using XOAP.");
return;
}
weather_process_info(p, p_max_size, obj->data.weather.uri, obj->data.weather.data_type, obj->data.weather.interval);
} }

View File

@ -30,52 +30,16 @@
#ifndef WEATHER_H_ #ifndef WEATHER_H_
#define WEATHER_H_ #define WEATHER_H_
#include "config.h"
/* WEATHER data */
typedef struct PWEATHER_ {
char lastupd[32];
#ifdef XOAP
char xoap_t[32];
char icon[3];
#endif /* XOAP */
int temp;
int dew;
int cc;
int bar;
int wind_s;
int wind_d;
int hmid;
int wc;
} PWEATHER;
#ifdef XOAP
#define FORECAST_DAYS 5
typedef struct PWEATHER_FORECAST_ {
int hi[FORECAST_DAYS];
int low[FORECAST_DAYS];
char icon[FORECAST_DAYS][3];
char xoap_t[FORECAST_DAYS][32];
char day[FORECAST_DAYS][9];
char date[FORECAST_DAYS][7];
int wind_s[FORECAST_DAYS];
int wind_d[FORECAST_DAYS];
int hmid[FORECAST_DAYS];
int ppcp[FORECAST_DAYS];
} PWEATHER_FORECAST;
#endif /* XOAP */
/* Prototypes */ /* Prototypes */
void weather_free_info(void); void weather_free_info(void);
void weather_process_info(char *p, int p_max_size, char *uri, char *data_type, int interval);
#ifdef XOAP
void weather_forecast_process_info(char *p, int p_max_size, char *uri, unsigned int day, char *data_type, int interval);
#endif /* XOAP */
int process_weather_uri(char *uri, char *locID, int dayf);
#ifdef XOAP #ifdef XOAP
void load_xoap_keys(void); void load_xoap_keys(void);
void scan_weather_forecast_arg(struct text_object *, const char *, void *);
void print_weather_forecast(struct text_object *, char *, int);
#endif /* XOAP */ #endif /* XOAP */
void scan_weather_arg(struct text_object *, const char *, void *);
void print_weather(struct text_object *, char *, int);
#endif /*WEATHER_H_*/ #endif /*WEATHER_H_*/