[ign] improve extraction and extract uploader_id
[youtube-dl] / youtube_dl / extractor / xstream.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 XstreamIE(InfoExtractor):
17     _VALID_URL = r'''(?x)
18                     (?:
19                         xstream:|
20                         https?://frontend\.xstream\.(?:dk|net)/
21                     )
22                     (?P<partner_id>[^/]+)
23                     (?:
24                         :|
25                         /feed/video/\?.*?\bid=
26                     )
27                     (?P<id>\d+)
28                     '''
29     _TESTS = [{
30         'url': 'http://frontend.xstream.dk/btno/feed/video/?platform=web&id=86588',
31         'md5': 'd7d17e3337dc80de6d3a540aefbe441b',
32         'info_dict': {
33             'id': '86588',
34             'ext': 'mov',
35             'title': 'Otto Wollertsen',
36             'description': 'Vestlendingen Otto Fredrik Wollertsen',
37             'timestamp': 1430473209,
38             'upload_date': '20150501',
39         },
40     }, {
41         'url': 'http://frontend.xstream.dk/ap/feed/video/?platform=web&id=21039',
42         'only_matching': True,
43     }]
44
45     def _real_extract(self, url):
46         mobj = re.match(self._VALID_URL, url)
47         partner_id = mobj.group('partner_id')
48         video_id = mobj.group('id')
49
50         data = self._download_xml(
51             'http://frontend.xstream.dk/%s/feed/video/?platform=web&id=%s'
52             % (partner_id, video_id),
53             video_id)
54
55         NS_MAP = {
56             'atom': 'http://www.w3.org/2005/Atom',
57             'xt': 'http://xstream.dk/',
58             'media': 'http://search.yahoo.com/mrss/',
59         }
60
61         entry = data.find(xpath_with_ns('./atom:entry', NS_MAP))
62
63         title = xpath_text(
64             entry, xpath_with_ns('./atom:title', NS_MAP), 'title')
65         description = xpath_text(
66             entry, xpath_with_ns('./atom:summary', NS_MAP), 'description')
67         timestamp = parse_iso8601(xpath_text(
68             entry, xpath_with_ns('./atom:published', NS_MAP), 'upload date'))
69
70         formats = []
71         media_group = entry.find(xpath_with_ns('./media:group', NS_MAP))
72         for media_content in media_group.findall(xpath_with_ns('./media:content', NS_MAP)):
73             media_url = media_content.get('url')
74             if not media_url:
75                 continue
76             tbr = int_or_none(media_content.get('bitrate'))
77             mobj = re.search(r'^(?P<url>rtmp://[^/]+/(?P<app>[^/]+))/(?P<playpath>.+)$', media_url)
78             if mobj:
79                 formats.append({
80                     'url': mobj.group('url'),
81                     'play_path': 'mp4:%s' % mobj.group('playpath'),
82                     'app': mobj.group('app'),
83                     'ext': 'flv',
84                     'tbr': tbr,
85                     'format_id': 'rtmp-%d' % tbr,
86                 })
87             else:
88                 formats.append({
89                     'url': media_url,
90                     'tbr': tbr,
91                 })
92         self._sort_formats(formats)
93
94         link = find_xpath_attr(
95             entry, xpath_with_ns('./atom:link', NS_MAP), 'rel', 'original')
96         if link is not None:
97             formats.append({
98                 'url': link.get('href'),
99                 'format_id': link.get('rel'),
100             })
101
102         thumbnails = [{
103             'url': splash.get('url'),
104             'width': int_or_none(splash.get('width')),
105             'height': int_or_none(splash.get('height')),
106         } for splash in media_group.findall(xpath_with_ns('./xt:splash', NS_MAP))]
107
108         return {
109             'id': video_id,
110             'title': title,
111             'description': description,
112             'timestamp': timestamp,
113             'formats': formats,
114             'thumbnails': thumbnails,
115         }