~ubuntu-branches/ubuntu/trusty/totem-plugin-arte/trusty-proposed

« back to all changes in this revision

Viewing changes to arteparser.vala

  • Committer: Package Import Robot
  • Author(s): Nicolas Delvaux
  • Date: 2011-10-23 13:55:28 UTC
  • mfrom: (3.1.5 experimental)
  • Revision ID: package-import@ubuntu.com-20111023135528-l9lso2mnile0dk8u
Tags: 3.0.0-2
* Upload to unstable
* Fix FTBS by switching to valac-0.14 and libpeas 1.1 (bump dependencies)
* Update the user-agent string
* Rework the "disable gsettings schema compilation" patch (use upstream patch)
* Packaging:
  - Tweak the long description (some videos are playable everywhere)
  - Remove VCS links in Control (there is no VCS for this package)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Totem Arte Plugin allows you to watch streams from arte.tv
 
3
 * Copyright (C) 2009, 2010, 2011 Simon Wenner <simon@wenner.ch>
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
 
18
 *
 
19
 * The Totem Arte Plugin project hereby grants permission for non-GPL compatible
 
20
 * GStreamer plugins to be used and distributed together with GStreamer, Totem
 
21
 * and Totem Arte Plugin. This permission is above and beyond the permissions
 
22
 * granted by the GPL license by which Totem Arte Plugin is covered.
 
23
 * If you modify this code, you may extend this exception to your version of the
 
24
 * code, but you are not obligated to do so. If you do not wish to do so,
 
25
 * delete this exception statement from your version.
 
26
 *
 
27
 */
 
28
 
 
29
using GLib;
 
30
using Soup;
 
31
 
 
32
public abstract class ArteParser : GLib.Object
 
33
{
 
34
    public bool has_data { get; protected set; default = false; }
 
35
    protected string xml_fr;
 
36
    protected string xml_de;
 
37
    protected GLib.SList<Video> videos;
 
38
 
 
39
    private const MarkupParser parser = {
 
40
            open_tag,
 
41
            close_tag,
 
42
            process_text,
 
43
            null,
 
44
            null
 
45
        };
 
46
 
 
47
    public ArteParser () {}
 
48
    public virtual void reset () {}
 
49
    public virtual bool has_duplicates () { return false; }
 
50
    public virtual bool has_image_urls () { return true; }
 
51
    public virtual uint get_error_threshold () { return 0; }
 
52
 
 
53
    public virtual bool advance ()
 
54
    {
 
55
        return has_data;
 
56
    }
 
57
 
 
58
    public unowned GLib.SList<Video> parse (Language lang) throws MarkupError, IOError
 
59
    {
 
60
        videos = new GLib.SList<Video> ();
 
61
 
 
62
        if(!has_data) {
 
63
            return videos;
 
64
        }
 
65
 
 
66
        Soup.Message msg;
 
67
        if (lang == Language.GERMAN) {
 
68
            msg = new Soup.Message ("GET", xml_de);
 
69
        } else {
 
70
            msg = new Soup.Message ("GET", xml_fr);
 
71
        }
 
72
 
 
73
        Soup.SessionAsync session = create_session ();
 
74
 
 
75
        session.send_message (msg);
 
76
 
 
77
        if (msg.status_code != Soup.KnownStatusCode.OK) {
 
78
            throw new IOError.HOST_NOT_FOUND ("plus7.arte.tv could not be accessed.");
 
79
        }
 
80
 
 
81
        var context = new MarkupParseContext (parser,
 
82
                MarkupParseFlags.TREAT_CDATA_AS_TEXT, this, null);
 
83
        context.parse ((string) msg.response_body.flatten ().data,
 
84
                (ssize_t) msg.response_body.length);
 
85
        context.end_parse ();
 
86
 
 
87
        return videos;
 
88
    }
 
89
 
 
90
    protected virtual void open_tag (MarkupParseContext ctx,
 
91
            string elem,
 
92
            string[] attribute_names,
 
93
            string[] attribute_values) throws MarkupError {}
 
94
 
 
95
    protected virtual void close_tag (MarkupParseContext ctx,
 
96
            string elem) throws MarkupError {}
 
97
 
 
98
    protected virtual void process_text (MarkupParseContext ctx,
 
99
            string text,
 
100
            size_t text_len) throws MarkupError {}
 
101
 
 
102
    protected string sanitise_markup(string str)
 
103
    {
 
104
        return str.replace("&", "&amp;");
 
105
    }
 
106
}
 
107
 
 
108
public class ArteRSSParser : ArteParser
 
