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/>.
33 gboolean format_subrip
;
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",
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
)
73 c
= g_option_context_new("URL");
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
);
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
);
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
,
103 quvi_subtitle_lang_get(l
, QUVI_SUBTITLE_LANG_PROPERTY_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
)
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
;
135 while ( (t
= quvi_subtitle_type_next(qsub
)) != NULL
)
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
;
162 memset(&opts
, 0, sizeof(struct _opts_s
));
163 setlocale(LC_ALL
, "");
165 r
= opts_new(argc
, argv
);
166 if (r
!= EXIT_SUCCESS
)
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");
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();
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
);
215 /* vim: set ts=2 sw=2 tw=72 expandtab: */