koo.py 4.6 KB

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