[gameone] Added extraction of age_limit
[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': 'FIFA-Pressepokal 2014, Star Citizen, Kingdom Come: Deliverance, Project Cars, Schöner Trants Nerdquiz Folge 2 Runde 1',
30             'age_limit': 16
31         }
32     }
33
34     def _real_extract(self, url):
35         mobj = re.match(self._VALID_URL, url)
36         video_id = mobj.group('id')
37
38         webpage = self._download_webpage(url, video_id)
39         og_video = self._og_search_video_url(webpage, secure=False)
40         description = self._html_search_meta('description', webpage)
41         age_limit = int(self._search_regex(r'age=(\d+)', self._html_search_meta('age-de-meta-label', webpage), 'age_limit', '0'))
42         mrss_url = self._search_regex(r'mrss=([^&]+)', og_video, 'mrss')
43
44         mrss = self._download_xml(mrss_url, video_id, 'Downloading mrss')
45         title = mrss.find('.//item/title').text
46         thumbnail = mrss.find('.//item/image').get('url')
47         content = mrss.find(xpath_with_ns('.//media:content', NAMESPACE_MAP))
48         content_url = content.get('url')
49
50         content = self._download_xml(content_url, video_id, 'Downloading media:content')
51         rendition_items = content.findall('.//rendition')
52         duration = int(rendition_items[0].get('duration'))
53         formats = [
54                 {
55                     'url': re.sub(r'.*/(r2)', RAW_MP4_URL + r'\1', r.find('./src').text),
56                     'width': int(r.get('width')),
57                     'height': int(r.get('height')),
58                     'tbr': int(r.get('bitrate')),
59                 }
60             for r in rendition_items
61         ]
62
63         return {
64             'id': video_id,
65             'title': title,
66             'thumbnail': thumbnail,
67             'duration': duration,
68             'formats': formats,
69             'description': description,
70             'age_limit': age_limit,
71         }