logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

caffeine.py (2790B)


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