caffeinetv.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. from .common import InfoExtractor
  2. from ..utils import (
  3. determine_ext,
  4. int_or_none,
  5. parse_iso8601,
  6. traverse_obj,
  7. urljoin,
  8. )
  9. class CaffeineTVIE(InfoExtractor):
  10. _VALID_URL = r'https?://(?:www\.)?caffeine\.tv/[^/?#]+/video/(?P<id>[\da-f-]+)'
  11. _TESTS = [{
  12. 'url': 'https://www.caffeine.tv/TsuSurf/video/cffc0a00-e73f-11ec-8080-80017d29f26e',
  13. 'info_dict': {
  14. 'id': 'cffc0a00-e73f-11ec-8080-80017d29f26e',
  15. 'ext': 'mp4',
  16. 'title': 'GOOOOD MORNINNNNN #highlights',
  17. 'timestamp': 1654702180,
  18. 'upload_date': '20220608',
  19. 'uploader': 'RahJON Wicc',
  20. 'uploader_id': 'TsuSurf',
  21. 'duration': 3145,
  22. 'age_limit': 17,
  23. 'thumbnail': 'https://www.caffeine.tv/broadcasts/776b6f84-9cd5-42e3-af1d-4a776eeed697/replay/lobby.jpg',
  24. 'comment_count': int,
  25. 'view_count': int,
  26. 'like_count': int,
  27. 'tags': ['highlights', 'battlerap'],
  28. },
  29. 'params': {
  30. 'skip_download': 'm3u8',
  31. },
  32. }]
  33. def _real_extract(self, url):
  34. video_id = self._match_id(url)
  35. json_data = self._download_json(
  36. f'https://api.caffeine.tv/social/public/activity/{video_id}', video_id)
  37. broadcast_info = traverse_obj(json_data, ('broadcast_info', {dict})) or {}
  38. video_url = broadcast_info['video_url']
  39. ext = determine_ext(video_url)
  40. if ext == 'm3u8':
  41. formats = self._extract_m3u8_formats(video_url, video_id, 'mp4')
  42. else:
  43. formats = [{'url': video_url}]
  44. return {
  45. 'id': video_id,
  46. 'formats': formats,
  47. **traverse_obj(json_data, {
  48. 'like_count': ('like_count', {int_or_none}),
  49. 'view_count': ('view_count', {int_or_none}),
  50. 'comment_count': ('comment_count', {int_or_none}),
  51. 'tags': ('tags', ..., {str}, {lambda x: x or None}),
  52. 'uploader': ('user', 'name', {str}),
  53. 'uploader_id': (((None, 'user'), 'username'), {str}, any),
  54. 'is_live': ('is_live', {bool}),
  55. }),
  56. **traverse_obj(broadcast_info, {
  57. 'title': ('broadcast_title', {str}),
  58. 'duration': ('content_duration', {int_or_none}),
  59. 'timestamp': ('broadcast_start_time', {parse_iso8601}),
  60. 'thumbnail': ('preview_image_path', {lambda x: urljoin(url, x)}),
  61. }),
  62. 'age_limit': {
  63. # assume Apple Store ratings: https://en.wikipedia.org/wiki/Mobile_software_content_rating_system
  64. 'FOUR_PLUS': 0,
  65. 'NINE_PLUS': 9,
  66. 'TWELVE_PLUS': 12,
  67. 'SEVENTEEN_PLUS': 17,
  68. }.get(broadcast_info.get('content_rating'), 17),
  69. }