Switch codebase to use sanitized_Request instead of
[youtube-dl] / youtube_dl / extractor / mooshare.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..compat import compat_urllib_parse
7 from ..utils import (
8     ExtractorError,
9     sanitized_Request,
10 )
11
12
13 class MooshareIE(InfoExtractor):
14     IE_NAME = 'mooshare'
15     IE_DESC = 'Mooshare.biz'
16     _VALID_URL = r'http://(?:www\.)?mooshare\.biz/(?P<id>[\da-z]{12})'
17
18     _TESTS = [
19         {
20             'url': 'http://mooshare.biz/8dqtk4bjbp8g',
21             'md5': '4e14f9562928aecd2e42c6f341c8feba',
22             'info_dict': {
23                 'id': '8dqtk4bjbp8g',
24                 'ext': 'mp4',
25                 'title': 'Comedy Football 2011 - (part 1-2)',
26                 'duration': 893,
27             },
28         },
29         {
30             'url': 'http://mooshare.biz/aipjtoc4g95j',
31             'info_dict': {
32                 'id': 'aipjtoc4g95j',
33                 'ext': 'mp4',
34                 'title': 'Orange Caramel  Dashing Through the Snow',
35                 'duration': 212,
36             },
37             'params': {
38                 # rtmp download
39                 'skip_download': True,
40             }
41         }
42     ]
43
44     def _real_extract(self, url):
45         video_id = self._match_id(url)
46         page = self._download_webpage(url, video_id, 'Downloading page')
47
48         if re.search(r'>Video Not Found or Deleted<', page) is not None:
49             raise ExtractorError('Video %s does not exist' % video_id, expected=True)
50
51         hash_key = self._html_search_regex(r'<input type="hidden" name="hash" value="([^"]+)">', page, 'hash')
52         title = self._html_search_regex(r'(?m)<div class="blockTitle">\s*<h2>Watch ([^<]+)</h2>', page, 'title')
53
54         download_form = {
55             'op': 'download1',
56             'id': video_id,
57             'hash': hash_key,
58         }
59
60         request = sanitized_Request(
61             'http://mooshare.biz/%s' % video_id, compat_urllib_parse.urlencode(download_form))
62         request.add_header('Content-Type', 'application/x-www-form-urlencoded')
63
64         self._sleep(5, video_id)
65
66         video_page = self._download_webpage(request, video_id, 'Downloading video page')
67
68         thumbnail = self._html_search_regex(r'image:\s*"([^"]+)",', video_page, 'thumbnail', fatal=False)
69         duration_str = self._html_search_regex(r'duration:\s*"(\d+)",', video_page, 'duration', fatal=False)
70         duration = int(duration_str) if duration_str is not None else None
71
72         formats = []
73
74         # SD video
75         mobj = re.search(r'(?m)file:\s*"(?P<url>[^"]+)",\s*provider:', video_page)
76         if mobj is not None:
77             formats.append({
78                 'url': mobj.group('url'),
79                 'format_id': 'sd',
80                 'format': 'SD',
81             })
82
83         # HD video
84         mobj = re.search(r'\'hd-2\': { file: \'(?P<url>[^\']+)\' },', video_page)
85         if mobj is not None:
86             formats.append({
87                 'url': mobj.group('url'),
88                 'format_id': 'hd',
89                 'format': 'HD',
90             })
91
92         # rtmp video
93         mobj = re.search(r'(?m)file: "(?P<playpath>[^"]+)",\s*streamer: "(?P<rtmpurl>rtmp://[^"]+)",', video_page)
94         if mobj is not None:
95             formats.append({
96                 'url': mobj.group('rtmpurl'),
97                 'play_path': mobj.group('playpath'),
98                 'rtmp_live': False,
99                 'ext': 'mp4',
100                 'format_id': 'rtmp',
101                 'format': 'HD',
102             })
103
104         return {
105             'id': video_id,
106             'title': title,
107             'thumbnail': thumbnail,
108             'duration': duration,
109             'formats': formats,
110         }