m_media_new: Store input URL in unescaped form
[libquvi.git] / examples / subtitle.c
blob1e304d4a01480ee70ff230116789cc70973e1229
1 /* libquvi
2 * Copyright (C) 2013 Toni Gundogdu <legatvs@gmail.com>
4 * This file is part of libquvi <http://quvi.sourceforge.net/>.
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU Affero General Public
8 * License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General
17 * Public License along with this program. If not, see
18 * <http://www.gnu.org/licenses/>.
21 #include "config.h"
23 #include <locale.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <glib.h>
27 #include <quvi.h>
29 #include "examples.h"
31 struct _opts_s
33 gboolean format_subrip;
34 gboolean autoproxy;
35 gboolean verbose;
36 gchar *language;
37 gchar **url;
40 static struct _opts_s opts;
42 static const GOptionEntry entries[] =
45 "format-subrip", 's', 0, G_OPTION_ARG_NONE, &opts.format_subrip,
46 "Retrieve selected language, convert it to SubRip and dump to stdout",
47 NULL
50 "language", 'l', 0, G_OPTION_ARG_STRING, &opts.language,
51 "Select subtitle language by ID, or a comma-separated list of IDs", "ID"
54 "autoproxy", 'a', 0, G_OPTION_ARG_NONE, &opts.autoproxy,
55 "Enable the autoproxy feature", NULL
58 "verbose", 'v', 0, G_OPTION_ARG_NONE, &opts.verbose,
59 "Verbose libcurl output", NULL
62 G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &opts.url, "URL"
64 {NULL, 0, 0, 0, NULL, NULL, NULL}
67 static gint opts_new(gint argc, gchar **argv)
69 GOptionContext *c;
70 GError *e;
71 gint r;
73 c = g_option_context_new("URL");
74 r = EXIT_SUCCESS;
75 e = NULL;
77 g_option_context_add_main_entries(c, entries, NULL);
78 g_option_context_set_help_enabled(c, TRUE);
80 if (g_option_context_parse(c, &argc, &argv, &e) == FALSE)
82 g_printerr("error: %s\n", e->message);
83 r = EXIT_FAILURE;
84 g_error_free(e);
86 g_option_context_free(c);
88 if (r == EXIT_SUCCESS && opts.url == NULL)
90 g_printerr("error: no input URL\n");
91 return (EXIT_FAILURE);
93 return (r);
96 static void dump_language(const quvi_subtitle_lang_t l)
98 gchar *translated, *original, *code, *id, *url;
100 quvi_subtitle_lang_get(l, QUVI_SUBTITLE_LANG_PROPERTY_TRANSLATED,
101 &translated);
103 quvi_subtitle_lang_get(l, QUVI_SUBTITLE_LANG_PROPERTY_ORIGINAL,
104 &original);
106 quvi_subtitle_lang_get(l, QUVI_SUBTITLE_LANG_PROPERTY_CODE, &code);
107 quvi_subtitle_lang_get(l, QUVI_SUBTITLE_LANG_PROPERTY_URL, &url);
108 quvi_subtitle_lang_get(l, QUVI_SUBTITLE_LANG_PROPERTY_ID, &id);
110 g_printerr(" lang: translated=%s, original=%s, code=%s, id=%s\n"
111 " url=%s\n", translated, original, code, id, url);
114 static void dump_languages(const quvi_subtitle_type_t t)
116 quvi_subtitle_lang_t l;
117 while ( (l = quvi_subtitle_type_next(t)) != NULL)
118 dump_language(l);
121 static void dump_subtitle(quvi_subtitle_type_t t)
123 gdouble type, format;
124 quvi_subtitle_type_get(t, QUVI_SUBTITLE_TYPE_PROPERTY_FORMAT, &format);
125 quvi_subtitle_type_get(t, QUVI_SUBTITLE_TYPE_PROPERTY_TYPE, &type);
126 g_printerr("[%s] subtitle: format=%g, type=%g\n", __func__, format, type);
129 static void dump_subtitles()
131 quvi_subtitle_type_t t;
132 gint i;
134 i = 0;
135 while ( (t = quvi_subtitle_type_next(qsub)) != NULL)
137 dump_subtitle(t);
138 dump_languages(t);
139 ++i;
142 if (i == 0)
143 g_printerr("subtitles: none found\n");
146 static void opts_free()
148 g_free(opts.language);
149 g_strfreev(opts.url);
151 memset(&opts, 0, sizeof(struct _opts_s));
154 typedef quvi_callback_status qcs;
156 gint main(gint argc, gchar **argv)
158 quvi_subtitle_export_t e;
159 quvi_subtitle_lang_t l;
160 gint r, i;
162 memset(&opts, 0, sizeof(struct _opts_s));
163 setlocale(LC_ALL, "");
165 r = opts_new(argc, argv);
166 if (r != EXIT_SUCCESS)
167 return (r);
169 q = quvi_new();
170 examples_exit_if_error();
172 if (opts.autoproxy == TRUE)
173 examples_enable_autoproxy();
175 if (opts.verbose == TRUE)
176 examples_enable_verbose();
178 if (opts.format_subrip == TRUE && opts.language == NULL)
180 g_printerr("error: --format-subrip requires --language\n");
181 examples_cleanup();
182 opts_free();
183 return (EXIT_FAILURE);
186 quvi_set(q, QUVI_OPTION_CALLBACK_STATUS, (qcs) examples_status);
188 for (r=EXIT_SUCCESS, i=0; (opts.url[i] != NULL && r == EXIT_SUCCESS); ++i)
190 qsub = quvi_subtitle_new(q, opts.url[i]);
191 examples_exit_if_error();
193 if (opts.language != NULL)
195 l = quvi_subtitle_select(qsub, opts.language);
196 examples_exit_if_error();
197 dump_language(l);
198 if (opts.format_subrip == TRUE)
200 e = quvi_subtitle_export_new(l, "srt");
201 examples_exit_if_error();
202 g_print("%s", quvi_subtitle_export_data(e));
203 quvi_subtitle_export_free(e);
206 else
207 dump_subtitles();
209 examples_cleanup();
210 opts_free();
212 return (r);
215 /* vim: set ts=2 sw=2 tw=72 expandtab: */