Merge remote-tracking branch 'adammw/tenplay'
[youtube-dl] / youtube_dl / extractor / tenplay.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7
8 class TenPlayIE(InfoExtractor):
9     _VALID_URL = r'https?://(?:www\.)?ten(play)?\.com\.au/.+'
10     _TEST = {
11         'url': 'http://tenplay.com.au/ten-insider/extra/season-2013/tenplay-tv-your-way',
12         'md5': 'c9dda6aac8f814352ad2aee8899b1612',
13         'info_dict': {
14             'id': '2695695426001',
15             'ext': 'flv',
16             'title': 'TENplay: TV your way',
17             'description': 'Welcome to a new TV experience. Enjoy a taste of the TENplay benefits.',
18             'timestamp': 1380150606.889,
19             'upload_date': '20130925',
20             'uploader': 'TENplay'
21         }
22     }
23
24     _video_fields = ["id","name","shortDescription","longDescription","creationDate","publishedDate","lastModifiedDate","customFields","videoStillURL","thumbnailURL","referenceId","length","playsTotal","playsTrailingWeek","renditions","captioning","startDate","endDate"]
25
26     def _real_extract(self, url):
27         webpage = self._download_webpage(url, url)
28         video_id = self._html_search_regex(r'videoID: "(\d+?)"', webpage, 'video_id')
29         api_token = self._html_search_regex(r'apiToken: "([a-zA-Z0-9-_\.]+?)"', webpage, 'api_token')
30         title = self._html_search_regex(r'<meta property="og:title" content="\s*(.*?)\s*"\s*/?\s*>', webpage, 'title')
31
32         json = self._download_json('https://api.brightcove.com/services/library?command=find_video_by_id&video_id=%s&token=%s&video_fields=%s' % (video_id, api_token, ','.join(self._video_fields)), title)
33
34         formats = []
35         for rendition in json['renditions']:
36             url = rendition['remoteUrl'] or rendition['url']
37             protocol = 'rtmp' if url.startswith('rtmp') else 'http'
38             ext = 'flv' if protocol == 'rtmp' else rendition['videoContainer'].lower()
39
40             if protocol == 'rtmp':
41                 url = url.replace('&mp4:', '')
42
43             formats.append({
44                 'format_id': '_'.join(['rtmp', rendition['videoContainer'].lower(), rendition['videoCodec'].lower()]),
45                 'width': rendition['frameWidth'],
46                 'height': rendition['frameHeight'],
47                 'tbr': rendition['encodingRate'] / 1024,
48                 'filesize': rendition['size'],
49                 'protocol': protocol,
50                 'ext': ext,
51                 'vcodec': rendition['videoCodec'].lower(),
52                 'container': rendition['videoContainer'].lower(),
53                 'url': url
54                 })
55
56         return {
57             'id': video_id,
58             'display_id': json['referenceId'],
59             'title': json['name'],
60             'description': json['shortDescription'] or json['longDescription'],
61             'formats': formats,
62             'thumbnails': [{
63                 'url': json['videoStillURL']
64             }, {
65                 'url': json['thumbnailURL']
66             }],
67             'thumbnail': json['videoStillURL'],
68             'duration': json['length'] / 1000,
69             'timestamp': float(json['creationDate']) / 1000,
70             'uploader': json['customFields']['production_company_distributor'] if 'production_company_distributor' in json['customFields'] else 'TENplay',
71             'view_count': json['playsTotal']
72         }