]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/jwplatform.py
Initiate new release.
[youtubedl] / 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 ..utils import (
8 determine_ext,
9 float_or_none,
10 int_or_none,
11 )
12
13
14 class JWPlatformBaseIE(InfoExtractor):
15 @staticmethod
16 def _find_jwplayer_data(webpage):
17 # TODO: Merge this with JWPlayer-related codes in generic.py
18
19 mobj = re.search(
20 'jwplayer\((?P<quote>[\'"])[^\'" ]+(?P=quote)\)\.setup\((?P<options>[^)]+)\)',
21 webpage)
22 if mobj:
23 return mobj.group('options')
24
25 def _extract_jwplayer_data(self, webpage, video_id, *args, **kwargs):
26 jwplayer_data = self._parse_json(
27 self._find_jwplayer_data(webpage), video_id)
28 return self._parse_jwplayer_data(
29 jwplayer_data, video_id, *args, **kwargs)
30
31 def _parse_jwplayer_data(self, jwplayer_data, video_id, require_title=True, m3u8_id=None, rtmp_params=None):
32 # JWPlayer backward compatibility: flattened playlists
33 # https://github.com/jwplayer/jwplayer/blob/v7.4.3/src/js/api/config.js#L81-L96
34 if 'playlist' not in jwplayer_data:
35 jwplayer_data = {'playlist': [jwplayer_data]}
36
37 video_data = jwplayer_data['playlist'][0]
38
39 # JWPlayer backward compatibility: flattened sources
40 # https://github.com/jwplayer/jwplayer/blob/v7.4.3/src/js/playlist/item.js#L29-L35
41 if 'sources' not in video_data:
42 video_data['sources'] = [video_data]
43
44 formats = []
45 for source in video_data['sources']:
46 source_url = self._proto_relative_url(source['file'])
47 source_type = source.get('type') or ''
48 if source_type in ('application/vnd.apple.mpegurl', 'hls') or determine_ext(source_url) == 'm3u8':
49 formats.extend(self._extract_m3u8_formats(
50 source_url, video_id, 'mp4', 'm3u8_native', m3u8_id=m3u8_id, fatal=False))
51 elif source_type.startswith('audio'):
52 formats.append({
53 'url': source_url,
54 'vcodec': 'none',
55 })
56 else:
57 a_format = {
58 'url': source_url,
59 'width': int_or_none(source.get('width')),
60 'height': int_or_none(source.get('height')),
61 }
62 if source_url.startswith('rtmp'):
63 a_format['ext'] = 'flv',
64
65 # See com/longtailvideo/jwplayer/media/RTMPMediaProvider.as
66 # of jwplayer.flash.swf
67 rtmp_url_parts = re.split(
68 r'((?:mp4|mp3|flv):)', source_url, 1)
69 if len(rtmp_url_parts) == 3:
70 rtmp_url, prefix, play_path = rtmp_url_parts
71 a_format.update({
72 'url': rtmp_url,
73 'play_path': prefix + play_path,
74 })
75 if rtmp_params:
76 a_format.update(rtmp_params)
77 formats.append(a_format)
78 self._sort_formats(formats)
79
80 subtitles = {}
81 tracks = video_data.get('tracks')
82 if tracks and isinstance(tracks, list):
83 for track in tracks:
84 if track.get('file') and track.get('kind') == 'captions':
85 subtitles.setdefault(track.get('label') or 'en', []).append({
86 'url': self._proto_relative_url(track['file'])
87 })
88
89 return {
90 'id': video_id,
91 'title': video_data['title'] if require_title else video_data.get('title'),
92 'description': video_data.get('description'),
93 'thumbnail': self._proto_relative_url(video_data.get('image')),
94 'timestamp': int_or_none(video_data.get('pubdate')),
95 'duration': float_or_none(jwplayer_data.get('duration')),
96 'subtitles': subtitles,
97 'formats': formats,
98 }
99
100
101 class JWPlatformIE(JWPlatformBaseIE):
102 _VALID_URL = r'(?:https?://content\.jwplatform\.com/(?:feeds|players|jw6)/|jwplatform:)(?P<id>[a-zA-Z0-9]{8})'
103 _TEST = {
104 'url': 'http://content.jwplatform.com/players/nPripu9l-ALJ3XQCI.js',
105 'md5': 'fa8899fa601eb7c83a64e9d568bdf325',
106 'info_dict': {
107 'id': 'nPripu9l',
108 'ext': 'mov',
109 'title': 'Big Buck Bunny Trailer',
110 'description': 'Big Buck Bunny is a short animated film by the Blender Institute. It is made using free and open source software.',
111 'upload_date': '20081127',
112 'timestamp': 1227796140,
113 }
114 }
115
116 @staticmethod
117 def _extract_url(webpage):
118 mobj = re.search(
119 r'<script[^>]+?src=["\'](?P<url>(?:https?:)?//content.jwplatform.com/players/[a-zA-Z0-9]{8})',
120 webpage)
121 if mobj:
122 return mobj.group('url')
123
124 def _real_extract(self, url):
125 video_id = self._match_id(url)
126 json_data = self._download_json('http://content.jwplatform.com/feeds/%s.json' % video_id, video_id)
127 return self._parse_jwplayer_data(json_data, video_id)