]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/tenplay.py
[tenplay] Improve extractor (#3280)
[yt-dlp.git] / yt_dlp / extractor / tenplay.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from datetime import datetime
5 import base64
6
7 from .common import InfoExtractor
8 from ..utils import (
9 HEADRequest,
10 int_or_none,
11 urlencode_postdata,
12 )
13
14
15 class TenPlayIE(InfoExtractor):
16 _VALID_URL = r'https?://(?:www\.)?10play\.com\.au/(?:[^/]+/)+(?P<id>tpv\d{6}[a-z]{5})'
17 _NETRC_MACHINE = '10play'
18 _TESTS = [{
19 'url': 'https://10play.com.au/neighbours/web-extras/season-39/nathan-borg-is-the-first-aussie-actor-with-a-cochlear-implant-to-join-neighbours/tpv210128qupwd',
20 'info_dict': {
21 'id': '6226844312001',
22 'ext': 'mp4',
23 'title': 'Nathan Borg Is The First Aussie Actor With A Cochlear Implant To Join Neighbours',
24 'alt_title': 'Nathan Borg Is The First Aussie Actor With A Cochlear Implant To Join Neighbours',
25 'description': 'md5:a02d0199c901c2dd4c796f1e7dd0de43',
26 'duration': 186,
27 'season': 39,
28 'series': 'Neighbours',
29 'thumbnail': r're:https://.*\.jpg',
30 'uploader': 'Channel 10',
31 'age_limit': 15,
32 'timestamp': 1611810000,
33 'upload_date': '20210128',
34 'uploader_id': '2199827728001',
35 },
36 'params': {
37 'skip_download': True,
38 },
39 'skip': 'Only available in Australia',
40 }, {
41 'url': 'https://10play.com.au/todd-sampsons-body-hack/episodes/season-4/episode-7/tpv200921kvngh',
42 'info_dict': {
43 'id': '6192880312001',
44 'ext': 'mp4',
45 'title': "Todd Sampson's Body Hack - S4 Ep. 2",
46 'description': 'md5:fa278820ad90f08ea187f9458316ac74',
47 'age_limit': 15,
48 'timestamp': 1600770600,
49 'upload_date': '20200922',
50 'uploader': 'Channel 10',
51 'uploader_id': '2199827728001'
52 },
53 'params': {
54 'skip_download': True,
55 }
56 }, {
57 'url': 'https://10play.com.au/how-to-stay-married/web-extras/season-1/terrys-talks-ep-1-embracing-change/tpv190915ylupc',
58 'only_matching': True,
59 }]
60 _GEO_BYPASS = False
61
62 _AUS_AGES = {
63 'G': 0,
64 'PG': 15,
65 'M': 15,
66 'MA': 15,
67 'MA15+': 15,
68 'R': 18,
69 'X': 18
70 }
71
72 def _get_bearer_token(self, video_id):
73 username, password = self._get_login_info()
74 if username is None or password is None:
75 self.raise_login_required('Your 10play account\'s details must be provided with --username and --password.')
76 _timestamp = datetime.now().strftime('%Y%m%d000000')
77 _auth_header = base64.b64encode(_timestamp.encode('ascii')).decode('ascii')
78 data = self._download_json('https://10play.com.au/api/user/auth', video_id, 'Getting bearer token', headers={
79 'X-Network-Ten-Auth': _auth_header,
80 }, data=urlencode_postdata({
81 'email': username,
82 'password': password,
83 }))
84 return 'Bearer ' + data['jwt']['accessToken']
85
86 def _real_extract(self, url):
87 content_id = self._match_id(url)
88 data = self._download_json(
89 'https://10play.com.au/api/v1/videos/' + content_id, content_id)
90 headers = {}
91
92 if data.get('memberGated') is True:
93 _token = self._get_bearer_token(content_id)
94 headers = {'Authorization': _token}
95
96 _video_url = self._download_json(
97 data.get('playbackApiEndpoint'), content_id, 'Downloading video JSON',
98 headers=headers).get('source')
99 m3u8_url = self._request_webpage(HEADRequest(
100 _video_url), content_id).geturl()
101 if '10play-not-in-oz' in m3u8_url:
102 self.raise_geo_restricted(countries=['AU'])
103 formats = self._extract_m3u8_formats(m3u8_url, content_id, 'mp4')
104 self._sort_formats(formats)
105
106 return {
107 'formats': formats,
108 'subtitles': {'en': [{'url': data.get('captionUrl')}]} if data.get('captionUrl') else None,
109 'id': data.get('altId') or content_id,
110 'duration': data.get('duration'),
111 'title': data.get('subtitle'),
112 'alt_title': data.get('title'),
113 'description': data.get('description'),
114 'age_limit': self._AUS_AGES.get(data.get('classification')),
115 'series': data.get('tvShow'),
116 'season': int_or_none(data.get('season')),
117 'episode_number': int_or_none(data.get('episode')),
118 'timestamp': data.get('published'),
119 'thumbnail': data.get('imageUrl'),
120 'uploader': 'Channel 10',
121 'uploader_id': '2199827728001',
122 }