[tubitv] fix extraction(closes #8741)
[youtube-dl] / youtube_dl / extractor / tubitv.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     ExtractorError,
9     int_or_none,
10     sanitized_Request,
11     urlencode_postdata,
12     parse_iso8601,
13 )
14
15
16 class TubiTvIE(InfoExtractor):
17     _VALID_URL = r'https?://(?:www\.)?tubitv\.com/video/(?P<id>[0-9]+)'
18     _LOGIN_URL = 'http://tubitv.com/login'
19     _NETRC_MACHINE = 'tubitv'
20     _TEST = {
21         'url': 'http://tubitv.com/video/283829/the_comedian_at_the_friday',
22         'info_dict': {
23             'id': '283829',
24             'ext': 'mp4',
25             'title': 'The Comedian at The Friday',
26             'description': 'A stand up comedian is forced to look at the decisions in his life while on a one week trip to the west coast.',
27             'uploader': 'Indie Rights Films',
28             'upload_date': '20160111',
29             'timestamp': 1452555979,
30         },
31         'params': {
32             'skip_download': 'HLS download',
33         },
34     }
35
36     def _login(self):
37         (username, password) = self._get_login_info()
38         if username is None:
39             return
40         self.report_login()
41         form_data = {
42             'username': username,
43             'password': password,
44         }
45         payload = urlencode_postdata(form_data)
46         request = sanitized_Request(self._LOGIN_URL, payload)
47         request.add_header('Content-Type', 'application/x-www-form-urlencoded')
48         login_page = self._download_webpage(
49             request, None, False, 'Wrong login info')
50         if not re.search(r'id="tubi-logout"', login_page):
51             raise ExtractorError(
52                 'Login failed (invalid username/password)', expected=True)
53
54     def _real_initialize(self):
55         self._login()
56
57     def _real_extract(self, url):
58         video_id = self._match_id(url)
59         video_data = self._download_json(
60             'http://tubitv.com/oz/videos/%s/content' % video_id, video_id)
61         title = video_data['n']
62
63         formats = self._extract_m3u8_formats(
64             video_data['mh'], video_id, 'mp4', 'm3u8_native')
65         self._sort_formats(formats)
66
67         subtitles = {}
68         for sub in video_data.get('sb', []):
69             sub_url = sub.get('u')
70             if not sub_url:
71                 continue
72             subtitles.setdefault(sub.get('l', 'en'), []).append({
73                 'url': sub_url,
74             })
75
76         return {
77             'id': video_id,
78             'title': title,
79             'formats': formats,
80             'subtitles': subtitles,
81             'thumbnail': video_data.get('ph'),
82             'description': video_data.get('d'),
83             'duration': int_or_none(video_data.get('s')),
84             'timestamp': parse_iso8601(video_data.get('u')),
85             'uploader': video_data.get('on'),
86         }