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