youtube: fix for when a video has no views - frontends - front-ends for some sites (experiment)
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit 68292dabe281a1a107cf65682df9ce0237759718
(DIR) parent 0cff6e88783acaf162e35bad870aee41c6cc4f3e
(HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Mon, 7 Oct 2024 20:32:56 +0200
youtube: fix for when a video has no views
Youtube then has the text "No views". This would be parsed and formatted to an
empty string showing " views".
Change it so if there is a parse error or empty it will just print "0 views".
Diffstat:
M util.c | 13 +++++++++----
M util.h | 2 +-
M youtube/cgi.c | 8 +++++---
M youtube/cli.c | 8 +++++---
M youtube/gopher.c | 6 ++++--
5 files changed, 24 insertions(+), 13 deletions(-)
---
(DIR) diff --git a/util.c b/util.c
@@ -222,12 +222,12 @@ durationstr(long secs, char *buf, size_t bufsiz)
return r;
}
-/* print views with thousand separators */
-void
+/* print views with thousand separators, returns printed characters */
+size_t
printnumsep(const char *s)
{
const char *p;
- int ndigits = 0;
+ size_t n = 0, ndigits = 0;
/* first count all digits */
for (p = s; *p; p++)
@@ -239,11 +239,16 @@ printnumsep(const char *s)
continue;
putchar(*p);
+ n++;
ndigits--;
/* show separator on every 3 digits and when there are
digits remaining */
- if ((ndigits % 3) == 0 && ndigits > 0)
+ if ((ndigits % 3) == 0 && ndigits > 0) {
putchar(',');
+ n++;
+ }
}
+
+ return n;
}
(DIR) diff --git a/util.h b/util.h
@@ -21,7 +21,7 @@ int friendlytime(time_t now, time_t t);
char *getparam(const char *query, const char *s);
void gophertext(FILE *fp, const char *s, size_t len);
int hexdigit(int c);
-void printnumsep(const char *s);
+size_t printnumsep(const char *s);
int uriencode(const char *s, char *buf, size_t bufsiz);
int utf8pad(char *buf, size_t bufsiz, const char *s, size_t len, int pad);
void xmlencode(const char *s);
(DIR) diff --git a/youtube/cgi.c b/youtube/cgi.c
@@ -155,7 +155,7 @@ render_search(struct search_response *r)
{
struct item *v;
int n;
- size_t i;
+ size_t i, len;
if (pledge("stdio", NULL) == -1) {
OUT("Status: 500 Internal Server Error\r\n\r\n");
@@ -282,7 +282,8 @@ render_search(struct search_response *r)
}
OUT(" <span class=\"stats\">");
if (v->viewcount[0]) {
- printnumsep(v->viewcount);
+ if (!printnumsep(v->viewcount))
+ OUT("0");
OUT(" views");
}
OUT(
@@ -370,7 +371,8 @@ render_video(struct video_response *r)
OUT("<tr><td><b>Views:</b></td><td>");
snprintf(buf, sizeof(buf), "%ld", r->viewcount);
- printnumsep(buf);
+ if (!printnumsep(buf))
+ OUT("0");
OUT("</td></tr>\n");
if (r->publishdate[0]) {
(DIR) diff --git a/youtube/cli.c b/youtube/cli.c
@@ -65,7 +65,7 @@ render_search_tsv(struct search_response *r)
OUT("\t");
OUTESCAPE(v->publishedat);
OUT("\t");
- OUTESCAPE(v->viewcount);
+ OUTESCAPE(v->viewcount); /* from Youtube can be text: "No views" */
OUT("\t");
OUTESCAPE(v->duration);
OUT("\t");
@@ -149,7 +149,8 @@ render_search(struct search_response *r)
}
if (v->viewcount[0]) {
OUT("Views: ");
- printnumsep(v->viewcount);
+ if (!printnumsep(v->viewcount))
+ OUT("0");
OUT("\n");
}
OUT("\n");
@@ -183,7 +184,8 @@ render_video(struct video_response *r)
OUT("Views: ");
snprintf(buf, sizeof(buf), "%ld", r->viewcount);
- printnumsep(buf);
+ if (!printnumsep(buf))
+ OUT("0");
OUT("\n");
if (r->publishdate[0]) {
(DIR) diff --git a/youtube/gopher.c b/youtube/gopher.c
@@ -151,7 +151,8 @@ render_search(struct search_response *r)
}
if (v->viewcount[0]) {
OUT("iViews: ");
- printnumsep(v->viewcount);
+ if (!printnumsep(v->viewcount))
+ OUT("0");
printf("\t%s\t%s\t%s\r\n", "", host, port);
}
}
@@ -216,7 +217,8 @@ render_video(struct video_response *r)
OUT("iViews: ");
snprintf(buf, sizeof(buf), "%ld", r->viewcount);
- printnumsep(buf);
+ if (!printnumsep(buf))
+ OUT("0");
printf("\t%s\t%s\t%s\r\n", "", host, port);
if (r->publishdate[0]) {