m_media_new: Store input URL in unescaped form
[libquvi.git] / examples / media.c
blob6085ca1aabd3adccffe195eec47912473d0c6b2e
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 gboolean autoproxy;
34 gboolean cookies;
35 gboolean verbose;
36 gboolean best;
37 gchar *stream;
38 gchar **url;
41 static struct _opts_s opts;
43 static const GOptionEntry entries[] =
46 "stream", 's', 0, G_OPTION_ARG_STRING, &opts.stream,
47 "Select stream ID, or a comma-separated list of IDs", "ID"
50 "best", 'b', 0, G_OPTION_ARG_NONE, &opts.best,
51 "Choose the best available stream, negates --stream", NULL
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 "cookies", 'c', 0, G_OPTION_ARG_NONE, &opts.cookies,
63 "Parse and use HTTP cookies", NULL
66 G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &opts.url, "URL"
68 {NULL, 0, 0, 0, NULL, NULL, NULL}
71 static void dump_stream()
73 gchar *url, *id;
75 quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_URL, &url);
76 quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_ID, &id);
78 g_print(" id='%s', url='%s'\n", id, url);
81 static void dump_streams()
83 while (quvi_media_stream_next(qm) == QUVI_TRUE)
84 dump_stream();
87 static gint opts_new(gint argc, gchar **argv)
89 GOptionContext *c;
90 GError *e;
91 gint r;
93 c = g_option_context_new("URL");
94 r = EXIT_SUCCESS;
95 e = NULL;
97 g_option_context_set_help_enabled(c, TRUE);
98 g_option_context_add_main_entries(c, entries, NULL);
100 if (g_option_context_parse(c, &argc, &argv, &e) == FALSE)
102 g_printerr("error: %s\n", e->message);
103 g_error_free(e);
104 r = EXIT_FAILURE;
105 e = NULL;
108 g_option_context_free(c);
109 c = NULL;
111 /* Check input. */
113 if (opts.url == NULL)
115 g_printerr("error: no input URL\n");
116 return (EXIT_FAILURE);
119 return (r);
122 static void opts_free()
124 g_free(opts.stream);
125 opts.stream = NULL;
127 g_strfreev(opts.url);
128 opts.url = NULL;
131 typedef quvi_callback_status qcs;
133 gint main(gint argc, gchar **argv)
135 gint i,r;
136 gchar *s;
138 g_assert(qm == NULL);
139 g_assert(q == NULL);
141 memset(&opts, 0, sizeof(struct _opts_s));
142 setlocale(LC_ALL, "");
144 r = opts_new(argc, argv);
145 if (r != EXIT_SUCCESS)
146 return (r);
148 q = quvi_new();
149 examples_exit_if_error();
151 if (opts.autoproxy == TRUE)
152 examples_enable_autoproxy();
154 if (opts.cookies == TRUE)
155 examples_enable_cookies();
157 if (opts.verbose == TRUE)
158 examples_enable_verbose();
160 quvi_set(q, QUVI_OPTION_CALLBACK_STATUS, (qcs) examples_status);
162 for (i=0; opts.url[i] != NULL; ++i)
164 qm = quvi_media_new(q, opts.url[i]);
165 examples_exit_if_error();
167 quvi_media_get(qm, QUVI_MEDIA_PROPERTY_TITLE, &s);
168 g_print("[%s]\n title='%s'\n", __func__, s);
170 if (opts.best == TRUE)
172 quvi_media_stream_choose_best(qm);
173 dump_stream();
175 else if (opts.stream != NULL)
177 quvi_media_stream_select(qm, opts.stream);
178 examples_exit_if_error();
179 dump_stream();
181 else
182 dump_streams();
184 quvi_media_free(qm);
185 qm = NULL;
188 opts_free();
189 examples_cleanup();
191 g_assert(qm == NULL);
192 g_assert(q == NULL);
194 return (r);
197 /* vim: set ts=2 sw=2 tw=72 expandtab: */