]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/screenjunkies.py
debian/rules: Ignore tests temporarily.
[youtubedl] / youtube_dl / extractor / screenjunkies.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..compat import compat_str
7 from ..utils import (
8 int_or_none,
9 parse_age_limit,
10 )
11
12
13 class ScreenJunkiesIE(InfoExtractor):
14 _VALID_URL = r'https?://www.screenjunkies.com/video/(?P<display_id>[^/]+?)(?:-(?P<id>\d+))?(?:[/?#&]|$)'
15 _TESTS = [{
16 'url': 'http://www.screenjunkies.com/video/best-quentin-tarantino-movie-2841915',
17 'md5': '5c2b686bec3d43de42bde9ec047536b0',
18 'info_dict': {
19 'id': '2841915',
20 'display_id': 'best-quentin-tarantino-movie',
21 'ext': 'mp4',
22 'title': 'Best Quentin Tarantino Movie',
23 'thumbnail': 're:^https?://.*\.jpg',
24 'duration': 3671,
25 'age_limit': 13,
26 'tags': list,
27 },
28 }, {
29 'url': 'http://www.screenjunkies.com/video/honest-trailers-the-dark-knight',
30 'info_dict': {
31 'id': '2348808',
32 'display_id': 'honest-trailers-the-dark-knight',
33 'ext': 'mp4',
34 'title': "Honest Trailers: 'The Dark Knight'",
35 'thumbnail': 're:^https?://.*\.jpg',
36 'age_limit': 10,
37 'tags': list,
38 },
39 }, {
40 # requires subscription but worked around
41 'url': 'http://www.screenjunkies.com/video/knocking-dead-ep-1-the-show-so-far-3003285',
42 'info_dict': {
43 'id': '3003285',
44 'display_id': 'knocking-dead-ep-1-the-show-so-far',
45 'ext': 'mp4',
46 'title': 'Knocking Dead Ep 1: State of The Dead Recap',
47 'thumbnail': 're:^https?://.*\.jpg',
48 'duration': 3307,
49 'age_limit': 13,
50 'tags': list,
51 },
52 }]
53
54 _DEFAULT_BITRATES = (48, 150, 496, 864, 2240)
55
56 def _real_extract(self, url):
57 mobj = re.match(self._VALID_URL, url)
58 video_id = mobj.group('id')
59 display_id = mobj.group('display_id')
60
61 if not video_id:
62 webpage = self._download_webpage(url, display_id)
63 video_id = self._search_regex(
64 (r'src=["\']/embed/(\d+)', r'data-video-content-id=["\'](\d+)'),
65 webpage, 'video id')
66
67 webpage = self._download_webpage(
68 'http://www.screenjunkies.com/embed/%s' % video_id,
69 display_id, 'Downloading video embed page')
70 embed_vars = self._parse_json(
71 self._search_regex(
72 r'(?s)embedVars\s*=\s*({.+?})\s*</script>', webpage, 'embed vars'),
73 display_id)
74
75 title = embed_vars['contentName']
76
77 formats = []
78 bitrates = []
79 for f in embed_vars.get('media', []):
80 if not f.get('uri') or f.get('mediaPurpose') != 'play':
81 continue
82 bitrate = int_or_none(f.get('bitRate'))
83 if bitrate:
84 bitrates.append(bitrate)
85 formats.append({
86 'url': f['uri'],
87 'format_id': 'http-%d' % bitrate if bitrate else 'http',
88 'width': int_or_none(f.get('width')),
89 'height': int_or_none(f.get('height')),
90 'tbr': bitrate,
91 'format': 'mp4',
92 })
93
94 if not bitrates:
95 # When subscriptionLevel > 0, i.e. plus subscription is required
96 # media list will be empty. However, hds and hls uris are still
97 # available. We can grab them assuming bitrates to be default.
98 bitrates = self._DEFAULT_BITRATES
99
100 auth_token = embed_vars.get('AuthToken')
101
102 def construct_manifest_url(base_url, ext):
103 pieces = [base_url]
104 pieces.extend([compat_str(b) for b in bitrates])
105 pieces.append('_kbps.mp4.%s?%s' % (ext, auth_token))
106 return ','.join(pieces)
107
108 if bitrates and auth_token:
109 hds_url = embed_vars.get('hdsUri')
110 if hds_url:
111 f4m_formats = self._extract_f4m_formats(
112 construct_manifest_url(hds_url, 'f4m'),
113 display_id, f4m_id='hds', fatal=False)
114 if len(f4m_formats) == len(bitrates):
115 for f, bitrate in zip(f4m_formats, bitrates):
116 if not f.get('tbr'):
117 f['format_id'] = 'hds-%d' % bitrate
118 f['tbr'] = bitrate
119 # TODO: fix f4m downloader to handle manifests without bitrates if possible
120 # formats.extend(f4m_formats)
121
122 hls_url = embed_vars.get('hlsUri')
123 if hls_url:
124 formats.extend(self._extract_m3u8_formats(
125 construct_manifest_url(hls_url, 'm3u8'),
126 display_id, 'mp4', entry_protocol='m3u8_native', m3u8_id='hls', fatal=False))
127 self._sort_formats(formats)
128
129 return {
130 'id': video_id,
131 'display_id': display_id,
132 'title': title,
133 'thumbnail': embed_vars.get('thumbUri'),
134 'duration': int_or_none(embed_vars.get('videoLengthInSeconds')) or None,
135 'age_limit': parse_age_limit(embed_vars.get('audienceRating')),
136 'tags': embed_vars.get('tags', '').split(','),
137 'formats': formats,
138 }