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
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 handle_resolve_status(quvi_word type
)
86 if (type
== QUVISTATUSTYPE_DONE
)
89 spew_qe(":: Check for URL redirection ...");
92 static void handle_fetch_status(quvi_word type
, void *p
)
97 spew_qe(":: Fetch %s ...", (char *)p
);
99 case QUVISTATUSTYPE_CONFIG
:
100 spew_qe(":: Fetch config ...");
102 case QUVISTATUSTYPE_PLAYLIST
:
103 spew_qe(":: Fetch playlist ...");
105 case QUVISTATUSTYPE_DONE
:
111 static void handle_verify_status(quvi_word type
)
116 spew_qe(":: Verify media URL ...");
118 case QUVISTATUSTYPE_DONE
:
124 static int status_callback(long param
, void *data
)
126 quvi_word status
, type
;
128 status
= quvi_loword(param
);
129 type
= quvi_hiword(param
);
133 case QUVISTATUS_RESOLVE
:
134 handle_resolve_status(type
);
137 case QUVISTATUS_FETCH
:
138 handle_fetch_status(type
, data
);
141 case QUVISTATUS_VERIFY
:
142 handle_verify_status(type
);
151 static size_t write_callback(void *p
, size_t size
, size_t nmemb
,
154 size_t r
= quvi_write_callback_default(p
, size
, nmemb
, data
);
155 /* Could do something useful here. */
162 /* Divided into smaller blocks. Otherwise -pedantic objects. */
166 " * Copyright (C) 2009,2010,2011 Toni Gundogdu <legatvs@gmail.com>\n"
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"
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"
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
);
198 static void version()
200 printf("quvi version %s\n", quvi_version(QUVI_VERSION_LONG
));
204 static void dump_host(char *domain
, char *formats
)
206 printf("%s\t%s\n", domain
, formats
);
211 /* Wraps quvi_supported_ident. */
212 static void supported(quvi_t quvi
)
214 #ifndef _QUVI_SUPPORTED
223 for (i
=0; i
<opts
->inputs_num
; ++i
)
225 rc
= quvi_supported_ident(quvi
, (char*)opts
->inputs
[i
], &ident
);
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
);
233 spew_qe("error: %s\n", quvi_strerror(quvi
, rc
));
239 static const char depr_msg
[] =
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
)
248 if (strcmp(opts
->format_arg
, "help") == 0)
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"
256 " --format list youtube print youtube formats\n"
257 " --format fmt34_360p get format fmt34_360p of media\n"
263 else if (strcmp(opts
->format_arg
, "list") == 0)
271 quvi_next_supported_website(quvi
, &d
, &f
);
280 /* -f list <pattern> */
281 if (opts
->inputs_num
> 0)
282 print
= strstr(d
, (char *)opts
->inputs
[0]) != 0;
286 printf("%s:\n %s\n\n", d
, f
);
298 spew_e("%s\n", quvi_strerror(quvi
, rc
));
302 spew_qe("%s\n", depr_msg
);
310 /* Query which formats are available for the URL */
311 static void query_formats(quvi_t quvi
)
316 if (opts
->inputs_num
< 1)
318 spew_qe("error: no input URLs\n");
322 for (i
=0; i
<opts
->inputs_num
; ++i
)
324 char *formats
= NULL
;
326 rc
= quvi_query_formats(quvi
, (char*)opts
->inputs
[i
], &formats
);
329 spew("%10s : %s\n", formats
, opts
->inputs
[i
]);
333 spew_qe("%s\n", quvi_strerror(quvi
, rc
));
339 /* dumps all supported hosts to stdout. */
340 static void support(quvi_t quvi
)
344 if (opts
->inputs_num
> 0)
349 char *domain
, *formats
;
352 rc
= quvi_next_supported_website(quvi
, &domain
, &formats
);
357 dump_host(domain
, formats
);
363 spew_e("%s\n", quvi_strerror(quvi
, rc
));
371 static void invoke_exec(quvi_media_t media
)
373 char *cmd
, *media_url
, *q_media_url
;
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
);
392 spew_e("error: failed to execute `%s'\n", cmd
);
395 spew_e("error: child exited with: %d\n", rc
>> 8);
406 double content_length
;
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"
429 media_url
? media_url
: p
->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
)
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"
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
);
485 dump_media_url_xml(&p
,i
);
486 else if (opts
->old_given
)
487 dump_media_url_old(&p
,i
);
490 dump_media_url_json(&p
, i
, i
>1);
495 while (quvi_next_media_url(media
) == QUVI_OK
);
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",
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
);
540 spew(" <duration>%.0f</duration>\n", p
->duration
);
544 curl_free(e_page_url
);
550 curl_free(e_thumb_url
);
555 static void dump_media_old(parsed_t p
)
557 spew(" > Dump media:\n"
562 "format : %s (requested)\n",
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
);
576 spew("duration: %.0f\n", p
->duration
);
579 static void dump_media_json(parsed_t p
)
583 t
= strdup(p
->page_title
);
584 t
= strepl(t
, "\"", "\\\"");
587 " \"host\": \"%s\",\n"
588 " \"page_title\": \"%s\",\n"
589 " \"page_url\": \"%s\",\n"
591 " \"format_requested\": \"%s\",\n",
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
);
605 spew(" \"duration\": \"%.0f\",\n", p
->duration
);
607 spew(" \"link\": [\n");
612 static void dump_media(quvi_media_t media
)
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
);
629 else if (opts
->old_given
)
634 dump_media_urls(media
);
638 else if (opts
->old_given
) ;
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()
653 if ((rc
= quvi_init(&quvi
)) != QUVI_OK
)
655 dump_error(quvi
, rc
);
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
);
678 if (opts
->category_http_given
)
680 if (opts
->category_mms_given
)
682 if (opts
->category_rtsp_given
)
684 if (opts
->category_rtmp_given
)
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
);
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
);
715 static void cleanup()
717 quvi_llst_free(&inputs
);
718 assert(inputs
== NULL
);
722 assert(quvi
== NULL
);
726 cmdline_parser_free(opts
);
729 assert(opts
== NULL
);
732 static void read_from(FILE *f
, int close
)
739 while (fgets(b
, sizeof(b
), f
))
743 const size_t n
= strlen(b
)-1;
748 quvi_llst_append(&inputs
, strdup(b
));
759 static char *parse_url_scheme(const char *url
)
763 p
= strstr(url
, ":/");
767 asprintf(&r
, "%.*s", (int)(p
- url
), url
);
772 static int is_url(const char *s
)
774 char *p
= parse_url_scheme(s
);
783 static FILE* open_file(const char *path
)
785 FILE *f
= fopen(path
, "rt");
788 spew_e("error: %s: %s\n", path
, strerror(errno
));
795 static int read_input()
797 if (opts
->inputs_num
== 0)
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
;
823 assert(quvi
== NULL
);
824 assert(curl
== NULL
);
825 assert(opts
== NULL
);
826 assert(inputs
== NULL
);
828 no_config
= getenv("QUVI_NO_CONFIG");
831 home
= getenv("QUVI_HOME");
833 home
= getenv("HOME");
843 opts
= calloc(1, sizeof(struct gengetopt_args_info
));
847 /* Init cmdline parser. */
849 if (home
&& !no_config
)
854 asprintf(&path
, "%s%s", home
, fname
);
855 f
= fopen(path
, "r");
859 struct cmdline_parser_params
*pp
;
864 pp
= cmdline_parser_params_create();
865 pp
->check_required
= 0;
867 if (cmdline_parser_config_file(path
, opts
, pp
) == 0)
871 pp
->check_required
= 1;
873 if (cmdline_parser_ext(argc
, argv
, opts
, pp
) == 0)
884 if (cmdline_parser(argc
, argv
, opts
) != 0)
885 return (QUVI_INVARG
);
888 if (opts
->version_given
)
891 if (opts
->license_given
)
894 verbose_flag
= !opts
->quiet_given
;
898 if (opts
->query_formats_given
)
901 if (opts
->support_given
)
904 if (opts
->format_given
)
909 inputs_num
= read_input();
913 spew_qe("error: no input URLs\n");
914 return (QUVI_INVARG
);
917 last_failure
= QUVI_OK
;
920 for (i
=0, curr
=inputs
; curr
; ++i
)
922 char *url
= quvi_llst_data(curr
);
923 rc
= quvi_parse(quvi
, url
, &media
);
929 if (opts
->exec_given
)
935 while (quvi_next_media_url(media
) == QUVI_OK
);
944 quvi_parse_close(&media
);
946 curr
= quvi_llst_next(curr
);
951 spew_qe("Results: %d OK, %d failed (last 0x%02x), exit with 0x%02x\n",
952 inputs_num
- errors
, errors
, last_failure
, rc
);
958 /* vim: set ts=2 sw=2 tw=72 expandtab: */