[gameone] add playlist capability
[youtube-dl] / youtube_dl / extractor / gameone.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import datetime
5 import re
6
7 from .common import InfoExtractor
8 from ..utils import (
9     xpath_with_ns,
10     parse_iso8601
11 )
12
13 NAMESPACE_MAP = {
14     'media': 'http://search.yahoo.com/mrss/',
15 }
16
17 # URL prefix to download the mp4 files directly instead of streaming via rtmp
18 # Credits go to XBox-Maniac
19 # http://board.jdownloader.org/showpost.php?p=185835&postcount=31
20 RAW_MP4_URL = 'http://cdn.riptide-mtvn.com/'
21
22
23 class GameOneIE(InfoExtractor):
24     _VALID_URL = r'https?://(?:www\.)?gameone\.de/tv/(?P<id>\d+)'
25     _TEST = {
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     def _real_extract(self, url):
42         mobj = re.match(self._VALID_URL, url)
43         video_id = mobj.group('id')
44
45         webpage = self._download_webpage(url, video_id)
46         og_video = self._og_search_video_url(webpage, secure=False)
47         description = self._html_search_meta('description', webpage)
48         age_limit = int(
49             self._search_regex(
50                 r'age=(\d+)',
51                 self._html_search_meta(
52                     'age-de-meta-label',
53                     webpage),
54                 'age_limit',
55                 '0'))
56         mrss_url = self._search_regex(r'mrss=([^&]+)', og_video, 'mrss')
57
58         mrss = self._download_xml(mrss_url, video_id, 'Downloading mrss')
59         title = mrss.find('.//item/title').text
60         thumbnail = mrss.find('.//item/image').get('url')
61         timestamp = parse_iso8601(mrss.find('.//pubDate').text, delimiter=' ')
62         content = mrss.find(xpath_with_ns('.//media:content', NAMESPACE_MAP))
63         content_url = content.get('url')
64
65         content = self._download_xml(
66             content_url,
67             video_id,
68             'Downloading media:content')
69         rendition_items = content.findall('.//rendition')
70         duration = int(rendition_items[0].get('duration'))
71         formats = [
72             {
73                 'url': re.sub(r'.*/(r2)', RAW_MP4_URL + r'\1', r.find('./src').text),
74                 'width': int(r.get('width')),
75                 'height': int(r.get('height')),
76                 'tbr': int(r.get('bitrate')),
77             }
78             for r in rendition_items
79         ]
80         self._sort_formats(formats)
81
82         return {
83             'id': video_id,
84             'title': title,
85             'thumbnail': thumbnail,
86             'duration': duration,
87             'formats': formats,
88             'description': description,
89             'age_limit': age_limit,
90             'timestamp': timestamp,
91         }
92
93 class GameOnePlaylistIE(InfoExtractor):
94     _VALID_URL = r'https?://(?:www\.)?gameone\.de(?:/tv)?/?$'
95
96     def _real_extract(self, url):
97         this_year = datetime.date.today().year
98         webpage = self._download_webpage('http://www.gameone.de/tv/year/%d' % this_year, this_year)
99         max_id = max(map(int, re.findall(r'<a href="/tv/(\d+)"', webpage)))
100         entries = [self.url_result('http://www.gameone.de/tv/%d' % video_id, 'GameOne') for video_id in range(max_id, 0, -1)]
101
102         return {
103             '_type': 'playlist',
104             'title': 'GameOne',
105             'entries': entries,
106         }