youtube: gopher: improvements - frontends - front-ends for some sites (experiment) (DIR) Log (DIR) Files (DIR) Refs (DIR) README (DIR) LICENSE --- (DIR) commit 8d9f251630daa1827e8651058d0bc31910dc1106 (DIR) parent 9f0f2a73c85c3fc233e5ad5e806c4e42c72e838f (HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org> Date: Sat, 2 May 2020 16:42:56 +0200 youtube: gopher: improvements Diffstat: M youtube/gopher.c | 95 +++++++++++++++++++------------ 1 file changed, 58 insertions(+), 37 deletions(-) --- (DIR) diff --git a/youtube/gopher.c b/youtube/gopher.c @@ -18,8 +18,40 @@ #define OUTTEXT(s) gophertext(stdout, s, strlen(s)); #define OUTLINK(s) gophertext(stdout, s, strlen(s)); -static const char *server = "127.0.0.1"; -static const char *port = "70"; +static const char *host = "127.0.0.1", *port = "70"; + +void +line(int _type, const char *username, const char *selector) +{ + putchar(_type); + OUTTEXT(username); + putchar('\t'); + OUTLINK(selector); + printf("\t%s\t%s\r\n", host, port); +} + +void +error(const char *s) +{ + line('3', s, ""); +} + +void +info(const char *s) +{ + line('i', s, ""); +} + +void +header(void) +{ +} + +void +footer(void) +{ + printf(".\r\n"); +} int render(struct search_response *r) @@ -27,11 +59,10 @@ render(struct search_response *r) struct item *videos = r->items; size_t i; - if (pledge("stdio", NULL) == -1) { - fprintf(stderr, "pledge: %s\n", strerror(errno)); + if (pledge("stdio", NULL) == -1) exit(1); - } + header(); for (i = 0; i < r->nitems; i++) { if (videos[i].id[0]) putchar('h'); @@ -61,7 +92,7 @@ render(struct search_response *r) OUT("URL:https://www.youtube.com/embed/"); OUTLINK(videos[i].id); } - printf("\t%s\t%s\r\n", server, port); + printf("\t%s\t%s\r\n", host, port); if (videos[i].channelid[0] || videos[i].userid[0]) { OUT("hAtom feed of "); @@ -75,70 +106,60 @@ render(struct search_response *r) OUT("user="); OUTLINK(videos[i].userid); } - printf("\t%s\t%s\r\n", server, port); + printf("\t%s\t%s\r\n", host, port); } if (videos[i].duration[0]) { OUT("iDuration: " ); OUTTEXT(videos[i].duration); - printf("\t%s\t%s\t%s\r\n", "", server, port); + printf("\t%s\t%s\t%s\r\n", "", host, port); } if (videos[i].publishedat[0]) { OUT("iPublished: "); OUTTEXT(videos[i].publishedat); - printf("\t%s\t%s\t%s\r\n", "", server, port); + printf("\t%s\t%s\t%s\r\n", "", host, port); } if (videos[i].viewcount[0]) { OUT("iViews: "); OUTTEXT(videos[i].viewcount); - printf("\t%s\t%s\t%s\r\n", "", server, port); + printf("\t%s\t%s\t%s\r\n", "", host, port); } - printf("i%s\t%s\t%s\t%s\r\n", "", "", server, port); - printf("i%s\t%s\t%s\t%s\r\n", "", "", server, port); + info(""); + info(""); } - printf(".\r\n"); + footer(); return 0; } -static void -usage(const char *argv0) -{ - fprintf(stderr, "usage: %s <keywords>\n", argv0); - exit(1); -} - int -main(int argc, char *argv[]) +main(void) { struct search_response *r; char *p, search[1024]; - if (pledge("stdio dns inet rpath unveil", NULL) == -1) { - fprintf(stderr, "pledge: %s\n", strerror(errno)); - exit(1); - } - if (unveil(TLS_CA_CERT_FILE, "r") == -1) { - fprintf(stderr, "unveil: %s\n", strerror(errno)); + if (pledge("stdio dns inet rpath unveil", NULL) == -1) exit(1); - } - if (unveil(NULL, NULL) == -1) { - fprintf(stderr, "unveil: %s\n", strerror(errno)); + if (unveil(TLS_CA_CERT_FILE, "r") == -1 || + unveil(NULL, NULL) == -1) exit(1); - } if ((p = getenv("SERVER_NAME"))) - server = p; + host = p; if ((p = getenv("SERVER_PORT"))) port = p; - if (argc < 2 || !argv[1][0]) - usage(argv[0]); - if (!uriencode(argv[1], search, sizeof(search))) - usage(argv[0]); + if (!(p = getenv("X_GOPHER_SEARCH"))) /* geomyidae */ + p = getenv("SEARCHREQUEST"); /* gophernicus */ + + if (p && !uriencode(p, search, sizeof(search))) { + error("Invalid search"); + printf(".\r\n"); + exit(1); + } r = youtube_search(search, "", "", "", "relevance"); if (!r || r->nitems == 0) { - printf("iNo videos found\t%s\t%s\t%s\r\n", "", server, port); + error("No videos found"); printf(".\r\n"); exit(1); }