[bigflix] Improve formats extraction
[youtube-dl] / youtube_dl / extractor / bigflix.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import base64
5 import re
6
7 from .common import InfoExtractor
8 from ..compat import compat_urllib_parse_unquote
9
10
11 class BigflixIE(InfoExtractor):
12     _VALID_URL = r'https?://(?:www\.)?bigflix\.com/.+/(?P<id>[0-9]+)'
13     _TESTS = [{
14         'url': 'http://www.bigflix.com/Hindi-movies/Action-movies/Singham-Returns/16537',
15         'md5': 'ec76aa9b1129e2e5b301a474e54fab74',
16         'info_dict': {
17             'id': '16537',
18             'ext': 'mp4',
19             'title': 'Singham Returns',
20             'description': 'md5:3d2ba5815f14911d5cc6a501ae0cf65d',
21         }
22     }, {
23         # 2 formats
24         'url': 'http://www.bigflix.com/Tamil-movies/Drama-movies/Madarasapatinam/16070',
25         'info_dict': {
26             'id': '16070',
27             'ext': 'mp4',
28             'title': 'Madarasapatinam',
29             'description': 'md5:63b9b8ed79189c6f0418c26d9a3452ca',
30             'formats': 'mincount:2',
31         },
32         'params': {
33             'skip_download': True,
34         }
35     }, {
36         # multiple formats
37         'url': 'http://www.bigflix.com/Malayalam-movies/Drama-movies/Indian-Rupee/15967',
38         'only_matching': True,
39     }]
40
41     def _real_extract(self, url):
42         video_id = self._match_id(url)
43
44         webpage = self._download_webpage(url, video_id)
45
46         title = self._html_search_regex(
47             r'<div[^>]+class=["\']pagetitle["\'][^>]*>(.+?)</div>',
48             webpage, 'title')
49
50         def decode_url(quoted_b64_url):
51             return base64.b64decode(compat_urllib_parse_unquote(
52                 quoted_b64_url).encode('ascii')).decode('utf-8')
53
54         formats = []
55         for height, encoded_url in re.findall(
56             r'ContentURL_(\d{3,4})[pP][^=]+=([^&]+)', webpage):
57             video_url = decode_url(encoded_url)
58             f = {
59                 'url': video_url,
60                 'format_id': '%sp' % height,
61                 'height': int(height),
62             }
63             if video_url.startswith('rtmp'):
64                 f['ext'] = 'flv'
65             formats.append(f)
66
67         file_url = self._search_regex(
68             r'file=([^&]+)', webpage, 'video url', default=None)
69         if file_url:
70             video_url = decode_url(file_url)
71             if all(f['url'] != video_url for f in formats):
72                 formats.append({
73                     'url': decode_url(file_url),
74                 })
75
76         self._sort_formats(formats)
77
78         description = self._html_search_meta('description', webpage)
79
80         return {
81             'id': video_id,
82             'title': title,
83             'description': description,
84             'formats': formats
85         }