]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/jwplatform.py
[vk] Improve view count extraction
[youtube-dl] / youtube_dl / extractor / jwplatform.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import compat_urlparse
8 from ..utils import (
9     determine_ext,
10     float_or_none,
11     int_or_none,
12     js_to_json,
13     mimetype2ext,
14 )
15
16
17 class JWPlatformBaseIE(InfoExtractor):
18     @staticmethod
19     def _find_jwplayer_data(webpage):
20         # TODO: Merge this with JWPlayer-related codes in generic.py
21
22         mobj = re.search(
23             r'jwplayer\((?P<quote>[\'"])[^\'" ]+(?P=quote)\)\.setup\s*\((?P<options>[^)]+)\)',
24             webpage)
25         if mobj:
26             return mobj.group('options')
27
28     def _extract_jwplayer_data(self, webpage, video_id, *args, **kwargs):
29         jwplayer_data = self._parse_json(
30             self._find_jwplayer_data(webpage), video_id,
31             transform_source=js_to_json)
32         return self._parse_jwplayer_data(
33             jwplayer_data, video_id, *args, **kwargs)
34
35     def _parse_jwplayer_data(self, jwplayer_data, video_id=None, require_title=True, m3u8_id=None, rtmp_params=None, base_url=None):
36         # JWPlayer backward compatibility: flattened playlists
37         # https://github.com/jwplayer/jwplayer/blob/v7.4.3/src/js/api/config.js#L81-L96
38         if 'playlist' not in jwplayer_data:
39             jwplayer_data = {'playlist': [jwplayer_data]}
40
41         entries = []
42         for video_data in jwplayer_data['playlist']:
43             # JWPlayer backward compatibility: flattened sources
44             # https://github.com/jwplayer/jwplayer/blob/v7.4.3/src/js/playlist/item.js#L29-L35
45             if 'sources' not in video_data:
46                 video_data['sources'] = [video_data]
47
48             this_video_id = video_id or video_data['mediaid']
49
50             formats = []
51             for source in video_data['sources']:
52                 source_url = self._proto_relative_url(source['file'])
53                 if base_url:
54                     source_url = compat_urlparse.urljoin(base_url, source_url)
55                 source_type = source.get('type') or ''
56                 ext = mimetype2ext(source_type) or determine_ext(source_url)
57                 if source_type == 'hls' or ext == 'm3u8':
58                     formats.extend(self._extract_m3u8_formats(
59                         source_url, this_video_id, 'mp4', 'm3u8_native', m3u8_id=m3u8_id, fatal=False))
60                 # https://github.com/jwplayer/jwplayer/blob/master/src/js/providers/default.js#L67
61                 elif source_type.startswith('audio') or ext in ('oga', 'aac', 'mp3', 'mpeg', 'vorbis'):
62                     formats.append({
63                         'url': source_url,
64                         'vcodec': 'none',
65                         'ext': ext,
66                     })
67                 else:
68                     height = int_or_none(source.get('height'))
69                     if height is None:
70                         # Often no height is provided but there is a label in
71                         # format like 1080p.
72                         height = int_or_none(self._search_regex(
73                             r'^(\d{3,})[pP]$', source.get('label') or '',
74                             'height', default=None))
75                     a_format = {
76                         'url': source_url,
77                         'width': int_or_none(source.get('width')),
78                         'height': height,
79                         'ext': ext,
80                     }
81                     if source_url.startswith('rtmp'):
82                         a_format['ext'] = 'flv'
83
84                         # See com/longtailvideo/jwplayer/media/RTMPMediaProvider.as
85                         # of jwplayer.flash.swf
86                         rtmp_url_parts = re.split(
87                             r'((?:mp4|mp3|flv):)', source_url, 1)
88                         if len(rtmp_url_parts) == 3:
89                             rtmp_url, prefix, play_path = rtmp_url_parts
90                             a_format.update({
91                                 'url': rtmp_url,
92                                 'play_path': prefix + play_path,
93                             })
94                         if rtmp_params:
95                             a_format.update(rtmp_params)
96                     formats.append(a_format)
97             self._sort_formats(formats)
98
99             subtitles = {}
100             tracks = video_data.get('tracks')
101             if tracks and isinstance(tracks, list):
102                 for track in tracks:
103                     if track.get('file') and track.get('kind') == 'captions':
104                         subtitles.setdefault(track.get('label') or 'en', []).append({
105                             'url': self._proto_relative_url(track['file'])
106                         })
107
108             entries.append({
109                 'id': this_video_id,
110                 'title': video_data['title'] if require_title else video_data.get('title'),
111                 'description': video_data.get('description'),
112                 'thumbnail': self._proto_relative_url(video_data.get('image')),
113                 'timestamp': int_or_none(video_data.get('pubdate')),
114                 'duration': float_or_none(jwplayer_data.get('duration')),
115                 'subtitles': subtitles,
116                 'formats': formats,
117             })
118         if len(entries) == 1:
119             return entries[0]
120         else:
121             return self.playlist_result(entries)
122
123
124 class JWPlatformIE(JWPlatformBaseIE):
125     _VALID_URL = r'(?:https?://content\.jwplatform\.com/(?:feeds|players|jw6)/|jwplatform:)(?P<id>[a-zA-Z0-9]{8})'
126     _TEST = {
127         'url': 'http://content.jwplatform.com/players/nPripu9l-ALJ3XQCI.js',
128         'md5': 'fa8899fa601eb7c83a64e9d568bdf325',
129         'info_dict': {
130             'id': 'nPripu9l',
131             'ext': 'mov',
132             'title': 'Big Buck Bunny Trailer',
133             'description': 'Big Buck Bunny is a short animated film by the Blender Institute. It is made using free and open source software.',
134             'upload_date': '20081127',
135             'timestamp': 1227796140,
136         }
137     }
138
139     @staticmethod
140     def _extract_url(webpage):
141         mobj = re.search(
142             r'<script[^>]+?src=["\'](?P<url>(?:https?:)?//content.jwplatform.com/players/[a-zA-Z0-9]{8})',
143             webpage)
144         if mobj:
145             return mobj.group('url')
146
147     def _real_extract(self, url):
148         video_id = self._match_id(url)
149         json_data = self._download_json('http://content.jwplatform.com/feeds/%s.json' % video_id, video_id)
150         return self._parse_jwplayer_data(json_data, video_id)