tver.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from .common import InfoExtractor
  2. from ..utils import (
  3. ExtractorError,
  4. join_nonempty,
  5. smuggle_url,
  6. str_or_none,
  7. strip_or_none,
  8. traverse_obj,
  9. )
  10. class TVerIE(InfoExtractor):
  11. _VALID_URL = r'https?://(?:www\.)?tver\.jp/(?:(?P<type>lp|corner|series|episodes?|feature|tokyo2020/video)/)+(?P<id>[a-zA-Z0-9]+)'
  12. _TESTS = [{
  13. 'skip': 'videos are only available for 7 days',
  14. 'url': 'https://tver.jp/episodes/ep83nf3w4p',
  15. 'info_dict': {
  16. 'title': '家事ヤロウ!!! 売り場席巻のチーズSP&財前直見×森泉親子の脱東京暮らし密着!',
  17. 'description': 'md5:dc2c06b6acc23f1e7c730c513737719b',
  18. 'series': '家事ヤロウ!!!',
  19. 'episode': '売り場席巻のチーズSP&財前直見×森泉親子の脱東京暮らし密着!',
  20. 'alt_title': '売り場席巻のチーズSP&財前直見×森泉親子の脱東京暮らし密着!',
  21. 'channel': 'テレビ朝日',
  22. 'onair_label': '5月3日(火)放送分',
  23. 'ext_title': '家事ヤロウ!!! 売り場席巻のチーズSP&財前直見×森泉親子の脱東京暮らし密着! テレビ朝日 5月3日(火)放送分',
  24. },
  25. 'add_ie': ['BrightcoveNew'],
  26. }, {
  27. 'url': 'https://tver.jp/corner/f0103888',
  28. 'only_matching': True,
  29. }, {
  30. 'url': 'https://tver.jp/lp/f0033031',
  31. 'only_matching': True,
  32. }]
  33. BRIGHTCOVE_URL_TEMPLATE = 'http://players.brightcove.net/%s/default_default/index.html?videoId=%s'
  34. _PLATFORM_UID = None
  35. _PLATFORM_TOKEN = None
  36. def _real_initialize(self):
  37. create_response = self._download_json(
  38. 'https://platform-api.tver.jp/v2/api/platform_users/browser/create', None,
  39. note='Creating session', data=b'device_type=pc', headers={
  40. 'Origin': 'https://s.tver.jp',
  41. 'Referer': 'https://s.tver.jp/',
  42. 'Content-Type': 'application/x-www-form-urlencoded',
  43. })
  44. self._PLATFORM_UID = traverse_obj(create_response, ('result', 'platform_uid'))
  45. self._PLATFORM_TOKEN = traverse_obj(create_response, ('result', 'platform_token'))
  46. def _real_extract(self, url):
  47. video_id, video_type = self._match_valid_url(url).group('id', 'type')
  48. if video_type not in {'series', 'episodes'}:
  49. webpage = self._download_webpage(url, video_id, note='Resolving to new URL')
  50. video_id = self._match_id(self._search_regex(
  51. (r'canonical"\s*href="(https?://tver\.jp/[^"]+)"', r'&link=(https?://tver\.jp/[^?&]+)[?&]'),
  52. webpage, 'url regex'))
  53. episode_info = self._download_json(
  54. f'https://platform-api.tver.jp/service/api/v1/callEpisode/{video_id}?require_data=mylist,later[epefy106ur],good[epefy106ur],resume[epefy106ur]',
  55. video_id, fatal=False,
  56. query={
  57. 'platform_uid': self._PLATFORM_UID,
  58. 'platform_token': self._PLATFORM_TOKEN,
  59. }, headers={
  60. 'x-tver-platform-type': 'web'
  61. })
  62. episode_content = traverse_obj(
  63. episode_info, ('result', 'episode', 'content')) or {}
  64. video_info = self._download_json(
  65. f'https://statics.tver.jp/content/episode/{video_id}.json', video_id,
  66. query={
  67. 'v': str_or_none(episode_content.get('version')) or '5',
  68. }, headers={
  69. 'Origin': 'https://tver.jp',
  70. 'Referer': 'https://tver.jp/',
  71. })
  72. p_id = video_info['video']['accountID']
  73. r_id = traverse_obj(video_info, ('video', ('videoRefID', 'videoID')), get_all=False)
  74. if not r_id:
  75. raise ExtractorError('Failed to extract reference ID for Brightcove')
  76. if not r_id.isdigit():
  77. r_id = f'ref:{r_id}'
  78. episode = strip_or_none(episode_content.get('title'))
  79. series = str_or_none(episode_content.get('seriesTitle'))
  80. title = (
  81. join_nonempty(series, episode, delim=' ')
  82. or str_or_none(video_info.get('title')))
  83. provider = str_or_none(episode_content.get('productionProviderName'))
  84. onair_label = str_or_none(episode_content.get('broadcastDateLabel'))
  85. return {
  86. '_type': 'url_transparent',
  87. 'title': title,
  88. 'series': series,
  89. 'episode': episode,
  90. # an another title which is considered "full title" for some viewers
  91. 'alt_title': join_nonempty(title, provider, onair_label, delim=' '),
  92. 'channel': provider,
  93. 'description': str_or_none(video_info.get('description')),
  94. 'url': smuggle_url(
  95. self.BRIGHTCOVE_URL_TEMPLATE % (p_id, r_id), {'geo_countries': ['JP']}),
  96. 'ie_key': 'BrightcoveNew',
  97. }