]>
Commit | Line | Data |
---|---|---|
5196b988 NJ |
1 | # coding: utf-8 |
2 | from __future__ import unicode_literals | |
3 | ||
5196b988 NJ |
4 | import re |
5 | ||
6 | from .common import InfoExtractor | |
5196b988 NJ |
7 | from ..utils import ( |
8 | ExtractorError, | |
9 | int_or_none, | |
b5be6dd5 | 10 | js_to_json, |
5c2266df | 11 | sanitized_Request, |
6e6bc8da | 12 | urlencode_postdata, |
5196b988 NJ |
13 | ) |
14 | ||
15 | ||
16 | class TubiTvIE(InfoExtractor): | |
b5be6dd5 A |
17 | _VALID_URL = r'''(?x) |
18 | (?: | |
19 | tubitv:| | |
20 | https?://(?:www\.)?tubitv\.com/(?:video|movies|tv-shows)/ | |
21 | ) | |
22 | (?P<id>[0-9]+)''' | |
5196b988 NJ |
23 | _LOGIN_URL = 'http://tubitv.com/login' |
24 | _NETRC_MACHINE = 'tubitv' | |
68f17a9c | 25 | _GEO_COUNTRIES = ['US'] |
4fe4bda2 | 26 | _TESTS = [{ |
9260cf1d | 27 | 'url': 'http://tubitv.com/video/283829/the_comedian_at_the_friday', |
f4dfa9a5 | 28 | 'md5': '43ac06be9326f41912dc64ccf7a80320', |
5196b988 | 29 | 'info_dict': { |
9260cf1d | 30 | 'id': '283829', |
5196b988 | 31 | 'ext': 'mp4', |
9260cf1d | 32 | 'title': 'The Comedian at The Friday', |
33 | '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.', | |
f4dfa9a5 | 34 | 'uploader_id': 'bc168bee0d18dd1cb3b86c68706ab434', |
5196b988 | 35 | }, |
4fe4bda2 RA |
36 | }, { |
37 | 'url': 'http://tubitv.com/tv-shows/321886/s01_e01_on_nom_stories', | |
38 | 'only_matching': True, | |
39 | }, { | |
40 | 'url': 'http://tubitv.com/movies/383676/tracker', | |
41 | 'only_matching': True, | |
29f7c58a | 42 | }, { |
43 | 'url': 'https://tubitv.com/movies/560057/penitentiary?start=true', | |
44 | 'info_dict': { | |
45 | 'id': '560057', | |
46 | 'ext': 'mp4', | |
47 | 'title': 'Penitentiary', | |
48 | 'description': 'md5:8d2fc793a93cc1575ff426fdcb8dd3f9', | |
49 | 'uploader_id': 'd8fed30d4f24fcb22ec294421b9defc2', | |
50 | 'release_year': 1979, | |
51 | }, | |
52 | 'params': { | |
53 | 'skip_download': True, | |
54 | }, | |
4fe4bda2 | 55 | }] |
5196b988 NJ |
56 | |
57 | def _login(self): | |
68217024 | 58 | username, password = self._get_login_info() |
5196b988 NJ |
59 | if username is None: |
60 | return | |
61 | self.report_login() | |
62 | form_data = { | |
63 | 'username': username, | |
64 | 'password': password, | |
65 | } | |
6e6bc8da | 66 | payload = urlencode_postdata(form_data) |
5c2266df | 67 | request = sanitized_Request(self._LOGIN_URL, payload) |
5196b988 NJ |
68 | request.add_header('Content-Type', 'application/x-www-form-urlencoded') |
69 | login_page = self._download_webpage( | |
70 | request, None, False, 'Wrong login info') | |
71 | if not re.search(r'id="tubi-logout"', login_page): | |
72 | raise ExtractorError( | |
73 | 'Login failed (invalid username/password)', expected=True) | |
74 | ||
75 | def _real_initialize(self): | |
76 | self._login() | |
77 | ||
78 | def _real_extract(self, url): | |
79 | video_id = self._match_id(url) | |
9260cf1d | 80 | video_data = self._download_json( |
81 | 'http://tubitv.com/oz/videos/%s/content' % video_id, video_id) | |
f4dfa9a5 | 82 | title = video_data['title'] |
5196b988 | 83 | |
9260cf1d | 84 | formats = self._extract_m3u8_formats( |
f4dfa9a5 RA |
85 | self._proto_relative_url(video_data['url']), |
86 | video_id, 'mp4', 'm3u8_native') | |
19dbaeec | 87 | self._sort_formats(formats) |
5196b988 | 88 | |
f4dfa9a5 RA |
89 | thumbnails = [] |
90 | for thumbnail_url in video_data.get('thumbnails', []): | |
91 | if not thumbnail_url: | |
92 | continue | |
93 | thumbnails.append({ | |
94 | 'url': self._proto_relative_url(thumbnail_url), | |
95 | }) | |
96 | ||
9260cf1d | 97 | subtitles = {} |
f4dfa9a5 RA |
98 | for sub in video_data.get('subtitles', []): |
99 | sub_url = sub.get('url') | |
9260cf1d | 100 | if not sub_url: |
101 | continue | |
f4dfa9a5 RA |
102 | subtitles.setdefault(sub.get('lang', 'English'), []).append({ |
103 | 'url': self._proto_relative_url(sub_url), | |
9260cf1d | 104 | }) |
105 | ||
5196b988 NJ |
106 | return { |
107 | 'id': video_id, | |
108 | 'title': title, | |
109 | 'formats': formats, | |
9260cf1d | 110 | 'subtitles': subtitles, |
f4dfa9a5 RA |
111 | 'thumbnails': thumbnails, |
112 | 'description': video_data.get('description'), | |
113 | 'duration': int_or_none(video_data.get('duration')), | |
114 | 'uploader_id': video_data.get('publisher_id'), | |
29f7c58a | 115 | 'release_year': int_or_none(video_data.get('year')), |
5196b988 | 116 | } |
b5be6dd5 A |
117 | |
118 | ||
119 | class TubiTvShowIE(InfoExtractor): | |
120 | _VALID_URL = r'https?://(?:www\.)?tubitv\.com/series/[0-9]+/(?P<show_name>[^/?#]+)' | |
121 | _TESTS = [{ | |
122 | 'url': 'https://tubitv.com/series/3936/the-joy-of-painting-with-bob-ross?start=true', | |
123 | 'playlist_mincount': 390, | |
124 | 'info_dict': { | |
125 | 'id': 'the-joy-of-painting-with-bob-ross', | |
126 | } | |
127 | }] | |
128 | ||
129 | def _entries(self, show_url, show_name): | |
130 | show_webpage = self._download_webpage(show_url, show_name) | |
131 | show_json = self._parse_json(self._search_regex( | |
132 | r"window\.__data\s*=\s*({.+?});\s*</script>", | |
133 | show_webpage, 'data',), show_name, transform_source=js_to_json)['video'] | |
134 | for episode_id in show_json['fullContentById'].keys(): | |
135 | yield self.url_result( | |
136 | 'tubitv:%s' % episode_id, | |
137 | ie=TubiTvIE.ie_key(), video_id=episode_id) | |
138 | ||
139 | def _real_extract(self, url): | |
140 | show_name = re.match(self._VALID_URL, url).group('show_name') | |
141 | return self.playlist_result(self._entries(url, show_name), playlist_id=show_name) |