109
{
 
110
    private Video current_video = null;
 
111
    private string current_data = null;
 
112
    /* official RSS feeds by topic, contains duplicats, no image urls and offline dates */
 
113
    private const string[] feeds_fr = {
 
114
        "http://videos.arte.tv/fr/do_delegate/videos/toutes_les_videos/actualites/index-3188636,view,rss.xml",
 
115
        "http://videos.arte.tv/fr/do_delegate/videos/toutes_les_videos/documentaire/index-3188646,view,rss.xml",
 
116
        "http://videos.arte.tv/fr/do_delegate/videos/decouverte/index-3188644,view,rss.xml",
 
117
        "http://videos.arte.tv/fr/do_delegate/videos/toutes_les_videos/europe/index-3188648,view,rss.xml",
 
118
        "http://videos.arte.tv/fr/do_delegate/videos/toutes_les_videos/geopolitique_histoire/index-3188654,view,rss.xml",
 
119
        "http://videos.arte.tv/fr/do_delegate/videos/toutes_les_videos/societe/index-3188652,view,rss.xml",
 
120
        "http://videos.arte.tv/fr/do_delegate/videos/toutes_les_videos/junior/index-3188656,view,rss.xml",
 
121
        "http://videos.arte.tv/fr/do_delegate/videos/toutes_les_videos/cinema_fiction/index-3188642,view,rss.xml",
 
122
        "http://videos.arte.tv/fr/do_delegate/videos/toutes_les_videos/arts_cultures_spectacles/index-3188640,view,rss.xml",
 
123
        "http://videos.arte.tv/fr/do_delegate/videos/toutes_les_videos/culture_pop_alternative/index-3188638,view,rss.xml",
 
124
        "http://videos.arte.tv/fr/do_delegate/videos/toutes_les_videos/environnement_science/index-3188650,view,rss.xml"
 
125
    };
 
126
    private const string[] feeds_de = {
 
127
        "http://videos.arte.tv/de/do_delegate/videos/alle_videos/aktuelles/index-3188636,view,rss.xml",
 
128
        "http://videos.arte.tv/de/do_delegate/videos/alle_videos/dokus/index-3188646,view,rss.xml",
 
129
        "http://videos.arte.tv/de/do_delegate/videos/entdeckung/index-3188644,view,rss.xml",
 
130
        "http://videos.arte.tv/de/do_delegate/videos/alle_videos/europa/index-3188648,view,rss.xml",
 
131
        "http://videos.arte.tv/de/do_delegate/videos/alle_videos/geopolitik_geschichte/index-3188654,view,rss.xml",
 
132
        "http://videos.arte.tv/de/do_delegate/videos/alle_videos/gesellschaft/index-3188652,view,rss.xml",
 
133
        "http://videos.arte.tv/de/do_delegate/videos/alle_videos/junior/index-3188656,view,rss.xml",
 
134
        "http://videos.arte.tv/de/do_delegate/videos/alle_videos/kino_serien/index-3188642,view,rss.xml",
 
135
        "http://videos.arte.tv/de/do_delegate/videos/alle_videos/kunst_kultur/index-3188640,view,rss.xml",
 
136
        "http://videos.arte.tv/de/do_delegate/videos/alle_videos/popkultur_musik/index-3188638,view,rss.xml",
 
137
        "http://videos.arte.tv/de/do_delegate/videos/alle_videos/umwelt_wissenschaft/index-3188650,view,rss.xml"
 
138
    };
 
139
    private const uint feed_count = 11;
 
140
    private uint feed_idx = 0;
 
141
 
 
142
    public ArteRSSParser ()
 
143
    {
 
144
        xml_fr = feeds_fr[0];
 
145
        xml_de = feeds_de[0];
 
146
 
 
147
        reset ();
 
148
    }
 
149
 
 
150
    public override void reset ()
 
151
    {
 
152
        has_data = true;
 
153
        feed_idx = 0;
 
154
    }
 
155
 
 
156
    public override bool has_duplicates () { return true; }
 
157
    public override bool has_image_urls () { return false; }
 
158
    public override uint get_error_threshold ()
 
159
    {
 
160
        return (uint)(feed_count * 0.5);
 
161
    }
 
162
 
 
163
    public override bool advance ()
 
164
    {
 
165
        feed_idx++;
 
166
        has_data = feed_idx < feed_count;
 
167
        if(has_data)
 
168
            set_feed(feed_idx);
 
169
 
 
170
        return has_data;
 
171
    }
 
172
 
 
173
    private void set_feed (uint idx)
 
174
    {
 
175
        xml_de = feeds_de[idx];
 
176
        xml_fr = feeds_fr[idx];
 
177
        feed_idx = idx;
 
178
    }
 
179
 
 
180
    protected override void open_tag (MarkupParseContext ctx,
 
181
            string elem,
 
182
            string[] attribute_names,
 
183
            string[] attribute_values) throws MarkupError
 
184
    {
 
185
        switch (elem) {
 
186
            case "item":
 
187
                current_video = new Video();
 
188
                break;
 
189
            default:
 
190
                current_data = elem;
 
191
                break;
 
192
        }
 
193
    }
 
194
 
 
195
    protected override void close_tag (MarkupParseContext ctx,
 
196
            string elem) throws MarkupError
 
197
    {
 
198
        switch (elem) {
 
199
            case "item":
 
200
                if (current_video != null) {
 
201
                    videos.append (current_video);
 
202
                    current_video = null;
 
203
                }
 
204
                break;
 
205
            default:
 
206
                current_data = null;
 
207
                break;
 
208
        }
 
209
    }
 
210
 
 
211
    protected override void process_text (MarkupParseContext ctx,
 
212
            string text,
 
213
            size_t text_len) throws MarkupError
 
214
    {
 
215
        if (current_video != null) {
 
216
            switch (current_data) {
 
217
                case "title":
 
218
                    current_video.title = text;
 
219
                    break;
 
220
                case "link":
 
221
                    current_video.page_url = text;
 
222
                    break;
 
223
                case "description":
 
224
                    current_video.desc = sanitise_markup(text);
 
225
                    break;
 
226
                case "pubDate":
 
227
                    current_video.publication_date.from_iso8601 (text);
 
228
                    break;
 
229
            }
 
230
        }
 
231
    }
 
232
}
 
