playsuisse.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import json
  2. from .common import InfoExtractor
  3. from ..utils import int_or_none, traverse_obj
  4. class PlaySuisseIE(InfoExtractor):
  5. _VALID_URL = r'https?://(?:www\.)?playsuisse\.ch/watch/(?P<id>[0-9]+)'
  6. _TESTS = [
  7. {
  8. 'url': 'https://www.playsuisse.ch/watch/763211/0',
  9. 'md5': '82df2a470b2dfa60c2d33772a8a60cf8',
  10. 'info_dict': {
  11. 'id': '763211',
  12. 'ext': 'mp4',
  13. 'title': 'Knochen',
  14. 'description': 'md5:8ea7a8076ba000cd9e8bc132fd0afdd8',
  15. 'duration': 3344,
  16. 'series': 'Wilder',
  17. 'season': 'Season 1',
  18. 'season_number': 1,
  19. 'episode': 'Knochen',
  20. 'episode_number': 1,
  21. 'thumbnail': 'md5:9260abe0c0ec9b69914d0a10d54c5878'
  22. }
  23. },
  24. {
  25. 'url': 'https://www.playsuisse.ch/watch/808675/0',
  26. 'md5': '818b94c1d2d7c4beef953f12cb8f3e75',
  27. 'info_dict': {
  28. 'id': '808675',
  29. 'ext': 'mp4',
  30. 'title': 'Der Läufer',
  31. 'description': 'md5:9f61265c7e6dcc3e046137a792b275fd',
  32. 'duration': 5280,
  33. 'episode': 'Der Läufer',
  34. 'thumbnail': 'md5:44af7d65ee02bbba4576b131868bb783'
  35. }
  36. },
  37. {
  38. 'url': 'https://www.playsuisse.ch/watch/817193/0',
  39. 'md5': '1d6c066f92cd7fffd8b28a53526d6b59',
  40. 'info_dict': {
  41. 'id': '817193',
  42. 'ext': 'mp4',
  43. 'title': 'Die Einweihungsparty',
  44. 'description': 'md5:91ebf04d3a42cb3ab70666acf750a930',
  45. 'duration': 1380,
  46. 'series': 'Nr. 47',
  47. 'season': 'Season 1',
  48. 'season_number': 1,
  49. 'episode': 'Die Einweihungsparty',
  50. 'episode_number': 1,
  51. 'thumbnail': 'md5:637585fb106e3a4bcd991958924c7e44'
  52. }
  53. }
  54. ]
  55. _GRAPHQL_QUERY = '''
  56. query AssetWatch($assetId: ID!) {
  57. assetV2(id: $assetId) {
  58. ...Asset
  59. episodes {
  60. ...Asset
  61. }
  62. }
  63. }
  64. fragment Asset on AssetV2 {
  65. id
  66. name
  67. description
  68. duration
  69. episodeNumber
  70. seasonNumber
  71. seriesName
  72. medias {
  73. type
  74. url
  75. }
  76. thumbnail16x9 {
  77. ...ImageDetails
  78. }
  79. thumbnail2x3 {
  80. ...ImageDetails
  81. }
  82. thumbnail16x9WithTitle {
  83. ...ImageDetails
  84. }
  85. thumbnail2x3WithTitle {
  86. ...ImageDetails
  87. }
  88. }
  89. fragment ImageDetails on AssetImage {
  90. id
  91. url
  92. }'''
  93. def _get_media_data(self, media_id):
  94. # NOTE In the web app, the "locale" header is used to switch between languages,
  95. # However this doesn't seem to take effect when passing the header here.
  96. response = self._download_json(
  97. 'https://4bbepzm4ef.execute-api.eu-central-1.amazonaws.com/prod/graphql',
  98. media_id, data=json.dumps({
  99. 'operationName': 'AssetWatch',
  100. 'query': self._GRAPHQL_QUERY,
  101. 'variables': {'assetId': media_id}
  102. }).encode('utf-8'),
  103. headers={'Content-Type': 'application/json', 'locale': 'de'})
  104. return response['data']['assetV2']
  105. def _real_extract(self, url):
  106. media_id = self._match_id(url)
  107. media_data = self._get_media_data(media_id)
  108. info = self._extract_single(media_data)
  109. if media_data.get('episodes'):
  110. info.update({
  111. '_type': 'playlist',
  112. 'entries': map(self._extract_single, media_data['episodes']),
  113. })
  114. return info
  115. def _extract_single(self, media_data):
  116. thumbnails = traverse_obj(media_data, lambda k, _: k.startswith('thumbnail'))
  117. formats, subtitles = [], {}
  118. for media in traverse_obj(media_data, 'medias', default=[]):
  119. if not media.get('url') or media.get('type') != 'HLS':
  120. continue
  121. f, subs = self._extract_m3u8_formats_and_subtitles(
  122. media['url'], media_data['id'], 'mp4', m3u8_id='HLS', fatal=False)
  123. formats.extend(f)
  124. self._merge_subtitles(subs, target=subtitles)
  125. return {
  126. 'id': media_data['id'],
  127. 'title': media_data.get('name'),
  128. 'description': media_data.get('description'),
  129. 'thumbnails': thumbnails,
  130. 'duration': int_or_none(media_data.get('duration')),
  131. 'formats': formats,
  132. 'subtitles': subtitles,
  133. 'series': media_data.get('seriesName'),
  134. 'season_number': int_or_none(media_data.get('seasonNumber')),
  135. 'episode': media_data.get('name'),
  136. 'episode_number': int_or_none(media_data.get('episodeNumber')),
  137. }