channel2tsv.c - frontends - front-ends for some sites (experiment)
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
channel2tsv.c (1957B)
---
1 #include <sys/socket.h>
2 #include <sys/types.h>
3
4 #include <ctype.h>
5 #include <errno.h>
6 #include <netdb.h>
7 #include <stdarg.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12
13 #include "https.h"
14 #include "util.h"
15 #include "youtube.h"
16
17 #define OUT(s) fputs((s), stdout)
18 #define OUTESCAPE(s) printescape((s))
19
20 /* print: ignore control-characters */
21 void
22 printescape(const char *s)
23 {
24 for (; *s; ++s)
25 if (!iscntrl((unsigned char)*s))
26 fputc(*s, stdout);
27 }
28
29 int
30 render(struct search_response *r)
31 {
32 struct item *videos = r->items;
33 size_t i;
34
35 if (pledge("stdio", NULL) == -1) {
36 fprintf(stderr, "pledge: %s\n", strerror(errno));
37 exit(1);
38 }
39
40 for (i = 0; i < r->nitems; i++) {
41 switch (videos[i].linktype) {
42 case Channel:
43 case Movie:
44 case Playlist:
45 continue;
46 default:
47 break;
48 }
49
50 OUTESCAPE(videos[i].id);
51 OUT("\t");
52 if (videos[i].id[0]) {
53 OUT("https://www.youtube.com/embed/");
54 OUTESCAPE(videos[i].id);
55 }
56 OUT("\t");
57 OUTESCAPE(videos[i].title);
58 OUT("\t");
59 OUTESCAPE(videos[i].publishedat);
60 OUT("\t");
61 OUTESCAPE(videos[i].viewcount);
62 OUT("\t");
63 OUTESCAPE(videos[i].duration);
64 OUT("\n");
65 }
66
67 return 0;
68 }
69
70 static void
71 usage(const char *argv0)
72 {
73 fprintf(stderr, "usage: %s <channelid>\n", argv0);
74 exit(1);
75 }
76
77 int
78 main(int argc, char *argv[])
79 {
80 struct search_response *r;
81 char channelid[1024];
82
83 if (pledge("stdio dns inet rpath unveil", NULL) == -1) {
84 fprintf(stderr, "pledge: %s\n", strerror(errno));
85 exit(1);
86 }
87 if (unveil(TLS_CA_CERT_FILE, "r") == -1) {
88 fprintf(stderr, "unveil: %s\n", strerror(errno));
89 exit(1);
90 }
91 if (unveil(NULL, NULL) == -1) {
92 fprintf(stderr, "unveil: %s\n", strerror(errno));
93 exit(1);
94 }
95
96 if (argc < 2 || !argv[1][0])
97 usage(argv[0]);
98 if (!uriencode(argv[1], channelid, sizeof(channelid)))
99 usage(argv[0]);
100
101 r = youtube_channel_videos(channelid);
102 if (!r || r->nitems == 0)
103 exit(1);
104
105 render(r);
106
107 return 0;
108 }