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
20 /* quvi.c - query media tool. */
31 #include <curl/curl.h>
35 #include "quvi/quvi.h"
36 #include "quvi/llst.h"
41 do { if (p) { free(p); p=0; } } while (0)
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
, ...)
60 vfprintf(stderr
, fmt
, ap
);
64 /* respects (q)uiet, prints to std(e)rr. */
65 static void spew_qe(const char *fmt
, ...)
71 vfprintf(stderr
, fmt
, ap
);
75 /* glorified printf. */
76 static void spew(const char *fmt
, ...)
80 vfprintf(stdout
, fmt
, ap
);
84 static void dump_error_json(quvi_t quvi
, QUVIcode rc
)
90 " \"message\": \"%s\"\n"
93 "}\n", quvi_strerror(quvi
, rc
));
96 static void dump_error_xml(quvi_t quvi
, QUVIcode rc
)
99 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
101 " <message>%s</message>\n"
103 quvi_strerror(quvi
, rc
));
106 static void dump_error(quvi_t quvi
, QUVIcode rc
)
108 if (opts
->export_errors_given
)
111 dump_error_xml(quvi
, rc
);
113 dump_error_json(quvi
, rc
);
116 fprintf(stderr
, "error: %s\n", quvi_strerror(quvi
, rc
));
119 static void handle_resolve_status(quvi_word type
)
121 if (type
== QUVISTATUSTYPE_DONE
)
124 spew_qe(":: Check for URL redirection ...");
127 static void handle_fetch_status(quvi_word type
, void *p
)
132 spew_qe(":: Fetch %s ...", (char *)p
);
134 case QUVISTATUSTYPE_CONFIG
:
135 spew_qe(":: Fetch config ...");
137 case QUVISTATUSTYPE_PLAYLIST
:
138 spew_qe(":: Fetch playlist ...");
140 case QUVISTATUSTYPE_DONE
:
146 static void handle_verify_status(quvi_word type
)
151 spew_qe(":: Verify media URL ...");
153 case QUVISTATUSTYPE_DONE
:
159 static int status_callback(long param
, void *data
)
161 quvi_word status
, type
;
163 status
= quvi_loword(param
);
164 type
= quvi_hiword(param
);
168 case QUVISTATUS_RESOLVE
:
169 handle_resolve_status(type
);
172 case QUVISTATUS_FETCH
:
173 handle_fetch_status(type
, data
);
176 case QUVISTATUS_VERIFY
:
177 handle_verify_status(type
);
186 /* Divided into smaller blocks. Otherwise -pedantic objects. */
190 " * Copyright (C) 2009-2011 Toni Gundogdu <legatvs@gmail.com>\n"
193 " * This library is free software; you can redistribute it and/or\n" \
194 " * modify it under the terms of the GNU Lesser General Public\n" \
195 " * License as published by the Free Software Foundation; either\n" \
196 " * version 2.1 of the License, or (at your option) any later version.\n"
199 " * This library is distributed in the hope that it will be useful,\n" \
200 " * but WITHOUT ANY WARRANTY; without even the implied warranty of\n" \
201 " * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n" \
202 " * Lesser General Public License for more details.\n"
205 " * You should have received a copy of the GNU Lesser General Public\n" \
206 " * License along with this library; if not, write to the Free Software\n" \
207 " * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA\n" \
208 " * 02110-1301 USA\n" " */"
210 static void license()
212 printf("%s *\n%s *\n%s *\n%s\n",
213 LICENSE_1
, LICENSE_2
, LICENSE_3
, LICENSE_4
);
222 static void print_version()
224 static const char version
[] =
230 " for " CANONICAL_TARGET
;
231 printf("quvi %s\n libquvi %s\n libquvi-scripts %s\n",
232 version
, quvi_version(QUVI_VERSION_LONG
),
233 quvi_version(QUVI_VERSION_SCRIPTS
));
237 static void dump_host(char *domain
, char *formats
)
239 printf("%s\t%s\n", domain
, formats
);
244 /* Wraps quvi_supported_ident. */
245 static void supported(quvi_t quvi
)
247 #ifndef _QUVI_SUPPORTED
256 for (i
=0; i
<opts
->inputs_num
; ++i
)
258 rc
= quvi_supported_ident(quvi
, (char*)opts
->inputs
[i
], &ident
);
261 quvi_ident_getprop(ident
, QUVI_IDENT_PROPERTY_FORMATS
, &formats
);
262 spew("%10s : %s\n", formats
, (char *)opts
->inputs
[i
]);
263 quvi_supported_ident_close(&ident
);
272 /* Query which formats are available for the URL */
273 static void query_formats(quvi_t quvi
)
278 if (opts
->inputs_num
< 1)
280 spew_qe("error: no input URLs\n");
284 for (i
=0; i
<opts
->inputs_num
; ++i
)
286 char *formats
= NULL
;
288 rc
= quvi_query_formats(quvi
, (char*)opts
->inputs
[i
], &formats
);
291 spew("%10s : %s\n", formats
, opts
->inputs
[i
]);
301 /* dumps all supported hosts to stdout. */
302 static void support(quvi_t quvi
)
306 if (opts
->inputs_num
> 0)
311 char *domain
, *formats
;
314 rc
= quvi_next_supported_website(quvi
, &domain
, &formats
);
319 dump_host(domain
, formats
);
325 spew_e("%s\n", quvi_strerror(quvi
, rc
));
333 static void invoke_exec(quvi_media_t media
)
335 char *cmd
, *media_url
, *q_media_url
;
336 char *page_title
, *q_page_title
, *t
;
339 quvi_getprop(media
, QUVIPROP_PAGETITLE
, &page_title
);
340 t
= strdup(page_title
);
341 t
= strepl(t
, "\"", "\\\""); /* Escape existing double quotation marks */
342 asprintf(&q_page_title
, "\"%s\"", t
); /* Put inside quotation marks */
345 quvi_getprop(media
, QUVIPROP_MEDIAURL
, &media_url
);
346 asprintf(&q_media_url
, "\"%s\"", media_url
);
348 cmd
= strdup(opts
->exec_arg
);
349 cmd
= strepl(cmd
, "%t", q_page_title
);
350 cmd
= strepl(cmd
, "%u", q_media_url
);
362 spew_e("error: failed to execute `%s'\n", cmd
);
365 spew_e("error: child exited with: %d\n", rc
>> 8);
376 double content_length
;
380 typedef struct parsed_url_s
*parsed_url_t
;
382 static void dump_media_url_xml(parsed_url_t p
, int i
)
384 char *media_url
= curl_easy_escape(curl
, p
->media_url
, 0);
386 spew(" <link id=\"%d\">\n", i
);
388 if (p
->content_length
)
389 spew(" <length_bytes>%.0f</length_bytes>\n", p
->content_length
);
391 if (strlen(p
->content_type
))
392 spew(" <content_type>%s</content_type>\n", p
->content_type
);
394 if (strlen(p
->file_suffix
))
395 spew(" <file_suffix>%s</file_suffix>\n", p
->file_suffix
);
397 spew(" <url>%s</url>\n"
399 media_url
? media_url
: p
->media_url
);
404 static void dump_media_url_json(parsed_url_t p
, int i
, int prepend_newln
)
410 " \"id\": \"%d\",\n", i
);
412 if (p
->content_length
)
413 spew(" \"length_bytes\": \"%.0f\",\n", p
->content_length
);
415 if (strlen(p
->content_type
))
416 spew(" \"content_type\": \"%s\",\n", p
->content_type
);
418 if (strlen(p
->file_suffix
))
419 spew(" \"file_suffix\": \"%s\",\n", p
->file_suffix
);
421 spew(" \"url\": \"%s\"\n"
426 static void dump_media_urls(quvi_media_t media
)
428 int json_flag
=0, i
=1;
431 struct parsed_url_s p
;
433 memset(&p
, 0, sizeof(&p
));
435 quvi_getprop(media
, QUVIPROP_MEDIAURL
, &p
.media_url
);
436 quvi_getprop(media
, QUVIPROP_MEDIACONTENTTYPE
, &p
.content_type
);
437 quvi_getprop(media
, QUVIPROP_MEDIACONTENTLENGTH
, &p
.content_length
);
438 quvi_getprop(media
, QUVIPROP_FILESUFFIX
, &p
.file_suffix
);
441 dump_media_url_xml(&p
,i
);
444 dump_media_url_json(&p
, i
, i
>1);
449 while (quvi_next_media_url(media
) == QUVI_OK
);
467 typedef struct parsed_s
*parsed_t
;
469 static void dump_media_xml(parsed_t p
)
471 char *e_page_url
, *e_thumb_url
;
473 e_page_url
= curl_easy_escape(curl
, p
->page_url
, 0);
474 e_thumb_url
= curl_easy_escape(curl
, p
->thumb_url
, 0);
476 spew("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
477 "<media id=\"%s\" host=\"%s\">\n"
478 " <format_requested>%s</format_requested>\n"
479 " <page_title>%s</page_title>\n"
480 " <page_url>%s</page_url>\n",
485 e_page_url
? e_page_url
: "");
487 if (strlen(p
->start_time
))
488 spew(" <start_time>%s</start_time>\n", p
->start_time
);
490 if (e_thumb_url
&& strlen(e_thumb_url
))
491 spew(" <thumbnail_url>%s</thumbnail_url>\n", e_thumb_url
);
494 spew(" <duration>%.0f</duration>\n", p
->duration
);
498 curl_free(e_page_url
);
504 curl_free(e_thumb_url
);
509 static void dump_media_json(parsed_t p
)
513 t
= strdup(p
->page_title
);
514 t
= strepl(t
, "\"", "\\\"");
517 " \"host\": \"%s\",\n"
518 " \"page_title\": \"%s\",\n"
519 " \"page_url\": \"%s\",\n"
521 " \"format_requested\": \"%s\",\n",
528 if (strlen(p
->start_time
))
529 spew(" \"start_time\": \"%s\",\n", p
->start_time
);
531 if (strlen(p
->thumb_url
))
532 spew(" \"thumbnail_url\": \"%s\",\n", p
->thumb_url
);
535 spew(" \"duration\": \"%.0f\",\n", p
->duration
);
537 spew(" \"link\": [\n");
542 static void dump_media(quvi_media_t media
)
546 memset(&p
, 0, sizeof(p
));
548 quvi_getprop(media
, QUVIPROP_HOSTID
, &p
.host
);
549 quvi_getprop(media
, QUVIPROP_PAGEURL
, &p
.page_url
);
550 quvi_getprop(media
, QUVIPROP_PAGETITLE
, &p
.page_title
);
551 quvi_getprop(media
, QUVIPROP_MEDIAID
, &p
.media_id
);
552 quvi_getprop(media
, QUVIPROP_FORMAT
, &p
.format
);
553 quvi_getprop(media
, QUVIPROP_STARTTIME
, &p
.start_time
);
554 quvi_getprop(media
, QUVIPROP_MEDIATHUMBNAILURL
, &p
.thumb_url
);
555 quvi_getprop(media
, QUVIPROP_MEDIADURATION
, &p
.duration
);
562 dump_media_urls(media
);
570 static quvi_t
init_quvi()
575 if ((rc
= quvi_init(&quvi
)) != QUVI_OK
)
577 dump_error(quvi
, rc
);
582 /* Set quvi options. */
584 if (opts
->format_given
)
585 quvi_setopt(quvi
, QUVIOPT_FORMAT
, opts
->format_arg
);
587 quvi_setopt(quvi
, QUVIOPT_NORESOLVE
, opts
->no_resolve_given
);
588 quvi_setopt(quvi
, QUVIOPT_NOVERIFY
, opts
->no_verify_given
);
590 if (opts
->category_all_given
)
591 quvi_setopt(quvi
, QUVIOPT_CATEGORY
, QUVIPROTO_ALL
);
595 if (opts
->category_http_given
)
597 if (opts
->category_mms_given
)
599 if (opts
->category_rtsp_given
)
601 if (opts
->category_rtmp_given
)
604 quvi_setopt(quvi
, QUVIOPT_CATEGORY
, n
);
607 quvi_setopt(quvi
, QUVIOPT_STATUSFUNCTION
, status_callback
);
609 /* Use the quvi created cURL handle. */
611 quvi_getinfo(quvi
, QUVIINFO_CURL
, &curl
);
614 if (opts
->agent_given
)
615 curl_easy_setopt(curl
, CURLOPT_USERAGENT
, opts
->agent_arg
);
617 if (opts
->proxy_given
)
618 curl_easy_setopt(curl
, CURLOPT_PROXY
, opts
->proxy_arg
);
620 if (opts
->no_proxy_given
)
621 curl_easy_setopt(curl
, CURLOPT_PROXY
, "");
623 curl_easy_setopt(curl
, CURLOPT_VERBOSE
, opts
->verbose_libcurl_given
);
625 curl_easy_setopt(curl
, CURLOPT_CONNECTTIMEOUT
,
626 opts
->connect_timeout_arg
);
631 static void cleanup()
633 quvi_llst_free(&inputs
);
634 assert(inputs
== NULL
);
638 assert(quvi
== NULL
);
642 cmdline_parser_free(opts
);
645 assert(opts
== NULL
);
648 static void read_from(FILE *f
, int close
)
655 while (fgets(b
, sizeof(b
), f
))
659 const size_t n
= strlen(b
)-1;
664 quvi_llst_append(&inputs
, strdup(b
));
675 static char *parse_url_scheme(const char *url
)
679 p
= strstr(url
, ":/");
683 asprintf(&r
, "%.*s", (int)(p
- url
), url
);
688 static int is_url(const char *s
)
690 char *p
= parse_url_scheme(s
);
699 static FILE* open_file(const char *path
)
701 FILE *f
= fopen(path
, "rt");
704 spew_e("error: %s: %s\n", path
, strerror(errno
));
711 static int read_input()
713 if (opts
->inputs_num
== 0)
718 for (i
=0; i
<opts
->inputs_num
; ++i
)
720 if (!is_url(opts
->inputs
[i
]))
721 read_from(open_file(opts
->inputs
[i
]), 1);
722 else /* Must be an URL. */
723 quvi_llst_append(&inputs
, strdup(opts
->inputs
[i
]));
726 return (quvi_llst_size(inputs
));
729 int main(int argc
, char *argv
[])
731 const char *home
, *no_config
, *fname
;
732 QUVIcode rc
, last_failure
;
733 quvi_llst_node_t curr
;
739 assert(quvi
== NULL
);
740 assert(curl
== NULL
);
741 assert(opts
== NULL
);
742 assert(inputs
== NULL
);
744 no_config
= getenv("QUVI_NO_CONFIG");
747 home
= getenv("QUVI_HOME");
749 home
= getenv("HOME");
759 opts
= calloc(1, sizeof(struct gengetopt_args_info
));
763 /* Init cmdline parser. */
765 if (home
&& !no_config
)
770 asprintf(&path
, "%s%s", home
, fname
);
771 f
= fopen(path
, "r");
775 struct cmdline_parser_params
*pp
;
780 pp
= cmdline_parser_params_create();
781 pp
->check_required
= 0;
783 if (cmdline_parser_config_file(path
, opts
, pp
) == 0)
787 pp
->check_required
= 1;
789 if (cmdline_parser_ext(argc
, argv
, opts
, pp
) == 0)
800 if (cmdline_parser(argc
, argv
, opts
) != 0)
801 return (QUVI_INVARG
);
804 if (opts
->version_given
)
807 if (opts
->license_given
)
810 verbose_flag
= !opts
->quiet_given
;
814 if (opts
->query_formats_given
)
817 if (opts
->support_given
)
822 inputs_num
= read_input();
826 spew_qe("error: no input URLs\n");
827 return (QUVI_INVARG
);
830 last_failure
= QUVI_OK
;
833 for (i
=0, curr
=inputs
; curr
; ++i
)
835 char *url
= quvi_llst_data(curr
);
836 rc
= quvi_parse(quvi
, url
, &media
);
842 if (opts
->exec_given
)
848 while (quvi_next_media_url(media
) == QUVI_OK
);
857 quvi_parse_close(&media
);
859 curr
= quvi_llst_next(curr
);
864 spew_qe("Results: %d OK, %d failed (last 0x%02x), exit with 0x%02x\n",
865 inputs_num
- errors
, errors
, last_failure
, rc
);
871 /* vim: set ts=2 sw=2 tw=72 expandtab: */