4c5a4e673f08dbae7075f9db567a1f238e18e1a1
[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 ..compat import (
8     compat_urllib_request,
9     compat_urllib_parse,
10 )
11 from ..utils import (
12     ExtractorError,
13 )
14
15
16 class MooshareIE(InfoExtractor):
17     IE_NAME = 'mooshare'
18     IE_DESC = 'Mooshare.biz'
19     _VALID_URL = r'http://(?:www\.)?mooshare\.biz/(?P<id>[\da-z]{12})'
20
21     _TESTS = [
22         {
23             'url': 'http://mooshare.biz/8dqtk4bjbp8g',
24             'md5': '4e14f9562928aecd2e42c6f341c8feba',
25             'info_dict': {
26                 'id': '8dqtk4bjbp8g',
27                 'ext': 'mp4',
28                 'title': 'Comedy Football 2011 - (part 1-2)',
29                 'duration': 893,
30             },
31         },
32         {
33             'url': 'http://mooshare.biz/aipjtoc4g95j',
34             'info_dict': {
35                 'id': 'aipjtoc4g95j',
36                 'ext': 'mp4',
37                 'title': 'Orange Caramel  Dashing Through the Snow',
38                 'duration': 212,
39             },
40             'params': {
41                 # rtmp download
42                 'skip_download': True,
43             }
44         }
45     ]
46
47     def _real_extract(self, url):
48         video_id = self._match_id(url)
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('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._sleep(5, video_id)
68
69         video_page = self._download_webpage(request, video_id, 'Downloading video page')
70
71         thumbnail = self._html_search_regex(r'image:\s*"([^"]+)",', video_page, 'thumbnail', fatal=False)
72         duration_str = self._html_search_regex(r'duration:\s*"(\d+)",', video_page, 'duration', fatal=False)
73         duration = int(duration_str) if duration_str is not None else None
74
75         formats = []
76
77         # SD video
78         mobj = re.search(r'(?m)file:\s*"(?P<url>[^"]+)",\s*provider:', video_page)
79         if mobj is not None:
80             formats.append({
81                 'url': mobj.group('url'),
82                 'format_id': 'sd',
83                 'format': 'SD',
84             })
85
86         # HD video
87         mobj = re.search(r'\'hd-2\': { file: \'(?P<url>[^\']+)\' },', video_page)
88         if mobj is not None:
89             formats.append({
90                 'url': mobj.group('url'),
91                 'format_id': 'hd',
92                 'format': 'HD',
93             })
94
95         # rtmp video
96         mobj = re.search(r'(?m)file: "(?P<playpath>[^"]+)",\s*streamer: "(?P<rtmpurl>rtmp://[^"]+)",', video_page)
97         if mobj is not None:
98             formats.append({
99                 'url': mobj.group('rtmpurl'),
100                 'play_path': mobj.group('playpath'),
101                 'rtmp_live': False,
102                 'ext': 'mp4',
103                 'format_id': 'rtmp',
104                 'format': 'HD',
105             })
106
107         return {
108             'id': video_id,
109             'title': title,
110             'thumbnail': thumbnail,
111             'duration': duration,
112             'formats': formats,
113         }