-
Notifications
You must be signed in to change notification settings - Fork 1
/
grl-arteplus7.vala
400 lines (351 loc) · 13.8 KB
/
grl-arteplus7.vala
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
* Totem Arte Plugin allows you to watch streams from arte.tv
* Copyright (C) 2015 Simon Wenner <simon@wenner.ch>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* The Totem Arte Plugin project hereby grants permission for non-GPL compatible
* GStreamer plugins to be used and distributed together with GStreamer, Totem
* and Totem Arte Plugin. This permission is above and beyond the permissions
* granted by the GPL license by which Totem Arte Plugin is covered.
* If you modify this code, you may extend this exception to your version of the
* code, but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version.
*
*/
using Grl;
using GLib;
Grl.Media construct_media_container ()
{
#if GRILO_VERSION_3
return new Grl.Media.container_new ();
#else
return new Grl.MediaBox ();
#endif
}
Grl.Media construct_media_video ()
{
#if GRILO_VERSION_3
return new Grl.Media.video_new ();
#else
return new Grl.MediaVideo ();
#endif
}
class GrlArteSource : Grl.Source
{
private GLib.List<weak Grl.KeyID> supported_keys_;
private ArteParser parsers[1]; /* array of parsers */
private GLib.Settings settings;
private GLib.Settings proxy_settings;
private Language language;
private VideoQuality quality;
private UrlExtractor extractor;
/* A common SourceSpec to ease code reuse between browse() and search() */
private struct SourceArteSpec {
weak Grl.Source source;
uint operation_id;
weak Grl.Media container;
weak string text;
weak Grl.SourceResultCb callback;
}
construct {
/* Debug log handling */
#if DEBUG_MESSAGES
GLib.Log.set_handler ("GrlArte", GLib.LogLevelFlags.LEVEL_DEBUG,
(domain, levels, msg) => {
stdout.printf ("%s-DEBUG: %s\n", domain, msg);
});
#endif
this.settings = new GLib.Settings (DCONF_ID);
this.proxy_settings = new GLib.Settings (DCONF_HTTP_PROXY);
load_properties ();
this.extractor = new RTMPStreamUrlExtractor ();
/* Generate the user-agent */
TimeVal tv = TimeVal ();
tv.get_current_time ();
/* First, we need to compute the number of weeks since Epoch */
int weeks = (int) tv.tv_sec / ( 60 * 60 * 24 * 7 );
/* We know there is a new Firefox release each 6 weeks.
And we know Firefox 11.0 was released the 03/13/2012, which
corresponds to 2201 weeks after Epoch.
We add a 3 weeks margin and then we can compute the version number! */
int version = 11 + (weeks - (2201 + 3)) / 6;
USER_AGENT = USER_AGENT_TMPL.printf(version, version);
debug (USER_AGENT);
parsers[0] = new ArteRSSParser ();
}
public GrlArteSource ()
{
source_id = "grl-arteplus7";
source_name = "Arte+7";
source_desc = "Arte+7 video provider";
#if GRILO_VERSION_3
supported_media = Grl.SupportedMedia.VIDEO;
#else
supported_media = Grl.MediaType.VIDEO;
#endif
source_tags = {"tv", "net:internet", "country:fr", "country:de"};
source_icon = new FileIcon(File.new_for_path (ICON));
supported_keys_ = Grl.MetadataKey.list_new(Grl.MetadataKey.ID,
Grl.MetadataKey.TITLE,
Grl.MetadataKey.URL,
Grl.MetadataKey.THUMBNAIL,
Grl.MetadataKey.DESCRIPTION,
Grl.MetadataKey.DURATION,
Grl.MetadataKey.SITE);
// TODO
// CREATION_DATE, PUBLICATION_DATE
// EXTERNAL_URL
// LICENSE
}
private void set_language (Language new_language)
{
language = new_language;
if (!settings.set_enum ("language", (int) new_language)) {
GLib.warning ("Storing the language setting failed.");
}
}
private void set_quality (VideoQuality new_quality)
{
quality = new_quality;
if (!settings.set_enum ("quality", (int) new_quality)) {
GLib.warning ("Storing the quality setting failed.");
}
}
/* loads properties from dconf */
private void load_properties ()
{
string parsed_proxy_uri = "";
int proxy_port;
quality = (VideoQuality) settings.get_enum ("quality");
language = (Language) settings.get_enum ("language");
use_proxy = proxy_settings.get_boolean ("enabled");
if (use_proxy) {
parsed_proxy_uri = proxy_settings.get_string ("host");
proxy_port = proxy_settings.get_int ("port");
if (parsed_proxy_uri == "") {
use_proxy = false; /* necessary to prevent a crash in this case */
} else {
proxy_uri = new Soup.URI ("http://" + parsed_proxy_uri + ":" + proxy_port.to_string());
debug ("Using proxy: %s", proxy_uri.to_string (false));
proxy_username = proxy_settings.get_string ("authentication-user");
proxy_password = proxy_settings.get_string ("authentication-password");
}
}
}
public override unowned GLib.List<weak Grl.KeyID> supported_keys ()
{
return supported_keys_;
}
private void browse_language (SourceArteSpec ars)
{
Grl.Media lang_box = construct_media_container ();
lang_box.set_title (_("French"));
lang_box.set_id (BOX_LANGUAGE_FRENCH);
ars.callback (ars.source, ars.operation_id, lang_box, 1, null);
lang_box = construct_media_container ();
lang_box.set_title (_("German"));
lang_box.set_id (BOX_LANGUAGE_GERMAN);
ars.callback (ars.source, ars.operation_id, lang_box, 0, null);
}
private void browse_quality (SourceArteSpec ars)
{
Grl.Media q_box = construct_media_container ();
q_box.set_title (_("Low quality (220p)"));
q_box.set_id (BOX_QUALITY_LOW);
ars.callback (ars.source, ars.operation_id, q_box, 3, null);
q_box = construct_media_container ();
q_box.set_title (_("Medium quality (400p)"));
q_box.set_id (BOX_QUALITY_MEDIUM);
ars.callback (ars.source, ars.operation_id, q_box, 2, null);
q_box = construct_media_container ();
q_box.set_title (_("High quality (400p, better encoding)"));
q_box.set_id (BOX_QUALITY_HIGH);
ars.callback (ars.source, ars.operation_id, q_box, 1, null);
q_box = construct_media_container ();
q_box.set_title (_("Best quality (720p)"));
q_box.set_id (BOX_QUALITY_HD);
ars.callback (ars.source, ars.operation_id, q_box, 0, null);
}
public override void browse (Grl.SourceBrowseSpec bs)
{
debug ("Browse streams...");
SourceArteSpec ars = SourceArteSpec () {
source = bs.source,
operation_id = bs.operation_id,
container = bs.container,
text = null,
callback = bs.callback
};
arte_browse (ars);
}
private void arte_browse (SourceArteSpec ars)
{
switch (ars.container.get_id ()) {
case null:
if (language == Language.UNKNOWN || quality == VideoQuality.UNKNOWN) {
browse_language (ars);
} else {
refresh_rss_feed (ars);
}
break;
case BOX_SETTINGS_RESET:
browse_language (ars);
break;
case BOX_LANGUAGE_FRENCH:
set_language (Language.FRENCH);
browse_quality (ars);
break;
case BOX_LANGUAGE_GERMAN:
set_language (Language.GERMAN);
browse_quality (ars);
break;
case BOX_QUALITY_LOW:
set_quality (VideoQuality.LOW);
refresh_rss_feed (ars);
break;
case BOX_QUALITY_MEDIUM:
set_quality (VideoQuality.MEDIUM);
refresh_rss_feed (ars);
break;
case BOX_QUALITY_HIGH:
set_quality (VideoQuality.HIGH);
refresh_rss_feed (ars);
break;
case BOX_QUALITY_HD:
set_quality (VideoQuality.HD);
refresh_rss_feed (ars);
break;
}
}
public override void search (Grl.SourceSearchSpec ss)
{
debug ("Searching '%s'...", ss.text);
SourceArteSpec ars = SourceArteSpec () {
source = ss.source,
operation_id = ss.operation_id,
container = null,
text = ss.text,
callback = ss.callback
};
arte_browse (ars);
debug ("Search finished");
}
private void refresh_rss_feed (SourceArteSpec ars)
{
uint parse_errors = 0;
uint network_errors = 0;
uint error_threshold = 0;
debug ("Refreshing Video Feed...");
// download and parse feeds
// try parsers one by one until enough videos are extracted
for (int i=0; i<parsers.length; i++)
{
var p = parsers[i];
p.reset ();
parse_errors = 0;
network_errors = 0;
error_threshold = p.get_error_threshold ();
// get all data chunks of a parser
while (p.has_data)
{
try {
if (ars.text != null) {
p.filter = ars.text;
}
Grl.Media media = null;
unowned GLib.SList<Video> videos = p.parse (language);
uint remaining = videos.length();
debug ("Parsed: %u\n", remaining);
// Create the settings box, if we are not in search mode
if (ars.container != null) {
media = construct_media_container ();
media.set_id (BOX_SETTINGS_RESET);
media.set_title (_("↻ Change settings"));
ars.callback (ars.source, ars.operation_id, media, remaining + 1, null);
}
foreach (Video v in videos) {
media = construct_media_video ();
media.set_title (v.title);
if (v.duration > 0) {
media.set_duration (v.duration);
}
media.set_site (v.page_url);
media.set_description(v.desc);
if (quality == VideoQuality.LOW) {
media.set_url(v.urls.get("300"));
} else if (quality == VideoQuality.MEDIUM) {
media.set_url(v.urls.get("800"));
} else if (quality == VideoQuality.HIGH) {
media.set_url(v.urls.get("1500"));
} else {
media.set_url(v.urls.get("2200"));
}
if (media.get_url () == null) {
GLib.warning ("Fallback to the old extraction method for %s.",
media.get_title ());
try {
media.set_url(get_stream_url(v.page_url, quality, language));
} catch (ExtractionError e) {
GLib.warning ("No playback url found for %s, skipping.",
media.get_title ());
remaining -= 1;
continue;
}
}
// TODO dates
media.set_thumbnail (v.image_url);
ars.callback (ars.source, ars.operation_id, media, remaining, null);
remaining -= 1;
}
ars.callback (ars.source, ars.operation_id, null, 0, null);
} catch (MarkupError e) {
parse_errors++;
GLib.critical ("XML Parse Error: %s", e.message);
} catch (IOError e) {
network_errors++;
GLib.critical ("Network problems: %s", e.message);
}
// request the next chunk of data
p.advance ();
// leave the loop if we got too many errors
if (parse_errors >= error_threshold || network_errors >= error_threshold)
break;
}
// leave the loop if we got enought videos
if (parse_errors < error_threshold && network_errors < error_threshold)
break;
}
debug ("Browse streams finished.");
}
private string get_stream_url (string page_url, VideoQuality q, Language lang)
throws ExtractionError
{
return extractor.get_url (q, lang, page_url);
}
}
public bool grl_arteplus7_plugin_init (Grl.Registry registry, Grl.Plugin plugin,
GLib.List configs)
{
GrlArteSource source = new GrlArteSource();
try {
registry.register_source (plugin, source);
} catch (GLib.Error e) {
GLib.message ("ArtePlus7 register source failed: %s", e.message);
return false;
}
GLib.message ("ArtePlus7 loaded!");
return true;
}