summaryrefslogtreecommitdiff
path: root/plparse/videosite-parser.c
blob: be374aa940d2226de41764366930fc16152778dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
   Copyright (C) 2013 Bastien Nocera <hadess@hadess.net>

   The Gnome Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301  USA.

   Author: Bastien Nocera <hadess@hadess.net>
 */

#include "config.h"

#include <locale.h>

#include <glib.h>
#include <quvi.h>
#include "totem-pl-parser.h"

#define BASE 20

static char *url = NULL;
static gboolean check = FALSE;
static gboolean debug = FALSE;

const GOptionEntry options[] = {
	{ "url", 'u', 0, G_OPTION_ARG_FILENAME, &url, "URL of the video site page", NULL },
	{ "check", 'c', 0, G_OPTION_ARG_NONE, &check, "Check whether this URL is supported", NULL },
	{ "debug", 'd', 0, G_OPTION_ARG_NONE, &debug, "Turn on debug mode", NULL },
	{ NULL }
};

static gboolean
supports_uri (const char *uri)
{
	quvi_t q;
	QuviBoolean r;

	q = quvi_new ();
	r = quvi_supports (q, uri, QUVI_SUPPORTS_MODE_OFFLINE, QUVI_SUPPORTS_TYPE_ANY);
	quvi_free (q);

	return r;
}

static struct {
	const char *container;
	const char *content_type;
} containers [] = {
	{ "webm", "video/webm" },
};

static const char *
container_to_content_type (const char *container)
{
	guint i;

	if (container == NULL)
		return NULL;
	for (i = 0; i < G_N_ELEMENTS (containers); i++) {
		if (g_str_equal (container, containers[i].container))
			return containers[i].content_type;
	}
	return NULL;
}

static void
print (const char *name,
       const char *value)
{
	g_return_if_fail (name != NULL);

	if (value == NULL)
		return;

	g_print ("%s=%s\n", name, value);
}

static void
parse_videosite (const char *uri)
{
	quvi_t q;
	quvi_media_t qm;
	/* properties */
	const char *video_uri;
	const char *title;
	const char *id;
	const char *content_type;
	const char *thumb_url;
	const char *container;
	double duration;
	double starttime;
	char *duration_str = NULL;
	char *starttime_str = NULL;

	if (!supports_uri (uri)) {
		g_print ("TOTEM_PL_PARSER_RESULT_UNHANDLED");
		return;
	}

	q = quvi_new ();
	qm = quvi_media_new (q, uri);

	/* Empty results list? */
	if (quvi_media_stream_next(qm) != QUVI_TRUE) {
		if (debug)
			g_print ("Parsing '%s' failed with error: %s\n",
				 uri, quvi_errmsg (q));
		g_print ("TOTEM_PL_PARSER_RESULT_ERROR");
		goto out;
	}

	/* Choose the best stream */
	quvi_media_stream_choose_best (qm);

	quvi_media_get (qm, QUVI_MEDIA_PROPERTY_TITLE, &title);
	quvi_media_get (qm, QUVI_MEDIA_PROPERTY_ID, &id);
	quvi_media_get (qm, QUVI_MEDIA_PROPERTY_THUMBNAIL_URL, &thumb_url);
	quvi_media_get (qm, QUVI_MEDIA_PROPERTY_DURATION_MS, &duration);
	if (duration)
		duration_str = g_strdup_printf ("%f", duration);
	quvi_media_get (qm, QUVI_MEDIA_STREAM_PROPERTY_URL, &video_uri);
	quvi_media_get (qm, QUVI_MEDIA_PROPERTY_START_TIME_MS, &starttime);
	if (starttime)
		starttime_str = g_strdup_printf ("%f", starttime);

	quvi_media_get (qm, QUVI_MEDIA_STREAM_PROPERTY_CONTAINER, &container);
	content_type = container_to_content_type (container);

	if (video_uri != NULL) {
		print (TOTEM_PL_PARSER_FIELD_TITLE, title);
		print (TOTEM_PL_PARSER_FIELD_ID, id);
		print (TOTEM_PL_PARSER_FIELD_MOREINFO, uri);
		print (TOTEM_PL_PARSER_FIELD_URI, video_uri);
		print (TOTEM_PL_PARSER_FIELD_STARTTIME, starttime_str);
		print (TOTEM_PL_PARSER_FIELD_CONTENT_TYPE, content_type);
		print (TOTEM_PL_PARSER_FIELD_IMAGE_URI, thumb_url);
		print (TOTEM_PL_PARSER_FIELD_DURATION, duration_str);
	}

	g_free (starttime_str);
	g_free (duration_str);

out:
	quvi_media_free (qm);
	quvi_free (q);
}

int main (int argc, char **argv)
{
	GOptionContext *context;

	setlocale (LC_ALL, "");

	context = g_option_context_new (NULL);
	g_option_context_set_summary (context, "totem-pl-parser libquvi Helper");
	g_option_context_add_main_entries (context, options, NULL);
	g_option_context_parse (context, &argc, &argv, NULL);

	if (url == NULL) {
		char *txt;

		txt = g_option_context_get_help (context, FALSE, NULL);
		g_print ("%s", txt);
		g_free (txt);

		g_option_context_free (context);

		return 1;
	}
	g_option_context_free (context);

	if (check) {
		g_print ("%s", supports_uri (url) ? "TRUE" : "FALSE");
		return 0;
	}

	parse_videosite (url);

	return 0;
}