4f86b3ee927541c8f31936103c3319ce48b97e72
[youtube-dl] / youtube_dl / extractor / tubitv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import codecs
5 import re
6
7 from .common import InfoExtractor
8 from ..compat import (
9     compat_urllib_parse,
10     compat_urllib_request
11 )
12 from ..utils import (
13     ExtractorError,
14     int_or_none,
15 )
16
17
18 class TubiTvIE(InfoExtractor):
19     _VALID_URL = r'https?://(?:www\.)?tubitv\.com/video\?id=(?P<id>[0-9]+)'
20     _LOGIN_URL = 'http://tubitv.com/login'
21     _NETRC_MACHINE = 'tubitv'
22     _TEST = {
23         'url': 'http://tubitv.com/video?id=54411&title=The_Kitchen_Musical_-_EP01',
24         'info_dict': {
25             'id': '54411',
26             'ext': 'mp4',
27             'title': 'The Kitchen Musical - EP01',
28             'thumbnail': 're:^https?://.*\.png$',
29             'description': 'md5:37532716166069b353e8866e71fefae7',
30             'duration': 2407,
31         },
32         'params': {
33             'skip_download': 'HLS download',
34         },
35     }
36
37     def _login(self):
38         (username, password) = self._get_login_info()
39         if username is None:
40             return
41         self.report_login()
42         form_data = {
43             'username': username,
44             'password': password,
45         }
46         payload = compat_urllib_parse.urlencode(form_data).encode('utf-8')
47         request = compat_urllib_request.Request(self._LOGIN_URL, payload)
48         request.add_header('Content-Type', 'application/x-www-form-urlencoded')
49         login_page = self._download_webpage(
50             request, None, False, 'Wrong login info')
51         if not re.search(r'id="tubi-logout"', login_page):
52             raise ExtractorError(
53                 'Login failed (invalid username/password)', expected=True)
54
55     def _real_initialize(self):
56         self._login()
57
58     def _real_extract(self, url):
59         video_id = self._match_id(url)
60
61         webpage = self._download_webpage(url, video_id)
62         if re.search(r"<(?:DIV|div) class='login-required-screen'>", webpage):
63             self.raise_login_required('This video requires login')
64
65         title = self._og_search_title(webpage)
66         description = self._og_search_description(webpage)
67         thumbnail = self._og_search_thumbnail(webpage)
68         duration = int_or_none(self._html_search_meta(
69             'video:duration', webpage, 'duration'))
70
71         apu = self._search_regex(r"apu='([^']+)'", webpage, 'apu')
72         m3u8_url = codecs.decode(apu, 'rot_13')[::-1]
73         formats = self._extract_m3u8_formats(m3u8_url, video_id, ext='mp4')
74
75         return {
76             'id': video_id,
77             'title': title,
78             'formats': formats,
79             'thumbnail': thumbnail,
80             'description': description,
81             'duration': duration,
82         }