m_match_playlist_script: Unescape URL
[libquvi.git] / examples / script.c
blob8ff9d3aa5ed8b4d2c7e53b0f5c25059b891a5adc
1 /* libquvi
2 * Copyright (C) 2012 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
17 * 02110-1301 USA
20 #include "config.h"
22 #include <locale.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <glib.h>
26 #include <quvi.h>
28 #include "examples.h"
30 struct _opts_s
32 gboolean autoproxy;
33 gchar **property;
34 gboolean verbose;
35 gchar *type;
38 static struct _opts_s opts;
40 static const GOptionEntry entries[] =
43 "property", 'p', 0, G_OPTION_ARG_STRING_ARRAY, &opts.property,
44 "Script property to output", "PROPERTY"
47 "type", 't', 0, G_OPTION_ARG_STRING, &opts.type,
48 "Script type", "TYPE"
51 "autoproxy", 'a', 0, G_OPTION_ARG_NONE, &opts.autoproxy,
52 "Enable the autoproxy feature", NULL
55 "verbose", 'v', 0, G_OPTION_ARG_NONE, &opts.verbose,
56 "Verbose libcurl output", NULL
58 {NULL, 0, 0, 0, NULL, NULL, NULL}
61 struct _property_lookup_s
63 QuviScriptProperty to;
64 const gchar *from;
67 static const struct _property_lookup_s property_conv[] =
69 {QUVI_SCRIPT_PROPERTY_FILEPATH, "filepath"},
70 {QUVI_SCRIPT_PROPERTY_FILENAME, "filename"},
71 {QUVI_SCRIPT_PROPERTY_DOMAINS, "domains"},
72 {QUVI_SCRIPT_PROPERTY_SHA1, "sha1"},
73 {0, NULL}
76 static void dump_script(QuviScriptType type)
78 QuviScriptProperty qsp;
79 gchar *s;
80 gint i,j;
82 for (i=0; opts.property[i] != NULL; ++i)
84 const gchar *p = opts.property[i];
85 for (j=0; property_conv[j].from != NULL; ++j)
87 if (g_strcmp0(p, property_conv[j].from) == 0)
89 qsp = property_conv[j].to;
90 break;
93 quvi_script_get(q, type, qsp, &s);
94 if (i == 0)
95 g_print("[%s]\n", __func__);
96 g_print(" %s=%s\n", p, s);
100 static gchar **property_sv()
102 gchar **r;
103 gint i,j;
105 i=0;
106 while (property_conv[i].from != NULL) ++i;
107 r = g_new(gchar*, i+1);
109 i=j=0;
110 while (property_conv[j].from != NULL)
111 r[i++] = g_strdup(property_conv[j++].from);
112 r[i] = NULL;
114 return (r);
117 static gboolean chk_property_values()
119 gchar **v, *s;
120 gboolean r;
122 v = property_sv();
123 r = examples_chk_val_sv(opts.property, v, &s);
124 if (r == FALSE)
126 g_printerr(
127 "error: invalid value (`%s') for the option `--property'\n", s);
130 g_strfreev(v);
131 v = NULL;
133 return (r);
136 struct _type_lookup_s
138 QuviScriptType to;
139 const gchar *from;
142 static const struct _type_lookup_s type_conv[] =
144 {QUVI_SCRIPT_TYPE_PLAYLIST, "playlist"},
145 {QUVI_SCRIPT_TYPE_MEDIA, "media"},
146 {QUVI_SCRIPT_TYPE_SCAN, "scan"},
147 {0, NULL}
150 static gchar **type_sv()
152 gchar **r;
153 gint i,j;
155 i=0;
156 while (type_conv[i].from != NULL) ++i;
157 r = g_new(gchar*, i+1);
159 i=j=0;
160 while (type_conv[j].from != NULL)
161 r[i++] = g_strdup(type_conv[j++].from);
162 r[i] = NULL;
164 return (r);
167 static gboolean chk_type_values()
169 gchar **v, *s;
170 gboolean r;
172 v = type_sv();
173 r = examples_chk_val_s(opts.type, v, &s);
174 if (r == FALSE)
176 g_printerr(
177 "error: invalid value (`%s') for the option `--type'\n", s);
180 g_strfreev(v);
181 v = NULL;
183 return (r);
186 static QuviScriptType type_n()
188 gint i;
189 for (i=0; type_conv[i].from != NULL; ++i)
191 if (g_strcmp0(opts.type, type_conv[i].from) == 0)
192 return (type_conv[i].to);
194 return (QUVI_SCRIPT_TYPE_MEDIA);
197 static gint opts_new(gint argc, gchar **argv)
199 GOptionContext *c;
200 GOptionGroup *g;
201 GError *e;
202 gint r;
204 c = g_option_context_new(NULL);
205 r = EXIT_SUCCESS;
206 g = NULL;
207 e = NULL;
209 g_option_context_set_help_enabled(c, TRUE);
210 g_option_context_add_main_entries(c, entries, NULL);
212 if (g_option_context_parse(c, &argc, &argv, &e) == FALSE)
214 g_printerr("error: %s\n", e->message);
215 g_error_free(e);
216 r = EXIT_FAILURE;
217 e = NULL;
220 g_option_context_free(c);
221 c = NULL;
223 /* Set the defaults. */
225 if (opts.property == NULL)
227 gchar *v[] = {"filename", "domains", NULL};
228 opts.property = g_strdupv(v);
231 if (opts.type == NULL)
232 opts.type = g_strdup("media");
234 /* Check input. */
236 if (chk_property_values() == FALSE)
237 return (EXIT_FAILURE);
239 if (chk_type_values() == FALSE)
240 return (EXIT_FAILURE);
242 return (r);
245 static void opts_free()
247 g_strfreev(opts.property);
248 opts.property = NULL;
250 g_free(opts.type);
251 opts.type = NULL;
254 typedef quvi_callback_status qcs;
256 gint main(gint argc, gchar **argv)
258 QuviScriptType type;
259 gint r;
261 setlocale(LC_ALL, "");
263 memset(&opts, 0, sizeof(struct _opts_s));
264 g_assert(q == NULL);
266 r = opts_new(argc, argv);
267 if (r != EXIT_SUCCESS)
268 return (r);
270 q = quvi_new();
271 examples_exit_if_error();
273 if (opts.autoproxy == TRUE)
274 examples_enable_autoproxy();
276 if (opts.verbose == TRUE)
277 examples_enable_verbose();
279 type = type_n();
282 gchar *p = g_strjoinv(",", opts.property);;
283 g_printerr("[%s] type=%s (0x%x), property=%s\n",
284 __func__, opts.type, type, p);
285 g_free(p);
286 p = NULL;
289 while (quvi_script_next(q, type) == QUVI_TRUE)
290 dump_script(type);
292 opts_free();
293 examples_cleanup();
295 g_assert(q == NULL);
296 return (r);
299 /* vim: set ts=2 sw=2 tw=72 expandtab: */