[gameone] Added extraction of description and fixed failing tests
[youtube-dl] / youtube_dl / extractor / gameone.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 import xml.etree.ElementTree as ET
6
7 from .common import InfoExtractor
8 from ..utils import xpath_with_ns
9
10 NAMESPACE_MAP = {
11     'media': 'http://search.yahoo.com/mrss/',
12 }
13
14 # URL prefix to download the mp4 files directly instead of streaming via rtmp
15 # Credits go to XBox-Maniac http://board.jdownloader.org/showpost.php?p=185835&postcount=31 
16 RAW_MP4_URL = 'http://cdn.riptide-mtvn.com/'
17
18 class GameOneIE(InfoExtractor):
19     _VALID_URL = r'https?://(?:www\.)?gameone\.de/tv/(?P<id>\d+)'
20     _TEST = {
21         'url': 'http://www.gameone.de/tv/288',
22         'md5': '136656b7fb4c9cb4a8e2d500651c499b',
23         'info_dict': {
24             'id': '288',
25             'ext': 'mp4',
26             'title': 'Game One - Folge 288',
27             'duration': 1238,
28             'thumbnail': 'http://s3.gameone.de/gameone/assets/video_metas/teaser_images/000/643/636/big/640x360.jpg',
29             'description': 'Puh, das ist ja wieder eine volle Packung! Erst begleiten wir Nils zum '
30                 'FIFA-Pressepokal 2014, den er nach 2010 nun zum zweiten Mal gewinnen will.\n'
31                 'Danach gibt’s eine Vorschau auf die drei kommenden Hits “Star Citizen”, “Kingdom Come: Deliverance” und “Project Cars”.\n'
32                 'Und dann geht’s auch schon weiter mit der nächsten Folge vom Nerdquiz! Der schöne Trant foltert seine Kandidaten wieder '
33                 'mit fiesen Fragen. Hier gibt’s die erste Hälfte, in Folge 289 geht’s weiter.'
34         }
35     }
36
37     def _real_extract(self, url):
38         mobj = re.match(self._VALID_URL, url)
39         video_id = mobj.group('id')
40
41         webpage = self._download_webpage(url, video_id)
42         og_video = self._og_search_video_url(webpage, secure=False)
43         mrss_url = self._search_regex(r'mrss=([^&]+)', og_video, 'mrss')
44
45         mrss = self._download_xml(mrss_url, video_id, 'Downloading mrss')
46         title = mrss.find('.//item/title').text
47         thumbnail = mrss.find('.//item/image').get('url')
48         description = self._extract_description(mrss)
49         content = mrss.find(xpath_with_ns('.//media:content', NAMESPACE_MAP))
50         content_url = content.get('url')
51
52         content = self._download_xml(content_url, video_id, 'Downloading media:content')
53         rendition_items = content.findall('.//rendition')
54         duration = int(rendition_items[0].get('duration'))
55         formats = [
56                 {
57                     'url': re.sub(r'.*/(r2)', RAW_MP4_URL + r'\1', r.find('./src').text),
58                     'width': int(r.get('width')),
59                     'height': int(r.get('height')),
60                     'tbr': int(r.get('bitrate')),
61                 }
62             for r in rendition_items
63         ]
64
65         return {
66             'id': video_id,
67             'title': title,
68             'thumbnail': thumbnail,
69             'duration': duration,
70             'formats': formats,
71             'description': description,
72         }
73
74     def _extract_description(self, mrss):
75         description = mrss.find('.//item/description')
76         return u''.join(t for t in description.itertext())