Merge branch 'tg/add-videobash'
[quvi.git] / src / quvi / quvi.c
blob3a4fe1ee4373117f489108bdbd532d7dbb92bd22
1 /* quvi
2 * Copyright (C) 2009-2011 Toni Gundogdu <legatvs@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA
20 /* quvi.c - query media tool. */
22 #include "config.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <assert.h>
31 #include <curl/curl.h>
33 #include "platform.h"
35 #include "quvi/quvi.h"
36 #include "quvi/llst.h"
38 #include "cmdline.h"
40 #define _free(p) \
41 do { if (p) { free(p); p=0; } } while (0)
43 /* strepl.c */
44 extern char *strepl(const char *s, const char *what, const char *with);
46 static int verbose_flag = 1;
47 static quvi_t quvi = NULL;
48 static CURL *curl = NULL;
50 typedef struct gengetopt_args_info *opts_t;
51 static opts_t opts = NULL;
53 static quvi_llst_node_t inputs = NULL;
55 /* prints to std(e)rr. */
56 static void spew_e(const char *fmt, ...)
58 va_list ap;
59 va_start(ap, fmt);
60 vfprintf(stderr, fmt, ap);
61 va_end(ap);
64 /* respects (q)uiet, prints to std(e)rr. */
65 static void spew_qe(const char *fmt, ...)
67 va_list ap;
68 if (!verbose_flag)
69 return;
70 va_start(ap, fmt);
71 vfprintf(stderr, fmt, ap);
72 va_end(ap);
75 /* glorified printf. */
76 static void spew(const char *fmt, ...)
78 va_list ap;
79 va_start(ap, fmt);
80 vfprintf(stdout, fmt, ap);
81 va_end(ap);
84 static void dump_error(quvi_t quvi, QUVIcode rc)
86 fprintf(stderr, "error: %s\n", quvi_strerror(quvi, rc));
89 static void handle_resolve_status(quvi_word type)
91 if (type == QUVISTATUSTYPE_DONE)
92 spew_qe("done.\n");
93 else
94 spew_qe(":: Check for URL redirection ...");
97 static void handle_fetch_status(quvi_word type, void *p)
99 switch (type)
101 default:
102 spew_qe(":: Fetch %s ...", (char *)p);
103 break;
104 case QUVISTATUSTYPE_CONFIG:
105 spew_qe(":: Fetch config ...");
106 break;
107 case QUVISTATUSTYPE_PLAYLIST:
108 spew_qe(":: Fetch playlist ...");
109 break;
110 case QUVISTATUSTYPE_DONE:
111 spew_qe("done.\n");
112 break;
116 static void handle_verify_status(quvi_word type)
118 switch (type)
120 default:
121 spew_qe(":: Verify media URL ...");
122 break;
123 case QUVISTATUSTYPE_DONE:
124 spew_qe("done.\n");
125 break;
129 static int status_callback(long param, void *data)
131 quvi_word status, type;
133 status = quvi_loword(param);
134 type = quvi_hiword(param);
136 switch (status)
138 case QUVISTATUS_RESOLVE:
139 handle_resolve_status(type);
140 break;
142 case QUVISTATUS_FETCH:
143 handle_fetch_status(type, data);
144 break;
146 case QUVISTATUS_VERIFY:
147 handle_verify_status(type);
148 break;
151 fflush(stderr);
153 return (0);
156 /* Divided into smaller blocks. Otherwise -pedantic objects. */
158 #define LICENSE_1 \
159 "/* quvi\n" \
160 " * Copyright (C) 2009,2010,2011 Toni Gundogdu <legatvs@gmail.com>\n"
162 #define LICENSE_2 \
163 " * This library is free software; you can redistribute it and/or\n" \
164 " * modify it under the terms of the GNU Lesser General Public\n" \
165 " * License as published by the Free Software Foundation; either\n" \
166 " * version 2.1 of the License, or (at your option) any later version.\n"
168 #define LICENSE_3 \
169 " * This library is distributed in the hope that it will be useful,\n" \
170 " * but WITHOUT ANY WARRANTY; without even the implied warranty of\n" \
171 " * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n" \
172 " * Lesser General Public License for more details.\n"
174 #define LICENSE_4 \
175 " * You should have received a copy of the GNU Lesser General Public\n" \
176 " * License along with this library; if not, write to the Free Software\n" \
177 " * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA\n" \
178 " * 02110-1301 USA\n" " */"
180 static void license()
182 printf("%s *\n%s *\n%s *\n%s\n",
183 LICENSE_1, LICENSE_2, LICENSE_3, LICENSE_4);
184 exit(0);
187 #undef LICENSE_4
188 #undef LICENSE_3
189 #undef LICENSE_2
190 #undef LICENSE_1
192 static void version()
194 printf("quvi version %s\n", quvi_version(QUVI_VERSION_LONG));
195 exit(0);
198 static void dump_host(char *domain, char *formats)
200 printf("%s\t%s\n", domain, formats);
201 quvi_free(domain);
202 quvi_free(formats);
205 /* Wraps quvi_supported_ident. */
206 static void supported(quvi_t quvi)
208 #ifndef _QUVI_SUPPORTED
209 quvi_ident_t ident;
210 char *formats;
211 #endif
212 QUVIcode rc;
213 int i;
215 rc = QUVI_NOSUPPORT;
217 for (i=0; i<opts->inputs_num; ++i)
219 rc = quvi_supported_ident(quvi, (char*)opts->inputs[i], &ident);
220 if (rc == QUVI_OK)
222 quvi_ident_getprop(ident, QUVI_IDENT_PROPERTY_FORMATS, &formats);
223 spew("%10s : %s\n", formats, (char *)opts->inputs[i]);
224 quvi_supported_ident_close(&ident);
226 else
227 dump_error(quvi,rc);
230 exit(rc);
233 /* Query which formats are available for the URL */
234 static void query_formats(quvi_t quvi)
236 QUVIcode rc;
237 int i;
239 if (opts->inputs_num < 1)
241 spew_qe("error: no input URLs\n");
242 exit (QUVI_INVARG);
245 for (i=0; i<opts->inputs_num; ++i)
247 char *formats = NULL;
249 rc = quvi_query_formats(quvi, (char*)opts->inputs[i], &formats);
250 if (rc == QUVI_OK)
252 spew("%10s : %s\n", formats, opts->inputs[i]);
253 quvi_free(formats);
255 else
256 dump_error(quvi,rc);
259 exit(rc);
262 /* dumps all supported hosts to stdout. */
263 static void support(quvi_t quvi)
265 int done = 0;
267 if (opts->inputs_num > 0)
268 supported(quvi);
270 while (!done)
272 char *domain, *formats;
273 QUVIcode rc;
275 rc = quvi_next_supported_website(quvi, &domain, &formats);
277 switch (rc)
279 case QUVI_OK:
280 dump_host(domain, formats);
281 break;
282 case QUVI_LAST:
283 done = 1;
284 break;
285 default:
286 spew_e("%s\n", quvi_strerror(quvi, rc));
287 break;
291 exit(0);
294 static void invoke_exec(quvi_media_t media)
296 char *cmd, *media_url, *q_media_url;
297 char *page_title, *q_page_title, *t;
298 int rc;
300 quvi_getprop(media, QUVIPROP_PAGETITLE, &page_title);
301 t = strdup(page_title);
302 t = strepl(t, "\"", "\\\""); /* Escape existing double quotation marks */
303 asprintf(&q_page_title, "\"%s\"", t); /* Put inside quotation marks */
304 _free(t);
306 quvi_getprop(media, QUVIPROP_MEDIAURL, &media_url);
307 asprintf(&q_media_url, "\"%s\"", media_url);
309 cmd = strdup(opts->exec_arg);
310 cmd = strepl(cmd, "%t", q_page_title);
311 cmd = strepl(cmd, "%u", q_media_url);
313 _free(q_page_title);
314 _free(q_media_url);
316 rc = system(cmd);
318 switch (rc)
320 case 0:
321 break;
322 case -1:
323 spew_e("error: failed to execute `%s'\n", cmd);
324 break;
325 default:
326 spew_e("error: child exited with: %d\n", rc >> 8);
327 break;
330 _free(cmd);
333 struct parsed_url_s
335 char *media_url;
336 char *content_type;
337 double content_length;
338 char *file_suffix;
341 typedef struct parsed_url_s *parsed_url_t;
343 static void dump_media_url_xml(parsed_url_t p, int i)
345 char *media_url = curl_easy_escape(curl, p->media_url, 0);
347 spew(" <link id=\"%d\">\n", i);
349 if (p->content_length)
350 spew(" <length_bytes>%.0f</length_bytes>\n", p->content_length);
352 if (strlen(p->content_type))
353 spew(" <content_type>%s</content_type>\n", p->content_type);
355 if (strlen(p->file_suffix))
356 spew(" <file_suffix>%s</file_suffix>\n", p->file_suffix);
358 spew(" <url>%s</url>\n"
359 " </link>\n",
360 media_url ? media_url : p->media_url);
362 _free(media_url);
365 static void dump_media_url_json(parsed_url_t p, int i, int prepend_newln)
367 if (prepend_newln)
368 spew(",\n");
370 spew(" {\n"
371 " \"id\": \"%d\",\n", i);
373 if (p->content_length)
374 spew(" \"length_bytes\": \"%.0f\",\n", p->content_length);
376 if (strlen(p->content_type))
377 spew(" \"content_type\": \"%s\",\n", p->content_type);
379 if (strlen(p->file_suffix))
380 spew(" \"file_suffix\": \"%s\",\n", p->file_suffix);
382 spew(" \"url\": \"%s\"\n"
383 " }",
384 p->media_url);
387 static void dump_media_urls(quvi_media_t media)
389 int json_flag=0, i=1;
392 struct parsed_url_s p;
394 memset(&p, 0, sizeof(&p));
396 quvi_getprop(media, QUVIPROP_MEDIAURL, &p.media_url);
397 quvi_getprop(media, QUVIPROP_MEDIACONTENTTYPE, &p.content_type);
398 quvi_getprop(media, QUVIPROP_MEDIACONTENTLENGTH, &p.content_length);
399 quvi_getprop(media, QUVIPROP_FILESUFFIX, &p.file_suffix);
401 if (opts->xml_given)
402 dump_media_url_xml(&p,i);
403 else
405 dump_media_url_json(&p, i, i>1);
406 json_flag = 1;
408 ++i;
410 while (quvi_next_media_url(media) == QUVI_OK);
412 if (json_flag)
413 spew("\n");
416 struct parsed_s
418 char *page_url;
419 char *page_title;
420 char *media_id;
421 char *format;
422 char *host;
423 char *start_time;
424 char *thumb_url;
425 double duration;
428 typedef struct parsed_s *parsed_t;
430 static void dump_media_xml(parsed_t p)
432 char *e_page_url, *e_thumb_url;
434 e_page_url = curl_easy_escape(curl, p->page_url, 0);
435 e_thumb_url = curl_easy_escape(curl, p->thumb_url, 0);
437 spew("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
438 "<media id=\"%s\" host=\"%s\">\n"
439 " <format_requested>%s</format_requested>\n"
440 " <page_title>%s</page_title>\n"
441 " <page_url>%s</page_url>\n",
442 p->media_id,
443 p->host,
444 p->format,
445 p->page_title,
446 e_page_url ? e_page_url : "");
448 if (strlen(p->start_time))
449 spew(" <start_time>%s</start_time>\n", p->start_time);
451 if (e_thumb_url && strlen(e_thumb_url))
452 spew(" <thumbnail_url>%s</thumbnail_url>\n", e_thumb_url);
454 if (p->duration)
455 spew(" <duration>%.0f</duration>\n", p->duration);
457 if (e_page_url)
459 curl_free(e_page_url);
460 e_page_url = NULL;
463 if (e_thumb_url)
465 curl_free(e_thumb_url);
466 e_thumb_url = NULL;
470 static void dump_media_json(parsed_t p)
472 char *t;
474 t = strdup(p->page_title);
475 t = strepl(t, "\"", "\\\"");
477 spew("{\n"
478 " \"host\": \"%s\",\n"
479 " \"page_title\": \"%s\",\n"
480 " \"page_url\": \"%s\",\n"
481 " \"id\": \"%s\",\n"
482 " \"format_requested\": \"%s\",\n",
483 p->host,
485 p->page_url,
486 p->media_id,
487 p->format);
489 if (strlen(p->start_time))
490 spew(" \"start_time\": \"%s\",\n", p->start_time);
492 if (strlen(p->thumb_url))
493 spew(" \"thumbnail_url\": \"%s\",\n", p->thumb_url);
495 if (p->duration)
496 spew(" \"duration\": \"%.0f\",\n", p->duration);
498 spew(" \"link\": [\n");
500 _free(t);
503 static void dump_media(quvi_media_t media)
505 struct parsed_s p;
507 memset(&p, 0, sizeof(p));
509 quvi_getprop(media, QUVIPROP_HOSTID, &p.host);
510 quvi_getprop(media, QUVIPROP_PAGEURL, &p.page_url);
511 quvi_getprop(media, QUVIPROP_PAGETITLE, &p.page_title);
512 quvi_getprop(media, QUVIPROP_MEDIAID, &p.media_id);
513 quvi_getprop(media, QUVIPROP_FORMAT, &p.format);
514 quvi_getprop(media, QUVIPROP_STARTTIME, &p.start_time);
515 quvi_getprop(media, QUVIPROP_MEDIATHUMBNAILURL, &p.thumb_url);
516 quvi_getprop(media, QUVIPROP_MEDIADURATION, &p.duration);
518 if (opts->xml_given)
519 dump_media_xml(&p);
520 else
521 dump_media_json(&p);
523 dump_media_urls(media);
525 if (opts->xml_given)
526 spew("</media>\n");
527 else
528 spew(" ]\n}\n");
531 static quvi_t init_quvi()
533 QUVIcode rc;
534 quvi_t quvi;
536 if ((rc = quvi_init(&quvi)) != QUVI_OK)
538 dump_error(quvi, rc);
539 exit(rc);
541 assert(quvi != 0);
543 /* Set quvi options. */
545 if (opts->format_given)
546 quvi_setopt(quvi, QUVIOPT_FORMAT, opts->format_arg);
548 quvi_setopt(quvi, QUVIOPT_NORESOLVE, opts->no_resolve_given);
549 quvi_setopt(quvi, QUVIOPT_NOVERIFY, opts->no_verify_given);
551 if (opts->category_all_given)
552 quvi_setopt(quvi, QUVIOPT_CATEGORY, QUVIPROTO_ALL);
553 else
555 long n = 0;
556 if (opts->category_http_given)
557 n |= QUVIPROTO_HTTP;
558 if (opts->category_mms_given)
559 n |= QUVIPROTO_MMS;
560 if (opts->category_rtsp_given)
561 n |= QUVIPROTO_RTSP;
562 if (opts->category_rtmp_given)
563 n |= QUVIPROTO_RTMP;
564 if (n > 0)
565 quvi_setopt(quvi, QUVIOPT_CATEGORY, n);
568 quvi_setopt(quvi, QUVIOPT_STATUSFUNCTION, status_callback);
570 /* Use the quvi created cURL handle. */
572 quvi_getinfo(quvi, QUVIINFO_CURL, &curl);
573 assert(curl != 0);
575 if (opts->agent_given)
576 curl_easy_setopt(curl, CURLOPT_USERAGENT, opts->agent_arg);
578 if (opts->proxy_given)
579 curl_easy_setopt(curl, CURLOPT_PROXY, opts->proxy_arg);
581 if (opts->no_proxy_given)
582 curl_easy_setopt(curl, CURLOPT_PROXY, "");
584 curl_easy_setopt(curl, CURLOPT_VERBOSE, opts->verbose_libcurl_given);
586 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT,
587 opts->connect_timeout_arg);
589 return (quvi);
592 static void cleanup()
594 quvi_llst_free(&inputs);
595 assert(inputs == NULL);
597 if (quvi)
598 quvi_close(&quvi);
599 assert(quvi == NULL);
601 if (opts)
603 cmdline_parser_free(opts);
604 _free(opts);
606 assert(opts == NULL);
609 static void read_from(FILE *f, int close)
611 char b[256];
613 if (!f)
614 return;
616 while (fgets(b, sizeof(b), f))
618 if (strlen(b) > 16)
620 const size_t n = strlen(b)-1;
622 if (b[n] == '\n')
623 b[n] = '\0';
625 quvi_llst_append(&inputs, strdup(b));
629 if (close)
631 fclose(f);
632 f = NULL;
636 static char *parse_url_scheme(const char *url)
638 char *p, *r;
640 p = strstr(url, ":/");
641 if (!p)
642 return (NULL);
644 asprintf(&r, "%.*s", (int)(p - url), url);
646 return (r);
649 static int is_url(const char *s)
651 char *p = parse_url_scheme(s);
652 if (p)
654 _free(p);
655 return (1);
657 return (0);
660 static FILE* open_file(const char *path)
662 FILE *f = fopen(path, "rt");
663 if (!f)
664 #ifdef HAVE_STRERROR
665 spew_e("error: %s: %s\n", path, strerror(errno));
666 #else
667 perror("fopen");
668 #endif
669 return (f);
672 static int read_input()
674 if (opts->inputs_num == 0)
675 read_from(stdin, 0);
676 else
678 int i;
679 for (i=0; i<opts->inputs_num; ++i)
681 if (!is_url(opts->inputs[i]))
682 read_from(open_file(opts->inputs[i]), 1);
683 else /* Must be an URL. */
684 quvi_llst_append(&inputs, strdup(opts->inputs[i]));
687 return (quvi_llst_size(inputs));
690 int main(int argc, char *argv[])
692 const char *home, *no_config, *fname;
693 QUVIcode rc, last_failure;
694 quvi_llst_node_t curr;
695 quvi_media_t media;
696 int no_config_flag;
697 int i, inputs_num;
698 int errors;
700 assert(quvi == NULL);
701 assert(curl == NULL);
702 assert(opts == NULL);
703 assert(inputs == NULL);
705 no_config = getenv("QUVI_NO_CONFIG");
706 no_config_flag = 1;
708 home = getenv("QUVI_HOME");
709 if (!home)
710 home = getenv("HOME");
712 #ifndef HOST_W32
713 fname = "/.quvirc";
714 #else
715 fname = "\\quvirc";
716 #endif
718 atexit(cleanup);
720 opts = calloc(1, sizeof(struct gengetopt_args_info));
721 if (!opts)
722 return(QUVI_MEM);
724 /* Init cmdline parser. */
726 if (home && !no_config)
728 char *path;
729 FILE *f;
731 asprintf(&path, "%s%s", home, fname);
732 f = fopen(path, "r");
734 if (f != NULL)
736 struct cmdline_parser_params *pp;
738 fclose(f);
739 f = NULL;
741 pp = cmdline_parser_params_create();
742 pp->check_required = 0;
744 if (cmdline_parser_config_file(path, opts, pp) == 0)
746 pp->initialize = 0;
747 pp->override = 1;
748 pp->check_required = 1;
750 if (cmdline_parser_ext(argc, argv, opts, pp) == 0)
751 no_config_flag = 0;
753 _free(pp);
756 _free(path);
759 if (no_config_flag)
761 if (cmdline_parser(argc, argv, opts) != 0)
762 return (QUVI_INVARG);
765 if (opts->version_given)
766 version(opts);
768 if (opts->license_given)
769 license(opts);
771 verbose_flag = !opts->quiet_given;
773 quvi = init_quvi();
775 if (opts->query_formats_given)
776 query_formats(quvi);
778 if (opts->support_given)
779 support(quvi);
781 /* User input */
783 inputs_num = read_input();
785 if (inputs_num == 0)
787 spew_qe("error: no input URLs\n");
788 return (QUVI_INVARG);
791 last_failure = QUVI_OK;
792 errors = 0;
794 for (i=0, curr=inputs; curr; ++i)
796 char *url = quvi_llst_data(curr);
797 rc = quvi_parse(quvi, url, &media);
798 if (rc == QUVI_OK)
800 assert(media != 0);
801 dump_media(media);
803 if (opts->exec_given)
807 invoke_exec(media);
809 while (quvi_next_media_url(media) == QUVI_OK);
812 else
814 dump_error(quvi,rc);
815 last_failure = rc;
816 ++errors;
818 quvi_parse_close(&media);
819 assert(media == 0);
820 curr = quvi_llst_next(curr);
823 if (inputs_num > 1)
825 spew_qe("Results: %d OK, %d failed (last 0x%02x), exit with 0x%02x\n",
826 inputs_num - errors, errors, last_failure, rc);
829 return (rc);
832 /* vim: set ts=2 sw=2 tw=72 expandtab: */