[streamable] Fix title extraction and improve (Closes #9122)
[youtube-dl] / youtube_dl / extractor / streamable.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6     ExtractorError,
7     float_or_none,
8     int_or_none,
9 )
10
11
12 class StreamableIE(InfoExtractor):
13     _VALID_URL = r'https?://streamable\.com/(?:e/)?(?P<id>\w+)'
14     _TESTS = [
15         {
16             'url': 'https://streamable.com/dnd1',
17             'md5': '3e3bc5ca088b48c2d436529b64397fef',
18             'info_dict': {
19                 'id': 'dnd1',
20                 'ext': 'mp4',
21                 'title': 'Mikel Oiarzabal scores to make it 0-3 for La Real against Espanyol',
22                 'thumbnail': 're:https?://.*\.jpg$',
23                 'uploader': 'teabaker',
24                 'timestamp': 1454964157.35115,
25                 'upload_date': '20160208',
26                 'duration': 61.516,
27                 'view_count': int,
28             }
29         },
30         # older video without bitrate, width/height, etc. info
31         {
32             'url': 'https://streamable.com/moo',
33             'md5': '2cf6923639b87fba3279ad0df3a64e73',
34             'info_dict': {
35                 'id': 'moo',
36                 'ext': 'mp4',
37                 'title': '"Please don\'t eat me!"',
38                 'thumbnail': 're:https?://.*\.jpg$',
39                 'timestamp': 1426115495,
40                 'upload_date': '20150311',
41                 'duration': 12,
42                 'view_count': int,
43             }
44         },
45         {
46             'url': 'https://streamable.com/e/dnd1',
47             'only_matching': True,
48         }
49     ]
50
51     def _real_extract(self, url):
52         video_id = self._match_id(url)
53
54         # Note: Using the ajax API, as the public Streamable API doesn't seem
55         # to return video info like the title properly sometimes, and doesn't
56         # include info like the video duration
57         video = self._download_json(
58             'https://streamable.com/ajax/videos/%s' % video_id, video_id)
59
60         # Format IDs:
61         # 0 The video is being uploaded
62         # 1 The video is being processed
63         # 2 The video has at least one file ready
64         # 3 The video is unavailable due to an error
65         status = video.get('status')
66         if status != 2:
67             raise ExtractorError(
68                 'This video is currently unavailable. It may still be uploading or processing.',
69                 expected=True)
70
71         title = video.get('reddit_title') or video['title']
72
73         formats = []
74         for key, info in video['files'].items():
75             if not info.get('url'):
76                 continue
77             formats.append({
78                 'format_id': key,
79                 'url': self._proto_relative_url(info['url']),
80                 'width': int_or_none(info.get('width')),
81                 'height': int_or_none(info.get('height')),
82                 'filesize': int_or_none(info.get('size')),
83                 'fps': int_or_none(info.get('framerate')),
84                 'vbr': float_or_none(info.get('bitrate'), 1000)
85             })
86         self._sort_formats(formats)
87
88         return {
89             'id': video_id,
90             'title': title,
91             'description': video.get('description'),
92             'thumbnail': self._proto_relative_url(video.get('thumbnail_url')),
93             'uploader': video.get('owner', {}).get('user_name'),
94             'timestamp': float_or_none(video.get('date_added')),
95             'duration': float_or_none(video.get('duration')),
96             'view_count': int_or_none(video.get('plays')),
97             'formats': formats
98         }