]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/extractor/mooshare.py
Fix imports and general cleanup
[youtube-dl.git] / 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.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         }