233
 
 
234
public class ArteXMLParser : ArteParser
 
235
{
 
236
    private Video current_video = null;
 
237
    private string current_data = null;
 
238
    private uint page = 1;
 
239
    /* number of video feed pages available */
 
240
    private uint page_limit = 14;
 
241
    /* Parses the XML feed of the Flash video wall */
 
242
    private const string xml_tmpl =
 
243
        "http://videos.arte.tv/%s/do_delegate/videos/index-3188698,view,asXml.xml?hash=%s////%u/10/";
 
244
 
 
245
    public ArteXMLParser ()
 
246
    {
 
247
        reset ();
 
248
    }
 
249
 
 
250
    public override void reset ()
 
251
    {
 
252
        set_page (1);
 
253
        has_data = true;
 
254
    }
 
255
 
 
256
    public override uint get_error_threshold ()
 
257
    {
 
258
        return (uint)(page_limit * 0.5);
 
259
    }
 
260
 
 
261
    public override bool advance ()
 
262
    {
 
263
        page++;
 
264
        has_data = page <= page_limit;
 
265
        if(has_data) {
 
266
            set_page (page);
 
267
        }
 
268
        return has_data;
 
269
    }
 
270
 
 
271
    private void set_page (uint page)
 
272
    {
 
273
        this.page = page;
 
274
        xml_fr = xml_tmpl.printf ("fr", "fr", page);
 
275
        xml_de = xml_tmpl.printf ("de", "de", page);
 
276
    }
 
277
 
 
278
    protected override void open_tag (MarkupParseContext ctx,
 
279
            string elem,
 
280
            string[] attribute_names,
 
281
            string[] attribute_values) throws MarkupError
 
282
    {
 
283
        switch (elem) {
 
284
            case "video":
 
285
                current_video = new Video();
 
286
                break;
 
287
             case "videowall":
 
288
                for (int i = 0; i < attribute_names.length ; i++) {
 
289
                    if (attribute_names[i] == "pageMax") {
 
290
                        page_limit = (uint) long.parse (attribute_values[i]);
 
291
                    }
 
292
                }
 
293
                break;
 
294
            default:
 
295
                current_data = elem;
 
296
                break;
 
297
        }
 
298
    }
 
299
 
 
300
    protected override void close_tag (MarkupParseContext ctx,
 
301
            string elem) throws MarkupError
 
302
    {
 
303
        switch (elem) {
 
304
            case "video":
 
305
                if (current_video != null) {
 
306
                    videos.prepend (current_video);
 
307
                    current_video = null;
 
308
                }
 
309
                break;
 
310
            default:
 
311
                current_data = null;
 
312
                break;
 
313
        }
 
314
    }
 
315
 
 
316
    protected override void process_text (MarkupParseContext ctx,
 
317
            string text,
 
318
            size_t text_len) throws MarkupError
 
319
    {
 
320
        if (current_video != null) {
 
321
            switch (current_data) {
 
322
                case "title":
 
323
                    current_video.title = text;
 
324
                    break;
 
325
                case "targetUrl":
 
326
                    current_video.page_url = "http://videos.arte.tv" + text;
 
327
                    break;
 
328
                case "imageUrl":
 
329
                    current_video.image_url = "http://videos.arte.tv" + text;
 
330
                    break;
 
331
                case "teaserText":
 
332
                    current_video.desc = sanitise_markup(text);
 
333
                    break;
 
334
                case "startDate":
 
335
                    current_video.publication_date.from_iso8601 (text);
 
336
                    break;
 
337
                case "endDate":
 
338
                    current_video.offline_date.from_iso8601 (text);
 
339
                    break;
 
340
            }
 
341
        }
 
342
    }
 
343
}