Merge branch 'douyutv' of https://github.com/bonfy/youtube-dl into bonfy-douyutv
[youtube-dl] / youtube_dl / extractor / aftenposten.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     int_or_none,
9     parse_iso8601,
10     xpath_with_ns,
11     xpath_text,
12     find_xpath_attr,
13 )
14
15
16 class AftenpostenIE(InfoExtractor):
17     _VALID_URL = r'https?://(?:www\.)?aftenposten\.no/webtv/(?:#!/)?video/(?P<id>\d+)'
18
19     _TEST = {
20         'url': 'http://www.aftenposten.no/webtv/#!/video/21039/trailer-sweatshop-i-can-t-take-any-more',
21         'md5': 'fd828cd29774a729bf4d4425fe192972',
22         'info_dict': {
23             'id': '21039',
24             'ext': 'mov',
25             'title': 'TRAILER: "Sweatshop" - I canĀ“t take any more',
26             'description': 'md5:21891f2b0dd7ec2f78d84a50e54f8238',
27             'timestamp': 1416927969,
28             'upload_date': '20141125',
29         }
30     }
31
32     def _real_extract(self, url):
33         video_id = self._match_id(url)
34
35         data = self._download_xml(
36             'http://frontend.xstream.dk/ap/feed/video/?platform=web&id=%s' % video_id, video_id)
37
38         NS_MAP = {
39             'atom': 'http://www.w3.org/2005/Atom',
40             'xt': 'http://xstream.dk/',
41             'media': 'http://search.yahoo.com/mrss/',
42         }
43
44         entry = data.find(xpath_with_ns('./atom:entry', NS_MAP))
45
46         title = xpath_text(
47             entry, xpath_with_ns('./atom:title', NS_MAP), 'title')
48         description = xpath_text(
49             entry, xpath_with_ns('./atom:summary', NS_MAP), 'description')
50         timestamp = parse_iso8601(xpath_text(
51             entry, xpath_with_ns('./atom:published', NS_MAP), 'upload date'))
52
53         formats = []
54         media_group = entry.find(xpath_with_ns('./media:group', NS_MAP))
55         for media_content in media_group.findall(xpath_with_ns('./media:content', NS_MAP)):
56             media_url = media_content.get('url')
57             if not media_url:
58                 continue
59             tbr = int_or_none(media_content.get('bitrate'))
60             mobj = re.search(r'^(?P<url>rtmp://[^/]+/(?P<app>[^/]+))/(?P<playpath>.+)$', media_url)
61             if mobj:
62                 formats.append({
63                     'url': mobj.group('url'),
64                     'play_path': 'mp4:%s' % mobj.group('playpath'),
65                     'app': mobj.group('app'),
66                     'ext': 'flv',
67                     'tbr': tbr,
68                     'format_id': 'rtmp-%d' % tbr,
69                 })
70             else:
71                 formats.append({
72                     'url': media_url,
73                     'tbr': tbr,
74                 })
75         self._sort_formats(formats)
76
77         link = find_xpath_attr(
78             entry, xpath_with_ns('./atom:link', NS_MAP), 'rel', 'original')
79         if link is not None:
80             formats.append({
81                 'url': link.get('href'),
82                 'format_id': link.get('rel'),
83             })
84
85         thumbnails = [{
86             'url': splash.get('url'),
87             'width': int_or_none(splash.get('width')),
88             'height': int_or_none(splash.get('height')),
89         } for splash in media_group.findall(xpath_with_ns('./xt:splash', NS_MAP))]
90
91         return {
92             'id': video_id,
93             'title': title,
94             'description': description,
95             'timestamp': timestamp,
96             'formats': formats,
97             'thumbnails': thumbnails,
98         }