5da3f9bcde14c827e3293fbb2da6e15ecae0c678
[youtube-dl] / youtube_dl / extractor / screenjunkies.py
1 from __future__ import unicode_literals
2
3 import re
4 import json
5
6 from .common import (
7     InfoExtractor,
8     ExtractorError,
9 )
10
11 from ..utils import (
12     int_or_none,
13 )
14
15 class ScreenJunkiesIE(InfoExtractor):
16     _VALID_URL = r'http://www.screenjunkies.com/video/(.+-(?P<id>\d+)|.+)'
17
18     _TESTS = [{
19         'url': 'http://www.screenjunkies.com/video/best-quentin-tarantino-movie-2841915',
20         'info_dict': {
21             'id': '2841915',
22             'ext': 'mp4',
23             'title': 'Best Quentin Tarantino Movie',
24         },
25     }, {
26         'url': 'http://www.screenjunkies.com/video/honest-trailers-the-dark-knight',
27         'info_dict': {
28             'id': '2348808',
29             'ext': 'mp4',
30             'title': "Honest Trailers - 'The Dark Knight'",
31         },
32     }, {
33         'url': 'http://www.screenjunkies.com/video/knocking-dead-ep-1-the-show-so-far-3003285',
34         'only_matching': True,
35     }]
36
37     def _real_extract(self, url):
38         video_id = self._match_id(url)
39
40         if not video_id: # Older urls didn't have the id in them, but we can grab it manually
41             webpage = self._download_webpage(url, url)
42             video_id = self._html_search_regex(r'src="/embed/(\d+)"', webpage, 'video id')
43
44         webpage = self._download_webpage('http://www.screenjunkies.com/embed/%s' %video_id, video_id)
45         video_vars_str = self._html_search_regex(r'embedVars = (\{.+\})\s*</script>', webpage, 'video variables', flags=re.S)
46         video_vars = self._parse_json(video_vars_str, video_id)
47
48         # TODO: Figure out required cookies
49         if video_vars['subscriptionLevel'] > 0:
50             raise ExtractorError('This video requires ScreenJunkiesPlus', expected=True)
51
52         formats = []
53         for f in video_vars['media']:
54             if not f['mediaPurpose'] == 'play':
55                 continue
56
57             formats.append({
58                 'url': f['uri'],
59                 'width': int_or_none(f.get('width')),
60                 'height': int_or_none(f.get('height')),
61                 'tbr': int_or_none(f.get('bitRate')),
62                 'format': 'mp4',
63             })
64         self._sort_formats(formats)
65
66         return {
67             'id': video_id,
68             'title': video_vars['contentName'],
69             'formats': formats,
70             'duration': int_or_none(video_vars.get('videoLengthInSeconds')),
71             'thumbnail': video_vars.get('thumbUri'),
72             'tags': video_vars['tags'].split(',') if 'tags' in video_vars else [],
73         }
74