[youtube] Fix extraction.
[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 )
13
14
15 class TubiTvIE(InfoExtractor):
16     _VALID_URL = r'https?://(?:www\.)?tubitv\.com/(?:video|movies|tv-shows)/(?P<id>[0-9]+)'
17     _LOGIN_URL = 'http://tubitv.com/login'
18     _NETRC_MACHINE = 'tubitv'
19     _GEO_COUNTRIES = ['US']
20     _TESTS = [{
21         'url': 'http://tubitv.com/video/283829/the_comedian_at_the_friday',
22         'md5': '43ac06be9326f41912dc64ccf7a80320',
23         'info_dict': {
24             'id': '283829',
25             'ext': 'mp4',
26             'title': 'The Comedian at The Friday',
27             '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.',
28             'uploader_id': 'bc168bee0d18dd1cb3b86c68706ab434',
29         },
30     }, {
31         'url': 'http://tubitv.com/tv-shows/321886/s01_e01_on_nom_stories',
32         'only_matching': True,
33     }, {
34         'url': 'http://tubitv.com/movies/383676/tracker',
35         'only_matching': True,
36     }]
37
38     def _login(self):
39         username, password = self._get_login_info()
40         if username is None:
41             return
42         self.report_login()
43         form_data = {
44             'username': username,
45             'password': password,
46         }
47         payload = urlencode_postdata(form_data)
48         request = sanitized_Request(self._LOGIN_URL, payload)
49         request.add_header('Content-Type', 'application/x-www-form-urlencoded')
50         login_page = self._download_webpage(
51             request, None, False, 'Wrong login info')
52         if not re.search(r'id="tubi-logout"', login_page):
53             raise ExtractorError(
54                 'Login failed (invalid username/password)', expected=True)
55
56     def _real_initialize(self):
57         self._login()
58
59     def _real_extract(self, url):
60         video_id = self._match_id(url)
61         video_data = self._download_json(
62             'http://tubitv.com/oz/videos/%s/content' % video_id, video_id)
63         title = video_data['title']
64
65         formats = self._extract_m3u8_formats(
66             self._proto_relative_url(video_data['url']),
67             video_id, 'mp4', 'm3u8_native')
68         self._sort_formats(formats)
69
70         thumbnails = []
71         for thumbnail_url in video_data.get('thumbnails', []):
72             if not thumbnail_url:
73                 continue
74             thumbnails.append({
75                 'url': self._proto_relative_url(thumbnail_url),
76             })
77
78         subtitles = {}
79         for sub in video_data.get('subtitles', []):
80             sub_url = sub.get('url')
81             if not sub_url:
82                 continue
83             subtitles.setdefault(sub.get('lang', 'English'), []).append({
84                 'url': self._proto_relative_url(sub_url),
85             })
86
87         return {
88             'id': video_id,
89             'title': title,
90             'formats': formats,
91             'subtitles': subtitles,
92             'thumbnails': thumbnails,
93             'description': video_data.get('description'),
94             'duration': int_or_none(video_data.get('duration')),
95             'uploader_id': video_data.get('publisher_id'),
96         }