[gameone] This fix resolves issue #4552
[youtube-dl] / youtube_dl / extractor / gameone.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     xpath_with_ns,
9     parse_iso8601
10 )
11
12 NAMESPACE_MAP = {
13     'media': 'http://search.yahoo.com/mrss/',
14 }
15
16 # URL prefix to download the mp4 files directly instead of streaming via rtmp
17 # Credits go to XBox-Maniac
18 # http://board.jdownloader.org/showpost.php?p=185835&postcount=31
19 RAW_MP4_URL = 'http://cdn.riptide-mtvn.com/'
20
21
22 class GameOneIE(InfoExtractor):
23     _VALID_URL = r'https?://(?:www\.)?gameone\.de/tv/(?P<id>\d+)'
24     _TESTS = [
25         {
26             'url': 'http://www.gameone.de/tv/288',
27             'md5': '136656b7fb4c9cb4a8e2d500651c499b',
28             'info_dict': {
29                 'id': '288',
30                 'ext': 'mp4',
31                 'title': 'Game One - Folge 288',
32                 'duration': 1238,
33                 'thumbnail': 'http://s3.gameone.de/gameone/assets/video_metas/teaser_images/000/643/636/big/640x360.jpg',
34                 'description': 'FIFA-Pressepokal 2014, Star Citizen, Kingdom Come: Deliverance, Project Cars, Schöner Trants Nerdquiz Folge 2 Runde 1',
35                 'age_limit': 16,
36                 'upload_date': '20140513',
37                 'timestamp': 1399980122,
38             }
39         },
40         {
41             'url': 'http://gameone.de/tv/220',
42             'md5': '5227ca74c4ae6b5f74c0510a7c48839e',
43             'info_dict': {
44                 'id': '220',
45                 'ext': 'mp4',
46                 'upload_date': '20120918',
47                 'description': 'Jet Set Radio HD, Tekken Tag Tournament 2, Source Filmmaker',
48                 'timestamp': 1347971451,
49                 'title': 'Game One - Folge 220',
50                 'duration': 896,
51                 'age_limit': 16,
52             }
53         }
54
55     ]
56
57     def _real_extract(self, url):
58         mobj = re.match(self._VALID_URL, url)
59         video_id = mobj.group('id')
60
61         webpage = self._download_webpage(url, video_id)
62         og_video = self._og_search_video_url(webpage, secure=False)
63         description = self._html_search_meta('description', webpage)
64         age_limit = int(
65             self._search_regex(
66                 r'age=(\d+)',
67                 self._html_search_meta(
68                     'age-de-meta-label',
69                     webpage),
70                 'age_limit',
71                 '0'))
72         mrss_url = self._search_regex(r'mrss=([^&]+)', og_video, 'mrss')
73
74         mrss = self._download_xml(mrss_url, video_id, 'Downloading mrss')
75         title = mrss.find('.//item/title').text
76         thumbnail = mrss.find('.//item/image').get('url')
77         timestamp = parse_iso8601(mrss.find('.//pubDate').text, delimiter=' ')
78         content = mrss.find(xpath_with_ns('.//media:content', NAMESPACE_MAP))
79         content_url = content.get('url')
80
81         content = self._download_xml(
82             content_url,
83             video_id,
84             'Downloading media:content')
85         rendition_items = content.findall('.//rendition')
86         duration = int(rendition_items[0].get('duration').split('.')[0])
87         formats = [
88             {
89                 'url': re.sub(r'.*/(r2)', RAW_MP4_URL + r'\1', r.find('./src').text),
90                 'width': int(r.get('width')),
91                 'height': int(r.get('height')),
92                 'tbr': int(r.get('bitrate')),
93             }
94             for r in rendition_items
95         ]
96         self._sort_formats(formats)
97
98         return {
99             'id': video_id,
100             'title': title,
101             'thumbnail': thumbnail,
102             'duration': duration,
103             'formats': formats,
104             'description': description,
105             'age_limit': age_limit,
106             'timestamp': timestamp,
107         }
108
109
110 class GameOnePlaylistIE(InfoExtractor):
111     _VALID_URL = r'https?://(?:www\.)?gameone\.de(?:/tv)?/?$'
112     IE_NAME = 'gameone:playlist'
113     _TEST = {
114         'url': 'http://www.gameone.de/tv',
115         'info_dict': {
116             'title': 'GameOne',
117         },
118         'playlist_mincount': 294,
119     }
120
121     def _real_extract(self, url):
122         webpage = self._download_webpage('http://www.gameone.de/tv', 'TV')
123         max_id = max(map(int, re.findall(r'<a href="/tv/(\d+)"', webpage)))
124         entries = [
125             self.url_result('http://www.gameone.de/tv/%d' %
126                             video_id, 'GameOne')
127             for video_id in range(max_id, 0, -1)]
128
129         return {
130             '_type': 'playlist',
131             'title': 'GameOne',
132             'entries': entries,
133         }