ref: 34e15a04000f6046a4e6a3828aea93f9c348f818
dir: /peertube.c/
#include <u.h> #include <libc.h> #include <json.h> #include "nvi.h" static int addfmt(Info *i, JSON *f) { JSON *x, *z, *j; Format *fmt; char *s, *t; JSONEl *e; int fd; if((x = jsonbyname(f, "metadataUrl")) == nil){ werrstr("no url"); return -1; } j = nil; if((fd = hget(jsonstr(x), -1)) >= 0){ if((s = readall(fd)) != nil){ j = jsonparse(s); free(s); } close(fd); } procwait(); if(j == nil){ werrstr("peertube: %r"); return -1; } if((x = jsonbyname(f, "fileDownloadUrl")) == nil){ werrstr("no url"); jsonfree(j); return -1; } i->nfmt++; if((i->fmt = realloc(i->fmt, i->nfmt * sizeof(*fmt))) == nil) sysfatal("memory"); fmt = &i->fmt[i->nfmt - 1]; memset(fmt, 0, sizeof(*fmt)); fmt->url = estrdup(jsonstr(x)); if((x = jsonbyname(f, "size")) != nil) fmt->sz = x->n; s = strdup(""); if((x = jsonbyname(j, "streams")) != nil && x->t == JSONArray){ for(e = x->first; e != nil; e = e->next){ if((x = jsonbyname(e->val, "codec_name")) != nil){ t = smprint("%s%s%s", s, *s ? "," : "", jsonstr(x)); free(s); s = t; } if((x = jsonbyname(e->val, "codec_type")) != nil){ t = jsonstr(x); if(strcmp(t, "video") == 0) fmt->included |= Ivideo; else if(strcmp(t, "audio") == 0) fmt->included |= Iaudio; } } } fmt->type = s; jsonfree(j); if((x = jsonbyname(f, "resolution")) != nil){ if((z = jsonbyname(x, "id")) != nil) fmt->id = z->n; if((fmt->included & Ivideo) && (z = jsonbyname(x, "label")) != nil) fmt->quality = estrdup(jsonstr(z)); if((z = jsonbyname(f, "fps")) != nil) fmt->fps = z->n; } return 0; } Info * peertube(char *url) { int fd, peertube; char *s, *o, *id; JSON *j, *z; JSONEl *e, *f; Info *i; peertube = 0; if((fd = hget(url, -1)) >= 0){ if((s = readall(fd)) != nil){ if((o = strstr(s, "property=\"og:platform\"")) != nil && strstr(o+23, "content=\"PeerTube\"") != nil) peertube = 1; free(s); } close(fd); } procwait(); if(!peertube){ if(fd >= 0) werrstr("not peertube"); return nil; } if((id = strrchr(url, '/')) == nil || (s = strstr(url, "://")) == nil || (s = strchr(s+3, '/')) == nil){ werrstr("bad url"); return nil; } url = smprint("%.*s/api/v1/videos%s", (int)(s-url), url, id); fd = hget(url, -1); free(url); j = nil; if(fd >= 0){ if((s = readall(fd)) != nil){ j = jsonparse(s); free(s); } close(fd); } procwait(); if(j == nil){ werrstr("peertube: %r"); return nil; } if((i = calloc(1, sizeof(*i))) == nil) sysfatal("memory"); if((z = jsonbyname(j, "account")) != nil) i->author = estrdup(jsonstr(jsonbyname(z, "displayName"))); i->title = estrdup(jsonstr(jsonbyname(j, "name"))); i->description = estrdup(jsonstr(jsonbyname(j, "description"))); i->duration = jsonbyname(j, "duration")->n; // FIXME i->published = jsonstr(jsonbyname(z, "published")); if((z = jsonbyname(j, "streamingPlaylists")) != nil && z->t == JSONArray){ for(e = z->first; e != nil; e = e->next){ if((z = jsonbyname(e->val, "files")) != nil && z->t == JSONArray){ for(f = z->first; f != nil; f = f->next) addfmt(i, f->val); } } } jsonfree(j); return i; }