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