quvi: Add depr. message to -f list
[quvi.git] / src / quvi / quvi.c
blob1006f46ab9a1edfa1b9747c1b9c529809275dc20
1 /* quvi
2 * Copyright (C) 2009,2010,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 handle_resolve_status(quvi_word type)
86 if (type == QUVISTATUSTYPE_DONE)
87 spew_qe("done.\n");
88 else
89 spew_qe(":: Check for URL redirection ...");
92 static void handle_fetch_status(quvi_word type, void *p)
94 switch (type)
96 default:
97 spew_qe(":: Fetch %s ...", (char *)p);
98 break;
99 case QUVISTATUSTYPE_CONFIG:
100 spew_qe(":: Fetch config ...");
101 break;
102 case QUVISTATUSTYPE_PLAYLIST:
103 spew_qe(":: Fetch playlist ...");
104 break;
105 case QUVISTATUSTYPE_DONE:
106 spew_qe("done.\n");
107 break;
111 static void handle_verify_status(quvi_word type)
113 switch (type)
115 default:
116 spew_qe(":: Verify media URL ...");
117 break;
118 case QUVISTATUSTYPE_DONE:
119 spew_qe("done.\n");
120 break;
124 static int status_callback(long param, void *data)
126 quvi_word status, type;
128 status = quvi_loword(param);
129 type = quvi_hiword(param);
131 switch (status)
133 case QUVISTATUS_RESOLVE:
134 handle_resolve_status(type);
135 break;
137 case QUVISTATUS_FETCH:
138 handle_fetch_status(type, data);
139 break;
141 case QUVISTATUS_VERIFY:
142 handle_verify_status(type);
143 break;
146 fflush(stderr);
148 return (0);
151 static size_t write_callback(void *p, size_t size, size_t nmemb,
152 void *data)
154 size_t r = quvi_write_callback_default(p, size, nmemb, data);
155 /* Could do something useful here. */
156 #ifdef _0
157 puts(__func__);
158 #endif
159 return r;
162 /* Divided into smaller blocks. Otherwise -pedantic objects. */
164 #define LICENSE_1 \
165 "/* quvi\n" \
166 " * Copyright (C) 2009,2010,2011 Toni Gundogdu <legatvs@gmail.com>\n"
168 #define LICENSE_2 \
169 " * This library is free software; you can redistribute it and/or\n" \
170 " * modify it under the terms of the GNU Lesser General Public\n" \
171 " * License as published by the Free Software Foundation; either\n" \
172 " * version 2.1 of the License, or (at your option) any later version.\n"
174 #define LICENSE_3 \
175 " * This library is distributed in the hope that it will be useful,\n" \
176 " * but WITHOUT ANY WARRANTY; without even the implied warranty of\n" \
177 " * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n" \
178 " * Lesser General Public License for more details.\n"
180 #define LICENSE_4 \
181 " * You should have received a copy of the GNU Lesser General Public\n" \
182 " * License along with this library; if not, write to the Free Software\n" \
183 " * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA\n" \
184 " * 02110-1301 USA\n" " */"
186 static void license()
188 printf("%s *\n%s *\n%s *\n%s\n",
189 LICENSE_1, LICENSE_2, LICENSE_3, LICENSE_4);
190 exit(0);
193 #undef LICENSE_4
194 #undef LICENSE_3
195 #undef LICENSE_2
196 #undef LICENSE_1
198 static void version()
200 printf("quvi version %s\n", quvi_version(QUVI_VERSION_LONG));
201 exit(0);
204 static void dump_host(char *domain, char *formats)
206 printf("%s\t%s\n", domain, formats);
207 quvi_free(domain);
208 quvi_free(formats);
211 /* Wraps quvi_supported_ident. */
212 static void supported(quvi_t quvi)
214 #ifndef _QUVI_SUPPORTED
215 quvi_ident_t ident;
216 char *formats;
217 #endif
218 QUVIcode rc;
219 int i;
221 rc = QUVI_NOSUPPORT;
223 for (i=0; i<opts->inputs_num; ++i)
225 rc = quvi_supported_ident(quvi, (char*)opts->inputs[i], &ident);
226 if (rc == QUVI_OK)
228 quvi_ident_getprop(ident, QUVI_IDENT_PROPERTY_FORMATS, &formats);
229 spew("%10s : %s\n", formats, (char *)opts->inputs[i]);
230 quvi_supported_ident_close(&ident);
232 else
233 spew_qe("error: %s\n", quvi_strerror(quvi, rc));
236 exit(rc);
239 static const char depr_msg[] =
240 "Warning:\n"
241 " '--format list' is deprecated and will be removed in\n"
242 " quvi 0.2.20. Use --query-formats instead.";
244 static void format_help(quvi_t quvi)
246 int quit = 0;
248 if (strcmp(opts->format_arg, "help") == 0)
250 printf(
251 "Usage:\n"
252 " --format arg get format arg of media\n"
253 " --format list print domains with formats\n"
254 " --format list arg match arg to supported domain names\n"
255 "Examples:\n"
256 " --format list youtube print youtube formats\n"
257 " --format fmt34_360p get format fmt34_360p of media\n"
258 "%s\n",
259 depr_msg);
260 quit = 1;
263 else if (strcmp(opts->format_arg, "list") == 0)
265 int done = 0;
266 char *d, *f;
268 while(!done)
270 const int rc =
271 quvi_next_supported_website(quvi, &d, &f);
273 switch (rc)
276 case QUVI_OK:
278 int print = 1;
280 /* -f list <pattern> */
281 if (opts->inputs_num > 0)
282 print = strstr(d, (char *)opts->inputs[0]) != 0;
284 /* -f list */
285 if (print)
286 printf("%s:\n %s\n\n", d, f);
288 quvi_free(d);
289 quvi_free(f);
291 break;
293 case QUVI_LAST:
294 done = 1;
295 break;
297 default:
298 spew_e("%s\n", quvi_strerror(quvi, rc));
299 break;
302 spew_qe("%s\n", depr_msg);
303 quit = 1;
306 if (quit)
307 exit(0);
310 /* Query which formats are available for the URL */
311 static void query_formats(quvi_t quvi)
313 QUVIcode rc;
314 int i;
316 if (opts->inputs_num < 1)
318 spew_qe("error: no input URLs\n");
319 exit (QUVI_INVARG);
322 for (i=0; i<opts->inputs_num; ++i)
324 char *formats = NULL;
326 rc = quvi_query_formats(quvi, (char*)opts->inputs[i], &formats);
327 if (rc == QUVI_OK)
329 spew("%10s : %s\n", formats, opts->inputs[i]);
330 quvi_free(formats);
332 else
333 spew_qe("%s\n", quvi_strerror(quvi, rc));
336 exit(rc);
339 /* dumps all supported hosts to stdout. */
340 static void support(quvi_t quvi)
342 int done = 0;
344 if (opts->inputs_num > 0)
345 supported(quvi);
347 while (!done)
349 char *domain, *formats;
350 QUVIcode rc;
352 rc = quvi_next_supported_website(quvi, &domain, &formats);
354 switch (rc)
356 case QUVI_OK:
357 dump_host(domain, formats);
358 break;
359 case QUVI_LAST:
360 done = 1;
361 break;
362 default:
363 spew_e("%s\n", quvi_strerror(quvi, rc));
364 break;
368 exit(0);
371 static void invoke_exec(quvi_media_t media)
373 char *cmd, *media_url, *q_media_url;
374 int rc;
376 quvi_getprop(media, QUVIPROP_MEDIAURL, &media_url);
378 asprintf(&q_media_url, "\"%s\"", media_url);
380 cmd = strdup(opts->exec_arg);
381 cmd = strepl(cmd, "%u", q_media_url);
383 _free(q_media_url);
385 rc = system(cmd);
387 switch (rc)
389 case 0:
390 break;
391 case -1:
392 spew_e("error: failed to execute `%s'\n", cmd);
393 break;
394 default:
395 spew_e("error: child exited with: %d\n", rc >> 8);
396 break;
399 _free(cmd);
402 struct parsed_url_s
404 char *media_url;
405 char *content_type;
406 double content_length;
407 char *file_suffix;
410 typedef struct parsed_url_s *parsed_url_t;
412 static void dump_media_url_xml(parsed_url_t p, int i)
414 char *media_url = curl_easy_escape(curl, p->media_url, 0);
416 spew(" <link id=\"%d\">\n", i);
418 if (p->content_length)
419 spew(" <length_bytes>%.0f</length_bytes>\n", p->content_length);
421 if (strlen(p->content_type))
422 spew(" <content_type>%s</content_type>\n", p->content_type);
424 if (strlen(p->file_suffix))
425 spew(" <file_suffix>%s</file_suffix>\n", p->file_suffix);
427 spew(" <url>%s</url>\n"
428 " </link>\n",
429 media_url ? media_url : p->media_url);
431 _free(media_url);
434 static void dump_media_url_old(parsed_url_t p, int i)
436 spew("link %02d : %s\n", i, p->media_url);
438 if (p->content_length)
439 spew(":: length: %.0f\n", p->content_length);
441 if (strlen(p->file_suffix))
442 spew(":: suffix: %s\n", p->file_suffix);
444 if (strlen(p->content_type))
445 spew(":: content-type: %s\n", p->content_type);
448 static void dump_media_url_json(parsed_url_t p, int i, int prepend_newln)
450 if (prepend_newln)
451 spew(",\n");
453 spew(" {\n"
454 " \"id\": \"%d\",\n", i);
456 if (p->content_length)
457 spew(" \"length_bytes\": \"%.0f\",\n", p->content_length);
459 if (strlen(p->content_type))
460 spew(" \"content_type\": \"%s\",\n", p->content_type);
462 if (strlen(p->file_suffix))
463 spew(" \"file_suffix\": \"%s\",\n", p->file_suffix);
465 spew(" \"url\": \"%s\"\n"
466 " }",
467 p->media_url);
470 static void dump_media_urls(quvi_media_t media)
472 int json_flag=0, i=1;
475 struct parsed_url_s p;
477 memset(&p, 0, sizeof(&p));
479 quvi_getprop(media, QUVIPROP_MEDIAURL, &p.media_url);
480 quvi_getprop(media, QUVIPROP_MEDIACONTENTTYPE, &p.content_type);
481 quvi_getprop(media, QUVIPROP_MEDIACONTENTLENGTH, &p.content_length);
482 quvi_getprop(media, QUVIPROP_FILESUFFIX, &p.file_suffix);
484 if (opts->xml_given)
485 dump_media_url_xml(&p,i);
486 else if (opts->old_given)
487 dump_media_url_old(&p,i);
488 else
490 dump_media_url_json(&p, i, i>1);
491 json_flag = 1;
493 ++i;
495 while (quvi_next_media_url(media) == QUVI_OK);
497 if (json_flag)
498 spew("\n");
501 struct parsed_s
503 char *page_url;
504 char *page_title;
505 char *media_id;
506 char *format;
507 char *host;
508 char *start_time;
509 char *thumb_url;
510 double duration;
513 typedef struct parsed_s *parsed_t;
515 static void dump_media_xml(parsed_t p)
517 char *e_page_url, *e_thumb_url;
519 e_page_url = curl_easy_escape(curl, p->page_url, 0);
520 e_thumb_url = curl_easy_escape(curl, p->thumb_url, 0);
522 spew("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
523 "<media id=\"%s\" host=\"%s\">\n"
524 " <format_requested>%s</format_requested>\n"
525 " <page_title>%s</page_title>\n"
526 " <page_url>%s</page_url>\n",
527 p->media_id,
528 p->host,
529 p->format,
530 p->page_title,
531 e_page_url ? e_page_url : "");
533 if (strlen(p->start_time))
534 spew(" <start_time>%s</start_time>\n", p->start_time);
536 if (e_thumb_url && strlen(e_thumb_url))
537 spew(" <thumbnail_url>%s</thumbnail_url>\n", e_thumb_url);
539 if (p->duration)
540 spew(" <duration>%.0f</duration>\n", p->duration);
542 if (e_page_url)
544 curl_free(e_page_url);
545 e_page_url = NULL;
548 if (e_thumb_url)
550 curl_free(e_thumb_url);
551 e_thumb_url = NULL;
555 static void dump_media_old(parsed_t p)
557 spew(" > Dump media:\n"
558 "host : %s\n"
559 "url : %s\n"
560 "title : %s\n"
561 "id : %s\n"
562 "format : %s (requested)\n",
563 p->host,
564 p->page_url,
565 p->page_title,
566 p->media_id,
567 p->format);
569 if (strlen(p->start_time))
570 spew("start time: %s\n", p->start_time);
572 if (strlen(p->thumb_url))
573 spew("thumbnail url: %s\n", p->thumb_url);
575 if (p->duration)
576 spew("duration: %.0f\n", p->duration);
579 static void dump_media_json(parsed_t p)
581 char *t;
583 t = strdup(p->page_title);
584 t = strepl(t, "\"", "\\\"");
586 spew("{\n"
587 " \"host\": \"%s\",\n"
588 " \"page_title\": \"%s\",\n"
589 " \"page_url\": \"%s\",\n"
590 " \"id\": \"%s\",\n"
591 " \"format_requested\": \"%s\",\n",
592 p->host,
594 p->page_url,
595 p->media_id,
596 p->format);
598 if (strlen(p->start_time))
599 spew(" \"start_time\": \"%s\",\n", p->start_time);
601 if (strlen(p->thumb_url))
602 spew(" \"thumbnail_url\": \"%s\",\n", p->thumb_url);
604 if (p->duration)
605 spew(" \"duration\": \"%.0f\",\n", p->duration);
607 spew(" \"link\": [\n");
609 _free(t);
612 static void dump_media(quvi_media_t media)
614 struct parsed_s p;
616 memset(&p, 0, sizeof(p));
618 quvi_getprop(media, QUVIPROP_HOSTID, &p.host);
619 quvi_getprop(media, QUVIPROP_PAGEURL, &p.page_url);
620 quvi_getprop(media, QUVIPROP_PAGETITLE, &p.page_title);
621 quvi_getprop(media, QUVIPROP_MEDIAID, &p.media_id);
622 quvi_getprop(media, QUVIPROP_FORMAT, &p.format);
623 quvi_getprop(media, QUVIPROP_STARTTIME, &p.start_time);
624 quvi_getprop(media, QUVIPROP_MEDIATHUMBNAILURL, &p.thumb_url);
625 quvi_getprop(media, QUVIPROP_MEDIADURATION, &p.duration);
627 if (opts->xml_given)
628 dump_media_xml(&p);
629 else if (opts->old_given)
630 dump_media_old(&p);
631 else
632 dump_media_json(&p);
634 dump_media_urls(media);
636 if (opts->xml_given)
637 spew("</media>\n");
638 else if (opts->old_given) ;
639 else
640 spew(" ]\n}\n");
643 static void dump_error(quvi_t quvi, QUVIcode rc)
645 fprintf(stderr, "error: %s\n", quvi_strerror(quvi, rc));
648 static quvi_t init_quvi()
650 QUVIcode rc;
651 quvi_t quvi;
653 if ((rc = quvi_init(&quvi)) != QUVI_OK)
655 dump_error(quvi, rc);
656 exit(rc);
658 assert(quvi != 0);
660 /* Set quvi options. */
662 if (opts->format_given)
663 quvi_setopt(quvi, QUVIOPT_FORMAT, opts->format_arg);
665 if (opts->no_shortened_given)
667 spew_e("warning: --no-shortened is deprecated, "
668 "use --no-resolve instead\n");
670 quvi_setopt(quvi, QUVIOPT_NORESOLVE, opts->no_resolve_given);
671 quvi_setopt(quvi, QUVIOPT_NOVERIFY, opts->no_verify_given);
673 if (opts->category_all_given)
674 quvi_setopt(quvi, QUVIOPT_CATEGORY, QUVIPROTO_ALL);
675 else
677 long n = 0;
678 if (opts->category_http_given)
679 n |= QUVIPROTO_HTTP;
680 if (opts->category_mms_given)
681 n |= QUVIPROTO_MMS;
682 if (opts->category_rtsp_given)
683 n |= QUVIPROTO_RTSP;
684 if (opts->category_rtmp_given)
685 n |= QUVIPROTO_RTMP;
686 if (n > 0)
687 quvi_setopt(quvi, QUVIOPT_CATEGORY, n);
690 quvi_setopt(quvi, QUVIOPT_STATUSFUNCTION, status_callback);
691 quvi_setopt(quvi, QUVIOPT_WRITEFUNCTION, write_callback);
693 /* Use the quvi created cURL handle. */
695 quvi_getinfo(quvi, QUVIINFO_CURL, &curl);
696 assert(curl != 0);
698 if (opts->agent_given)
699 curl_easy_setopt(curl, CURLOPT_USERAGENT, opts->agent_arg);
701 if (opts->proxy_given)
702 curl_easy_setopt(curl, CURLOPT_PROXY, opts->proxy_arg);
704 if (opts->no_proxy_given)
705 curl_easy_setopt(curl, CURLOPT_PROXY, "");
707 curl_easy_setopt(curl, CURLOPT_VERBOSE, opts->verbose_libcurl_given);
709 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT,
710 opts->connect_timeout_arg);
712 return (quvi);
715 static void cleanup()
717 quvi_llst_free(&inputs);
718 assert(inputs == NULL);
720 if (quvi)
721 quvi_close(&quvi);
722 assert(quvi == NULL);
724 if (opts)
726 cmdline_parser_free(opts);
727 _free(opts);
729 assert(opts == NULL);
732 static void read_from(FILE *f, int close)
734 char b[256];
736 if (!f)
737 return;
739 while (fgets(b, sizeof(b), f))
741 if (strlen(b) > 16)
743 const size_t n = strlen(b)-1;
745 if (b[n] == '\n')
746 b[n] = '\0';
748 quvi_llst_append(&inputs, strdup(b));
752 if (close)
754 fclose(f);
755 f = NULL;
759 static char *parse_url_scheme(const char *url)
761 char *p, *r;
763 p = strstr(url, ":/");
764 if (!p)
765 return (NULL);
767 asprintf(&r, "%.*s", (int)(p - url), url);
769 return (r);
772 static int is_url(const char *s)
774 char *p = parse_url_scheme(s);
775 if (p)
777 _free(p);
778 return (1);
780 return (0);
783 static FILE* open_file(const char *path)
785 FILE *f = fopen(path, "rt");
786 if (!f)
787 #ifdef HAVE_STRERROR
788 spew_e("error: %s: %s\n", path, strerror(errno));
789 #else
790 perror("fopen");
791 #endif
792 return (f);
795 static int read_input()
797 if (opts->inputs_num == 0)
798 read_from(stdin, 0);
799 else
801 int i;
802 for (i=0; i<opts->inputs_num; ++i)
804 if (!is_url(opts->inputs[i]))
805 read_from(open_file(opts->inputs[i]), 1);
806 else /* Must be an URL. */
807 quvi_llst_append(&inputs, strdup(opts->inputs[i]));
810 return (quvi_llst_size(inputs));
813 int main(int argc, char *argv[])
815 const char *home, *no_config, *fname;
816 QUVIcode rc, last_failure;
817 quvi_llst_node_t curr;
818 quvi_media_t media;
819 int no_config_flag;
820 int i, inputs_num;
821 int errors;
823 assert(quvi == NULL);
824 assert(curl == NULL);
825 assert(opts == NULL);
826 assert(inputs == NULL);
828 no_config = getenv("QUVI_NO_CONFIG");
829 no_config_flag = 1;
831 home = getenv("QUVI_HOME");
832 if (!home)
833 home = getenv("HOME");
835 #ifndef HOST_W32
836 fname = "/.quvirc";
837 #else
838 fname = "\\quvirc";
839 #endif
841 atexit(cleanup);
843 opts = calloc(1, sizeof(struct gengetopt_args_info));
844 if (!opts)
845 return(QUVI_MEM);
847 /* Init cmdline parser. */
849 if (home && !no_config)
851 char *path;
852 FILE *f;
854 asprintf(&path, "%s%s", home, fname);
855 f = fopen(path, "r");
857 if (f != NULL)
859 struct cmdline_parser_params *pp;
861 fclose(f);
862 f = NULL;
864 pp = cmdline_parser_params_create();
865 pp->check_required = 0;
867 if (cmdline_parser_config_file(path, opts, pp) == 0)
869 pp->initialize = 0;
870 pp->override = 1;
871 pp->check_required = 1;
873 if (cmdline_parser_ext(argc, argv, opts, pp) == 0)
874 no_config_flag = 0;
876 _free(pp);
879 _free(path);
882 if (no_config_flag)
884 if (cmdline_parser(argc, argv, opts) != 0)
885 return (QUVI_INVARG);
888 if (opts->version_given)
889 version(opts);
891 if (opts->license_given)
892 license(opts);
894 verbose_flag = !opts->quiet_given;
896 quvi = init_quvi();
898 if (opts->query_formats_given)
899 query_formats(quvi);
901 if (opts->support_given)
902 support(quvi);
904 if (opts->format_given)
905 format_help(quvi);
907 /* User input */
909 inputs_num = read_input();
911 if (inputs_num == 0)
913 spew_qe("error: no input URLs\n");
914 return (QUVI_INVARG);
917 last_failure = QUVI_OK;
918 errors = 0;
920 for (i=0, curr=inputs; curr; ++i)
922 char *url = quvi_llst_data(curr);
923 rc = quvi_parse(quvi, url, &media);
924 if (rc == QUVI_OK)
926 assert(media != 0);
927 dump_media(media);
929 if (opts->exec_given)
933 invoke_exec(media);
935 while (quvi_next_media_url(media) == QUVI_OK);
938 else
940 dump_error(quvi,rc);
941 last_failure = rc;
942 ++errors;
944 quvi_parse_close(&media);
945 assert(media == 0);
946 curr = quvi_llst_next(curr);
949 if (inputs_num > 1)
951 spew_qe("Results: %d OK, %d failed (last 0x%02x), exit with 0x%02x\n",
952 inputs_num - errors, errors, last_failure, rc);
955 return (rc);
958 /* vim: set ts=2 sw=2 tw=72 expandtab: */