forked from jiiksteri/yt_history
/
feed.c
430 lines (355 loc) · 9.55 KB
/
feed.c
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#include "feed.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <event2/buffer.h>
#include <event2/http.h>
#include <event2/keyvalq_struct.h>
#include <expat.h>
#include "verbose.h"
/* Fields we're interested in. Now, the code that actually figures out
* we're interested in something can't really stand daylight..
*/
enum {
F_TITLE,
F_UPLOADER,
F_CONTENT,
F_PLAYER,
F_UPDATED,
F_PUBLISHED,
F_THUMBNAIL,
F_LINK_PREVIOUS,
F_LINK_NEXT,
F__COUNT,
};
struct feed {
XML_Parser parser;
struct evbuffer *sink;
int header_sent;
int navi_sent;
/* parser state */
int in_entry;
char *fields[F__COUNT];
struct evbuffer *cdata_buf;
};
static void XMLCALL cdata(void *user_data, const char *s, int len)
{
struct feed *feed = user_data;
if (feed->cdata_buf != NULL) {
evbuffer_add(feed->cdata_buf, s, len);
}
}
static void clear_fields(struct feed *feed)
{
int i;
for (i = 0; i < F__COUNT; i++) {
free(feed->fields[i]);
feed->fields[i] = NULL;
}
}
static int find_cdata_target(const char *element)
{
int ret;
if (strcmp(element, "updated") == 0) {
ret = F_UPDATED;
} else if (strcmp(element, "published") == 0) {
ret = F_PUBLISHED;
} else if (strcmp(element, "title") == 0) {
ret = F_TITLE;
} else {
ret = -1;
}
return ret;
}
static const char *find_attribute_value(const char **attrs, const char *name)
{
int i;
const char *value;
for (i = 0, value = NULL; attrs[i]; i += 2) {
if (strcmp(attrs[i], name) == 0) {
value = attrs[i+1];
break;
}
}
return value;
}
static void copy_attribute_override(char **tgt, const char **attrs, const char *name)
{
const char *new_value;
new_value = find_attribute_value(attrs, name);
if (new_value != NULL) {
if (*tgt != NULL) {
free(*tgt);
}
*tgt = strdup(new_value);
}
}
static void copy_attribute_keep_old(char **tgt, const char **attrs, const char *name)
{
if (*tgt == NULL) {
copy_attribute_override(tgt, attrs, name);
}
}
static void handle_navigation_link(struct feed *feed, const char **attrs)
{
const char *rel;
int field;
rel = find_attribute_value(attrs, "rel");
if (rel != NULL) {
if (strcmp(rel, "next") == 0) {
field = F_LINK_NEXT;
} else if (strcmp(rel, "previous") == 0) {
field = F_LINK_PREVIOUS;
} else {
field = -1;
}
if (field != -1) {
copy_attribute_keep_old(&feed->fields[field], attrs, "href");
}
}
}
static void XMLCALL element_start(void *user_data, const char *element, const char **attrs)
{
struct feed *feed = user_data;
if (strcmp(element, "entry") == 0) {
feed->in_entry++;
} else if (feed->in_entry == 0 && strcmp(element, "link") == 0) {
handle_navigation_link(feed, attrs);
} else if (feed->in_entry) {
if (find_cdata_target(element) != -1) {
feed->cdata_buf = evbuffer_new();
}
if (strcmp(element, "content") == 0) {
copy_attribute_override(&feed->fields[F_CONTENT], attrs, "src");
} else if (strcmp(element, "media:thumbnail") == 0) {
copy_attribute_keep_old(&feed->fields[F_THUMBNAIL], attrs, "url");
} else if (strcmp(element, "media:player") == 0) {
copy_attribute_keep_old(&feed->fields[F_PLAYER], attrs, "url");
} else if (strcmp(element, "media:credit") == 0) {
/* This is tricky. There are other credit entries than
* uploader, probably */
copy_attribute_keep_old(&feed->fields[F_UPLOADER], attrs, "yt:display");
}
}
}
static void filter_query_into(struct evbuffer *buf, const char *full_url)
{
struct evkeyvalq params;
const char *val;
/* FIXME: Remember that passthrough thing we do if
* there's _any_ alt=... format specifier? Yeah, that one.
* Great idea. Now we get to jump through hoops dropping it from
* the next/previous links.
*
* Just go make passthrough an explicit url parameter, and teach
* /list how to deal with alt=atom, so we don't have to do this.
*/
memset(¶ms, 0, sizeof(params));
if (evhttp_parse_query_str(full_url, ¶ms) == -1) {
/* Bad idea, we're kind of committed to this :( */
return;
}
/* If we'd dig into the internals we could just iterate the parameters
* and skip alt=... As it is, we'll just pick start-index and
* max-results. Those are the only ones /list? will pass to Google
* anyway
*/
if ((val = evhttp_find_header(¶ms, "start-index")) != NULL) {
evbuffer_add_printf(buf, "start-index=%s&", val);
}
if ((val = evhttp_find_header(¶ms, "max-results")) != NULL) {
evbuffer_add_printf(buf, "max-results=%s&", val);
}
evhttp_clear_headers(¶ms);
}
static void parse_and_send_link(struct evbuffer *buf, const char *full_url,
const char *id, const char *name)
{
evbuffer_add_printf(buf, "<a id='%s' href='", id);
if (full_url != NULL) {
evbuffer_add_printf(buf, "/list?");
filter_query_into(buf, full_url);
}
evbuffer_add_printf(buf, "'>%s</a>\n", name);
}
static void send_navi(struct feed *feed)
{
evbuffer_add_printf(feed->sink, "<div class='navi'>\n");
if (feed->fields[F_LINK_PREVIOUS] != NULL) {
parse_and_send_link(feed->sink, feed->fields[F_LINK_PREVIOUS],
"prev", "Previous");
}
if (feed->fields[F_LINK_NEXT] != NULL) {
parse_and_send_link(feed->sink, feed->fields[F_LINK_NEXT],
"next", "Next");
}
evbuffer_add_printf(feed->sink, "</div>");
}
static char *amp_to_amp(char *to, size_t sz, const char *from)
{
int ind = 0;
while (from && *from && ind < sz) {
if (*from == '&') {
to[ind++] = '&';
to[ind++] = 'a';
to[ind++] = 'm';
to[ind++] = 'p';
to[ind++] = ';';
} else {
to[ind++] = *from;
}
from++;
}
if (ind == sz) {
verbose(ERROR, "%s(): player links grow tall here."
" I had to truncate this: '%s'\n",
__func__, from);
ind = sz-1;
}
to[ind++] = '\0';
return to;
}
static void flush_element(struct feed *feed)
{
char clean_player[128];
if (!feed->navi_sent) {
send_navi(feed);
feed->navi_sent = 1;
}
/* Sure, browsers deal with unescaped &'s in URLs just fine.
* But our fancy unit... massive expat-using html-parsing test
* thing is picky.
*/
amp_to_amp(clean_player, sizeof(clean_player), feed->fields[F_PLAYER]);
evbuffer_add_printf(feed->sink,
"<div class='entry'>\n"
" <a href='%s'>\n"
" <img src='%s' class='thumbnail'/>\n"
" </a>\n"
" <div class='info'>\n"
" <p class='title'>\n"
" <a href='%s'>%s</a>\n"
" </p>\n"
" <p class='uploader'>%s</p>\n"
" <div class='dates'>\n"
" <p>Last: %s</p>\n"
" <p>First: %s</p>\n"
" </div>\n"
" </div>\n"
"</div>\n",
clean_player,
feed->fields[F_THUMBNAIL],
clean_player,
feed->fields[F_TITLE],
feed->fields[F_UPLOADER],
feed->fields[F_UPDATED],
feed->fields[F_PUBLISHED]);
}
static void copy_cdata_to_field(struct feed *feed, int field)
{
int needed;
needed = evbuffer_get_length(feed->cdata_buf) + 1;
feed->fields[field] = malloc(needed);
evbuffer_remove(feed->cdata_buf, feed->fields[field], needed);
feed->fields[field][needed-1] = '\0';
}
static void XMLCALL element_end(void *user_data, const char *element)
{
struct feed *feed = user_data;
int cdata_target;
if (strcmp("entry", element) == 0) {
flush_element(feed);
clear_fields(feed);
if (feed->cdata_buf != NULL) {
evbuffer_free(feed->cdata_buf);
feed->cdata_buf = NULL;
}
feed->in_entry--;
} else if (feed->in_entry && (cdata_target = find_cdata_target(element)) != -1) {
copy_cdata_to_field(feed, cdata_target);
evbuffer_free(feed->cdata_buf);
feed->cdata_buf = NULL;
}
}
int feed_init(struct feed **feedp, struct evbuffer *sink)
{
struct feed *feed;
if ((feed = malloc(sizeof(*feed))) == NULL) {
return ENOMEM;
}
memset(feed, 0, sizeof(*feed));
feed->parser = XML_ParserCreate(NULL);
XML_SetUserData(feed->parser, feed);
XML_SetElementHandler(feed->parser, element_start, element_end);
XML_SetCharacterDataHandler(feed->parser, cdata);
feed->sink = sink;
*feedp = feed;
return 0;
}
void feed_destroy(struct feed *feed)
{
if (feed != NULL) {
XML_ParserFree(feed->parser);
if (feed->cdata_buf != NULL) {
evbuffer_free(feed->cdata_buf);
}
clear_fields(feed);
free(feed);
}
}
static const char *HEADER =
"<html>\n"
"<head>\n"
" <style>\n"
" body {"
" background-color: #666666;"
" font-family: sans-serif;"
" font-size: 16px;"
" margin-top: 20px;"
" }\n"
" a { color: inherit; }"
" div.entry {"
" margin-left: 20px; margin-right: 20px;"
" margin-top: 10px;"
" margin-bottom: 10px;"
" background-color: #888888;"
" }\n"
" p.uploader { padding-left: 10px; padding-top: 5px; }\n"
" div.info {"
" display: inline-block;"
" }\n"
" div.info p { margin-left: 10px; margin-top: 0em; margin-bottom: 0em; }\n"
" div.info p.title { margin-top: 10px; }"
" div.info p.uploader { font-size: 12px; }"
" div.info div.dates { font-size: 10px; margin-top: 16px; }\n"
" img.thumbnail {"
" padding: 5px;"
" background-color: black;"
" vertical-align: top;"
" }\n"
" </style>\n"
" <title>watch_history</title>\n"
"</head>\n"
"<body>\n";
static const char *FOOTER = "\n</body></html>\n";
int feed_consume(struct feed *feed, struct evbuffer *buf)
{
char input[1024];
int removed;
if (!feed->header_sent) {
evbuffer_add(feed->sink, HEADER, strlen(HEADER));
feed->header_sent++;
}
while ((removed = evbuffer_remove(buf, input, sizeof(input))) > 0) {
XML_Parse(feed->parser, input, removed, 0);
/* TODO: Handle error. */
}
return 0;
}
int feed_final(struct feed *feed)
{
char one;
XML_Parse(feed->parser, &one, 0, 1);
evbuffer_add(feed->sink, FOOTER, strlen(FOOTER));
return 0;
}