-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimers-cli.c
More file actions
executable file
·258 lines (211 loc) · 5.29 KB
/
timers-cli.c
File metadata and controls
executable file
·258 lines (211 loc) · 5.29 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <curl/curl.h>
#include "libs/curl_conn.h"
#include "libs/easy_args.h"
#include "libs/logger.h"
#include "libs/easy_json.h"
#define URL "https://api.kitinfo.de/timers/"
struct MemoryStruct {
char* memory;
size_t size;
};
typedef struct {
LOGGER log;
char* pre;
char* post;
int max;
struct MemoryStruct* data;
} Config;
size_t handleData(char* ptr, size_t size, size_t nmemb, Config* config) {
int length = size * nmemb;
config->data->memory = realloc(config->data->memory, config->data->size + length + 1);
// out of memory
if (config->data->memory == NULL) {
return 0;
}
memcpy(&(config->data->memory[config->data->size]), ptr, length);
config->data->size += length;
config->data->memory[config->data->size] = 0;
return size * nmemb;
}
int count_str(char* src) {
int counter = 0;
while (*src != 0) {
//printf("%02x ", (unsigned char) *src);
switch(*src & 0xE0) {
case 0xC0:
counter++;
break;
case 0xE0:
counter += 2;
break;
}
src++;
}
return counter;
}
int convert_str_to_int(ejson_key* next, int* container) {
int status;
char* value;
char* endptr;
status = ejson_get_string(next->value, &value);
if (status != EJSON_OK ) {
return status;
}
*container = strtoul(value, &endptr, 10);
if (value == endptr) {
return 1;
}
return EJSON_OK;
}
bool print_item(Config* config, ejson_base* ejson) {
if (!ejson || ejson->type != EJSON_OBJECT) {
logprintf(config->log, LOG_ERROR, "The timers item is not an object.\n");
return false;
}
char* event = "";
char* message = "";
struct tm event_time = {0};
long i;
int status;
ejson_key* next;
for (i = 0; i < ejson->object.length; i++) {
next = ejson->object.keys[i];
if (!next->key) {
continue;
} else if (!strcmp(next->key, "event")) {
status = ejson_get_string(next->value, &event);
} else if (!strcmp(next->key, "message")) {
status = ejson_get_string(next->value, &message);
} else if (!strcmp(next->key, "day")) {
status = convert_str_to_int(next, &event_time.tm_mday);
} else if (!strcmp(next->key, "month")) {
status = convert_str_to_int(next, &event_time.tm_mon);
event_time.tm_mon--;
} else if (!strcmp(next->key, "year")) {
status = convert_str_to_int(next, &event_time.tm_year);
event_time.tm_year -= 1900;
} else if (!strcmp(next->key, "hour")) {
status = convert_str_to_int(next, &event_time.tm_hour);
} else if (!strcmp(next->key, "minute")) {
status = convert_str_to_int(next, &event_time.tm_min);
} else if (!strcmp(next->key, "second")) {
status = convert_str_to_int(next, &event_time.tm_sec);
} else {
status = EJSON_OK;
}
if (status != EJSON_OK) {
logprintf(config->log, LOG_ERROR, "Cannot parse key: %s\n", next->key);
return false;
}
}
char* words[] = {
"seconds",
"minutes",
"hours",
"days"
};
time_t act = mktime(&event_time);
time_t now = time(NULL);
time_t diff = act - now;
if (diff < -3600 * 24) {
return true;
}
char* word = NULL;
if (diff < 60) {
word = words[0];
} else if (diff < 60 * 60) {
diff /= 60;
word = words[1];
} else if (diff < 60 * 60 * 24) {
diff /= 60 * 60;
word = words[2];
} else {
diff /= 60 * 60 * 24;
word = words[3];
}
printf("%s%s\n%s", config->pre, event, asctime(&event_time));
if (act - now > 0) {
printf("%zd %s left\n", diff, word);
}
if (act - now < 3600 && strlen(message) > 0) {
printf("(%s)", message);
}
printf("%s", config->post);
fflush(stdout);
sleep(10);
return true;
}
void print_list(Config* config, ejson_base* ejson) {
if (!ejson) {
return;
}
if (ejson->type != EJSON_OBJECT) {
logprintf(config->log, LOG_ERROR, "The json root must be an object.\n");
}
ejson = ejson_find_by_key(&ejson->object, "timers", false, false);
if (!ejson) {
logprintf(config->log, LOG_ERROR, "Something is wrong with the json (Cannot find timers key).\n");
return;
}
if (ejson->type != EJSON_ARRAY) {
logprintf(config->log, LOG_ERROR, "The type of the key 'timers' must be an array.\n");
return;
}
if (ejson->array.length == 0) {
printf("%sKeine Timers%s", config->pre, config->post);
sleep(1200);
}
long i = 0;
for (i = 0; i < ejson->array.length; i++) {
if (!print_item(config, ejson->array.values[i])) {
logprintf(config->log, LOG_ERROR, "Cannot print item.\n");
printf("%sKann Timer nicht anzeigen%s", config->pre, config->post);
sleep(60);
return;
}
if (i > config->max) {
break;
}
}
}
void run(Config* config, char* url) {
config->data->memory = malloc(1);
config->data->size = 0;
int status = get_request(config->log, url, handleData, config, "", "");
logprintf(config->log, LOG_INFO, "STATUS: %d\n", status);
ejson_base* ejson = NULL;
if (ejson_parse(config->data->memory, config->data->size, &ejson) != EJSON_OK) {
printf("%s", config->pre);
logprintf(config->log, LOG_ERROR, "ERROR in parsing json.\n");
sleep(300);
} else {
print_list(config, ejson);
}
ejson_cleanup(ejson);
free(config->data->memory);
}
int main(int argc, char** argv) {
LOGGER log = {
.stream = stdout,
.verbosity = 4
};
struct MemoryStruct ms = {
.size = 0,
};
Config config = {
.log = log,
.pre = "\f",
.post = "\n",
.max = 10,
.data = &ms
};
while (true) {
run(&config, URL);
}
return 0;
}