[snotr] PEP8 and minor fixes (#3296)
[youtube-dl] / youtube_dl / extractor / snotr.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     float_or_none,
9     str_to_int,
10     parse_duration,
11 )
12
13
14 class SnotrIE(InfoExtractor):
15     _VALID_URL = r'http?://(?:www\.)?snotr\.com/video/(?P<id>\d+)/([\w]+)'
16     _TESTS = [{
17         'url': 'http://www.snotr.com/video/13708/Drone_flying_through_fireworks',
18         'info_dict': {
19             'id': '13708',
20             'ext': 'flv',
21             'title': 'Drone flying through fireworks!',
22             'duration': 247,
23             'filesize_approx': 98566144,
24         }
25     }, {
26         'url': 'http://www.snotr.com/video/530/David_Letteman_-_George_W_Bush_Top_10',
27         'info_dict': {
28             'id': '530',
29             'ext': 'flv',
30             'title': 'David Letteman - George W. Bush Top 10',
31             'duration': 126,
32             'filesize_approx': 8912896,
33         }
34     }]
35
36     def _real_extract(self, url):
37         mobj = re.match(self._VALID_URL, url)
38         video_id = mobj.group('id')
39
40         webpage = self._download_webpage(url, video_id)
41         title = self._og_search_title(webpage)
42
43         description = self._og_search_description(webpage)
44
45         video_url = "http://cdn.videos.snotr.com/%s.flv" % video_id
46
47         view_count = str_to_int(self._html_search_regex(
48             r'<p>\n<strong>Views:</strong>\n([\d,\.]+)</p>',
49             webpage, 'view count', fatal=False))
50
51         duration = parse_duration(self._html_search_regex(
52             r'<p>\n<strong>Length:</strong>\n\s*([0-9:]+).*?</p>',
53             webpage, 'duration', fatal=False))
54
55         filesize_approx = float_or_none(self._html_search_regex(
56             r'<p>\n<strong>Filesize:</strong>\n\s*([0-9.]+)\s*megabyte</p>',
57             webpage, 'filesize', fatal=False), invscale=1024 * 1024)
58
59         return {
60             'id': video_id,
61             'title': title,
62             'url': video_url,
63             'view_count': view_count,
64             'duration': duration,
65             'filesize_approx': filesize_approx,
66         }