m_media_new: Store input URL in unescaped form
[libquvi.git] / examples / script.c
blob629d56477db1311956afa8f894e9430f01f1665c
1 /* libquvi
2 * Copyright (C) 2012-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 gchar **property;
34 gchar *type;
37 static struct _opts_s opts;
39 static const GOptionEntry entries[] =
42 "property", 'p', 0, G_OPTION_ARG_STRING_ARRAY, &opts.property,
43 "Script property to output", "PROPERTY"
46 "type", 't', 0, G_OPTION_ARG_STRING, &opts.type,
47 "Script type", "TYPE"
49 {NULL, 0, 0, 0, NULL, NULL, NULL}
52 struct _property_lookup_s
54 QuviScriptProperty to;
55 const gchar *from;
58 static const struct _property_lookup_s property_conv[] =
60 {QUVI_SCRIPT_PROPERTY_EXPORT_FORMAT, "export.format"},
61 {QUVI_SCRIPT_PROPERTY_FILEPATH, "filepath"},
62 {QUVI_SCRIPT_PROPERTY_FILENAME, "filename"},
63 {QUVI_SCRIPT_PROPERTY_DOMAINS, "domains"},
64 {QUVI_SCRIPT_PROPERTY_SHA1, "sha1"},
65 {0, NULL}
68 static void dump_script(QuviScriptType type)
70 QuviScriptProperty qsp;
71 gchar *s;
72 gint i,j;
74 for (i=0; opts.property[i] != NULL; ++i)
76 const gchar *p = opts.property[i];
77 for (j=0; property_conv[j].from != NULL; ++j)
79 if (g_strcmp0(p, property_conv[j].from) == 0)
81 qsp = property_conv[j].to;
82 break;
85 quvi_script_get(q, type, qsp, &s);
86 if (i == 0)
87 g_print("[%s]\n", __func__);
88 g_print(" %s=%s\n", p, s);
92 static gchar **property_sv()
94 gchar **r;
95 gint i,j;
97 i=0;
98 while (property_conv[i].from != NULL) ++i;
99 r = g_new(gchar*, i+1);
101 i=j=0;
102 while (property_conv[j].from != NULL)
103 r[i++] = g_strdup(property_conv[j++].from);
104 r[i] = NULL;
106 return (r);
109 static gboolean chk_property_values()
111 gchar **v, *s;
112 gboolean r;
114 v = property_sv();
115 r = examples_chk_val_sv(opts.property, v, &s);
116 if (r == FALSE)
118 g_printerr(
119 "error: invalid value (`%s') for the option `--property'\n", s);
121 g_strfreev(v);
122 return (r);
125 struct _type_lookup_s
127 QuviScriptType to;
128 const gchar *from;
131 static const struct _type_lookup_s type_conv[] =
133 {QUVI_SCRIPT_TYPE_SUBTITLE_EXPORT, "subtitle.export"},
134 {QUVI_SCRIPT_TYPE_SUBTITLE, "subtitle"},
135 {QUVI_SCRIPT_TYPE_PLAYLIST, "playlist"},
136 {QUVI_SCRIPT_TYPE_MEDIA, "media"},
137 {QUVI_SCRIPT_TYPE_SCAN, "scan"},
138 {0, NULL}
141 static gchar **type_sv()
143 gchar **r;
144 gint i,j;
146 i=0;
147 while (type_conv[i].from != NULL) ++i;
148 r = g_new(gchar*, i+1);
150 i=j=0;
151 while (type_conv[j].from != NULL)
152 r[i++] = g_strdup(type_conv[j++].from);
153 r[i] = NULL;
155 return (r);
158 static gboolean chk_type_values()
160 gchar **v, *s;
161 gboolean r;
163 v = type_sv();
164 r = examples_chk_val_s(opts.type, v, &s);
165 if (r == FALSE)
167 g_printerr(
168 "error: invalid value (`%s') for the option `--type'\n", s);
171 g_strfreev(v);
172 v = NULL;
174 return (r);
177 static QuviScriptType type_n()
179 gint i;
180 for (i=0; type_conv[i].from != NULL; ++i)
182 if (g_strcmp0(opts.type, type_conv[i].from) == 0)
183 return (type_conv[i].to);
185 return (QUVI_SCRIPT_TYPE_MEDIA);
188 static gint opts_new(gint argc, gchar **argv)
190 GOptionContext *c;
191 GError *e;
192 gint r;
194 c = g_option_context_new(NULL);
195 r = EXIT_SUCCESS;
196 e = NULL;
198 g_option_context_set_help_enabled(c, TRUE);
199 g_option_context_add_main_entries(c, entries, NULL);
201 if (g_option_context_parse(c, &argc, &argv, &e) == FALSE)
203 g_printerr("error: %s\n", e->message);
204 g_error_free(e);
205 r = EXIT_FAILURE;
207 g_option_context_free(c);
209 /* Set the defaults. */
211 if (opts.property == NULL)
213 gchar *v[] = {"filename", "domains", NULL};
214 opts.property = g_strdupv(v);
217 if (opts.type == NULL)
218 opts.type = g_strdup("media");
220 /* Check input. */
222 if (chk_property_values() == FALSE)
223 return (EXIT_FAILURE);
225 if (chk_type_values() == FALSE)
226 return (EXIT_FAILURE);
228 return (r);
231 static void opts_free()
233 g_strfreev(opts.property);
234 opts.property = NULL;
236 g_free(opts.type);
237 opts.type = NULL;
240 gint main(gint argc, gchar **argv)
242 QuviScriptType type;
243 gint r;
245 setlocale(LC_ALL, "");
247 memset(&opts, 0, sizeof(struct _opts_s));
248 g_assert(q == NULL);
250 r = opts_new(argc, argv);
251 if (r != EXIT_SUCCESS)
252 return (r);
254 q = quvi_new();
255 examples_exit_if_error();
257 type = type_n();
259 gchar *p = g_strjoinv(",", opts.property);
260 g_printerr("[%s] type=%s (0x%x), property=%s\n",
261 __func__, opts.type, type, p);
262 g_free(p);
265 while (quvi_script_next(q, type) == QUVI_TRUE)
266 dump_script(type);
268 examples_cleanup();
269 opts_free();
271 g_assert(q == NULL);
272 return (r);
275 /* vim: set ts=2 sw=2 tw=72 expandtab: */