[gameone] Added timestamp extraction
[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 http://board.jdownloader.org/showpost.php?p=185835&postcount=31 
18 RAW_MP4_URL = 'http://cdn.riptide-mtvn.com/'
19
20 PUB_DATE_FORMAT = '%Y-%m-%d %H:%M:%S %z'
21
22 class GameOneIE(InfoExtractor):
23     _VALID_URL = r'https?://(?:www\.)?gameone\.de/tv/(?P<id>\d+)'
24     _TEST = {
25         'url': 'http://www.gameone.de/tv/288',
26         'md5': '136656b7fb4c9cb4a8e2d500651c499b',
27         'info_dict': {
28             'id': '288',
29             'ext': 'mp4',
30             'title': 'Game One - Folge 288',
31             'duration': 1238,
32             'thumbnail': 'http://s3.gameone.de/gameone/assets/video_metas/teaser_images/000/643/636/big/640x360.jpg',
33             'description': 'FIFA-Pressepokal 2014, Star Citizen, Kingdom Come: Deliverance, Project Cars, Schöner Trants Nerdquiz Folge 2 Runde 1',
34             'age_limit': 16,
35             'upload_date': '20140513',
36             'timestamp': 1399980122,
37         }
38     }
39
40     def _real_extract(self, url):
41         mobj = re.match(self._VALID_URL, url)
42         video_id = mobj.group('id')
43
44         webpage = self._download_webpage(url, video_id)
45         og_video = self._og_search_video_url(webpage, secure=False)
46         description = self._html_search_meta('description', webpage)
47         age_limit = int(self._search_regex(r'age=(\d+)', self._html_search_meta('age-de-meta-label', webpage), 'age_limit', '0'))
48         mrss_url = self._search_regex(r'mrss=([^&]+)', og_video, 'mrss')
49
50         mrss = self._download_xml(mrss_url, video_id, 'Downloading mrss')
51         title = mrss.find('.//item/title').text
52         thumbnail = mrss.find('.//item/image').get('url')
53         timestamp = parse_iso8601(mrss.find('.//pubDate').text, delimiter=' ')
54         content = mrss.find(xpath_with_ns('.//media:content', NAMESPACE_MAP))
55         content_url = content.get('url')
56
57         content = self._download_xml(content_url, video_id, 'Downloading media:content')
58         rendition_items = content.findall('.//rendition')
59         duration = int(rendition_items[0].get('duration'))
60         formats = [
61                 {
62                     'url': re.sub(r'.*/(r2)', RAW_MP4_URL + r'\1', r.find('./src').text),
63                     'width': int(r.get('width')),
64                     'height': int(r.get('height')),
65                     'tbr': int(r.get('bitrate')),
66                 }
67             for r in rendition_items
68         ]
69
70         return {
71             'id': video_id,
72             'title': title,
73             'thumbnail': thumbnail,
74             'duration': duration,
75             'formats': formats,
76             'description': description,
77             'age_limit': age_limit,
78             'timestamp': timestamp,
79         }