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