PEP8: applied even more rules
[youtube-dl] / youtube_dl / extractor / fivemin.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7     compat_str,
8     compat_urllib_parse,
9     ExtractorError,
10 )
11
12
13 class FiveMinIE(InfoExtractor):
14     IE_NAME = '5min'
15     _VALID_URL = r'''(?x)
16         (?:https?://[^/]*?5min\.com/Scripts/PlayerSeed\.js\?(.*?&)?playList=|
17             5min:)
18         (?P<id>\d+)
19         '''
20
21     _TESTS = [
22         {
23             # From http://www.engadget.com/2013/11/15/ipad-mini-retina-display-review/
24             'url': 'http://pshared.5min.com/Scripts/PlayerSeed.js?sid=281&width=560&height=345&playList=518013791',
25             'md5': '4f7b0b79bf1a470e5004f7112385941d',
26             'info_dict': {
27                 'id': '518013791',
28                 'ext': 'mp4',
29                 'title': 'iPad Mini with Retina Display Review',
30             },
31         },
32         {
33             # From http://on.aol.com/video/how-to-make-a-next-level-fruit-salad-518086247
34             'url': '5min:518086247',
35             'md5': 'e539a9dd682c288ef5a498898009f69e',
36             'info_dict': {
37                 'id': '518086247',
38                 'ext': 'mp4',
39                 'title': 'How to Make a Next-Level Fruit Salad',
40             },
41         },
42     ]
43
44     @classmethod
45     def _build_result(cls, video_id):
46         return cls.url_result('5min:%s' % video_id, cls.ie_key())
47
48     def _real_extract(self, url):
49         mobj = re.match(self._VALID_URL, url)
50         video_id = mobj.group('id')
51         embed_url = 'https://embed.5min.com/playerseed/?playList=%s' % video_id
52         embed_page = self._download_webpage(embed_url, video_id,
53                                             'Downloading embed page')
54         sid = self._search_regex(r'sid=(\d+)', embed_page, 'sid')
55         query = compat_urllib_parse.urlencode({
56             'func': 'GetResults',
57             'playlist': video_id,
58             'sid': sid,
59             'isPlayerSeed': 'true',
60             'url': embed_url,
61         })
62         response = self._download_json(
63             'https://syn.5min.com/handlers/SenseHandler.ashx?' + query,
64             video_id)
65         if not response['success']:
66             err_msg = response['errorMessage']
67             if err_msg == 'ErrorVideoUserNotGeo':
68                 msg = 'Video not available from your location'
69             else:
70                 msg = 'Aol said: %s' % err_msg
71             raise ExtractorError(msg, expected=True, video_id=video_id)
72         info = response['binding'][0]
73
74         second_id = compat_str(int(video_id[:-2]) + 1)
75         formats = []
76         for quality, height in [(1, 320), (2, 480), (4, 720), (8, 1080)]:
77             if any(r['ID'] == quality for r in info['Renditions']):
78                 formats.append({
79                     'format_id': compat_str(quality),
80                     'url': 'http://avideos.5min.com/%s/%s/%s_%s.mp4' % (second_id[-3:], second_id, video_id, quality),
81                     'height': height,
82                 })
83
84         return {
85             'id': video_id,
86             'title': info['Title'],
87             'formats': formats,
88         }