Merge remote-tracking branch 'rupertbaxter2/master'
[youtube-dl] / youtube_dl / extractor / playvid.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..compat import (
7     compat_urllib_parse,
8 )
9 from ..utils import (
10     clean_html,
11     ExtractorError,
12 )
13
14
15 class PlayvidIE(InfoExtractor):
16     _VALID_URL = r'https?://www\.playvid\.com/watch(\?v=|/)(?P<id>.+?)(?:#|$)'
17     _TEST = {
18         'url': 'http://www.playvid.com/watch/RnmBNgtrrJu',
19         'md5': 'ffa2f6b2119af359f544388d8c01eb6c',
20         'info_dict': {
21             'id': 'RnmBNgtrrJu',
22             'ext': 'mp4',
23             'title': 'md5:9256d01c6317e3f703848b5906880dc8',
24             'duration': 82,
25             'age_limit': 18,
26         }
27     }
28
29     def _real_extract(self, url):
30         video_id = self._match_id(url)
31         webpage = self._download_webpage(url, video_id)
32
33         m_error = re.search(
34             r'<div class="block-error">\s*<div class="heading">\s*<div>(?P<msg>.+?)</div>\s*</div>', webpage)
35         if m_error:
36             raise ExtractorError(clean_html(m_error.group('msg')), expected=True)
37
38         video_title = None
39         duration = None
40         video_thumbnail = None
41         formats = []
42
43         # most of the information is stored in the flashvars
44         flashvars = self._html_search_regex(
45             r'flashvars="(.+?)"', webpage, 'flashvars')
46
47         infos = compat_urllib_parse.unquote(flashvars).split(r'&')
48         for info in infos:
49             videovars_match = re.match(r'^video_vars\[(.+?)\]=(.+?)$', info)
50             if videovars_match:
51                 key = videovars_match.group(1)
52                 val = videovars_match.group(2)
53
54                 if key == 'title':
55                     video_title = compat_urllib_parse.unquote_plus(val)
56                 if key == 'duration':
57                     try:
58                         duration = int(val)
59                     except ValueError:
60                         pass
61                 if key == 'big_thumb':
62                     video_thumbnail = val
63
64                 videourl_match = re.match(
65                     r'^video_urls\]\[(?P<resolution>[0-9]+)p', key)
66                 if videourl_match:
67                     height = int(videourl_match.group('resolution'))
68                     formats.append({
69                         'height': height,
70                         'url': val,
71                     })
72         self._sort_formats(formats)
73
74         # Extract title - should be in the flashvars; if not, look elsewhere
75         if video_title is None:
76             video_title = self._html_search_regex(
77                 r'<title>(.*?)</title', webpage, 'title')
78
79         return {
80             'id': video_id,
81             'formats': formats,
82             'title': video_title,
83             'thumbnail': video_thumbnail,
84             'duration': duration,
85             'description': None,
86             'age_limit': 18
87         }