]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/koo.py
Tolerate failure to `--write-link` due to unknown URL
[yt-dlp.git] / yt_dlp / extractor / koo.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3 from .common import InfoExtractor
4 from ..utils import (
5 clean_html,
6 try_get,
7 )
8
9
10 class KooIE(InfoExtractor):
11 _VALID_URL = r'https?://(?:www\.)?kooapp\.com/koo/[^/]+/(?P<id>[^/&#$?]+)'
12 _TESTS = [{ # Test for video in the comments
13 'url': 'https://www.kooapp.com/koo/ytdlpTestAccount/946c4189-bc2d-4524-b95b-43f641e2adde',
14 'info_dict': {
15 'id': '946c4189-bc2d-4524-b95b-43f641e2adde',
16 'ext': 'mp4',
17 'title': 'test for video in comment',
18 'description': 'md5:daa77dc214add4da8b6ea7d2226776e7',
19 'timestamp': 1632215195,
20 'uploader_id': 'ytdlpTestAccount',
21 'uploader': 'yt-dlpTestAccount',
22 'duration': 7000,
23 'upload_date': '20210921'
24 },
25 'params': {'skip_download': True}
26 }, { # Test for koo with long title
27 'url': 'https://www.kooapp.com/koo/laxman_kumarDBFEC/33decbf7-5e1e-4bb8-bfd7-04744a064361',
28 'info_dict': {
29 'id': '33decbf7-5e1e-4bb8-bfd7-04744a064361',
30 'ext': 'mp4',
31 'title': 'md5:47a71c2337295330c5a19a8af1bbf450',
32 'description': 'md5:06a6a84e9321499486dab541693d8425',
33 'timestamp': 1632106884,
34 'uploader_id': 'laxman_kumarDBFEC',
35 'uploader': 'Laxman Kumar 🇮🇳',
36 'duration': 46000,
37 'upload_date': '20210920'
38 },
39 'params': {'skip_download': True}
40 }, { # Test for audio
41 'url': 'https://www.kooapp.com/koo/ytdlpTestAccount/a2a9c88e-ce4b-4d2d-952f-d06361c5b602',
42 'info_dict': {
43 'id': 'a2a9c88e-ce4b-4d2d-952f-d06361c5b602',
44 'ext': 'mp4',
45 'title': 'Test for audio',
46 'description': 'md5:ecb9a2b6a5d34b736cecb53788cb11e8',
47 'timestamp': 1632211634,
48 'uploader_id': 'ytdlpTestAccount',
49 'uploader': 'yt-dlpTestAccount',
50 'duration': 214000,
51 'upload_date': '20210921'
52 },
53 'params': {'skip_download': True}
54 }, { # Test for video
55 'url': 'https://www.kooapp.com/koo/ytdlpTestAccount/a3e56c53-c1ed-4ac9-ac02-ed1630e6b1d1',
56 'info_dict': {
57 'id': 'a3e56c53-c1ed-4ac9-ac02-ed1630e6b1d1',
58 'ext': 'mp4',
59 'title': 'Test for video',
60 'description': 'md5:7afc4eb839074ddeb2beea5dd6fe9500',
61 'timestamp': 1632211468,
62 'uploader_id': 'ytdlpTestAccount',
63 'uploader': 'yt-dlpTestAccount',
64 'duration': 14000,
65 'upload_date': '20210921'
66 },
67 'params': {'skip_download': True}
68 }, { # Test for link
69 'url': 'https://www.kooapp.com/koo/ytdlpTestAccount/01bf5b94-81a5-4d8e-a387-5f732022e15a',
70 'skip': 'No video/audio found at the provided url.',
71 'info_dict': {
72 'id': '01bf5b94-81a5-4d8e-a387-5f732022e15a',
73 'title': 'Test for link',
74 'ext': 'none',
75 },
76 }, { # Test for images
77 'url': 'https://www.kooapp.com/koo/ytdlpTestAccount/dc05d9cd-a61d-45fd-bb07-e8019d8ca8cb',
78 'skip': 'No video/audio found at the provided url.',
79 'info_dict': {
80 'id': 'dc05d9cd-a61d-45fd-bb07-e8019d8ca8cb',
81 'title': 'Test for images',
82 'ext': 'none',
83 },
84 }]
85
86 def _real_extract(self, url):
87 id = self._match_id(url)
88 data_json = self._download_json(f'https://www.kooapp.com/apiV1/ku/{id}?limit=20&offset=0&showSimilarKoos=true', id)['parentContent']
89 item_json = next(content['items'][0] for content in data_json
90 if try_get(content, lambda x: x['items'][0]['id']) == id)
91 media_json = item_json['mediaMap']
92 formats = []
93
94 mp4_url = media_json.get('videoMp4')
95 video_m3u8_url = media_json.get('videoHls')
96 if mp4_url:
97 formats.append({
98 'url': mp4_url,
99 'ext': 'mp4',
100 })
101 if video_m3u8_url:
102 formats.extend(self._extract_m3u8_formats(video_m3u8_url, id, fatal=False, ext='mp4'))
103 if not formats:
104 self.raise_no_formats('No video/audio found at the provided url.', expected=True)
105
106 self._sort_formats(formats)
107 return {
108 'id': id,
109 'title': clean_html(item_json.get('title')),
110 'description': f'{clean_html(item_json.get("title"))}\n\n{clean_html(item_json.get("enTransliteration"))}',
111 'timestamp': item_json.get('createdAt'),
112 'uploader_id': item_json.get('handle'),
113 'uploader': item_json.get('name'),
114 'duration': media_json.get('duration'),
115 'formats': formats,
116 }