urplay.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. from .common import InfoExtractor
  2. from ..utils import (
  3. dict_get,
  4. ExtractorError,
  5. int_or_none,
  6. ISO639Utils,
  7. parse_age_limit,
  8. try_get,
  9. unified_timestamp,
  10. )
  11. class URPlayIE(InfoExtractor):
  12. _VALID_URL = r'https?://(?:www\.)?ur(?:play|skola)\.se/(?:program|Produkter)/(?P<id>[0-9]+)'
  13. _TESTS = [{
  14. 'url': 'https://urplay.se/program/203704-ur-samtiden-livet-universum-och-rymdens-markliga-musik-om-vetenskap-kritiskt-tankande-och-motstand',
  15. 'md5': 'ff5b0c89928f8083c74bbd5099c9292d',
  16. 'info_dict': {
  17. 'id': '203704',
  18. 'ext': 'mp4',
  19. 'title': 'UR Samtiden - Livet, universum och rymdens märkliga musik : Om vetenskap, kritiskt tänkande och motstånd',
  20. 'description': 'md5:5344508a52aa78c1ced6c1b8b9e44e9a',
  21. 'timestamp': 1513292400,
  22. 'upload_date': '20171214',
  23. 'series': 'UR Samtiden - Livet, universum och rymdens märkliga musik',
  24. 'duration': 2269,
  25. 'categories': ['Vetenskap & teknik'],
  26. 'tags': ['Kritiskt tänkande', 'Vetenskap', 'Vetenskaplig verksamhet'],
  27. 'episode': 'Om vetenskap, kritiskt tänkande och motstånd',
  28. 'age_limit': 15,
  29. },
  30. }, {
  31. 'url': 'https://urskola.se/Produkter/190031-Tripp-Trapp-Trad-Sovkudde',
  32. 'info_dict': {
  33. 'id': '190031',
  34. 'ext': 'mp4',
  35. 'title': 'Tripp, Trapp, Träd : Sovkudde',
  36. 'description': 'md5:b86bffdae04a7e9379d1d7e5947df1d1',
  37. 'timestamp': 1440086400,
  38. 'upload_date': '20150820',
  39. 'series': 'Tripp, Trapp, Träd',
  40. 'duration': 865,
  41. 'tags': ['Sova'],
  42. 'episode': 'Sovkudde',
  43. },
  44. }, {
  45. 'url': 'http://urskola.se/Produkter/155794-Smasagor-meankieli-Grodan-i-vida-varlden',
  46. 'only_matching': True,
  47. }]
  48. def _real_extract(self, url):
  49. video_id = self._match_id(url)
  50. url = url.replace('skola.se/Produkter', 'play.se/program')
  51. webpage = self._download_webpage(url, video_id)
  52. urplayer_data = self._search_nextjs_data(webpage, video_id, fatal=False) or {}
  53. if urplayer_data:
  54. urplayer_data = try_get(urplayer_data, lambda x: x['props']['pageProps']['program'], dict)
  55. if not urplayer_data:
  56. raise ExtractorError('Unable to parse __NEXT_DATA__')
  57. else:
  58. accessible_episodes = self._parse_json(self._html_search_regex(
  59. r'data-react-class="routes/Product/components/ProgramContainer/ProgramContainer"[^>]+data-react-props="({.+?})"',
  60. webpage, 'urplayer data'), video_id)['accessibleEpisodes']
  61. urplayer_data = next(e for e in accessible_episodes if e.get('id') == int_or_none(video_id))
  62. episode = urplayer_data['title']
  63. host = self._download_json('http://streaming-loadbalancer.ur.se/loadbalancer.json', video_id)['redirect']
  64. formats = []
  65. urplayer_streams = urplayer_data.get('streamingInfo', {})
  66. for k, v in urplayer_streams.get('raw', {}).items():
  67. if not (k in ('sd', 'hd') and isinstance(v, dict)):
  68. continue
  69. file_http = v.get('location')
  70. if file_http:
  71. formats.extend(self._extract_wowza_formats(
  72. 'http://%s/%splaylist.m3u8' % (host, file_http),
  73. video_id, skip_protocols=['f4m', 'rtmp', 'rtsp']))
  74. subtitles = {}
  75. def parse_lang_code(code):
  76. "3-character language code or None (utils candidate)"
  77. if code is None:
  78. return
  79. lang = code.lower()
  80. if not ISO639Utils.long2short(lang):
  81. lang = ISO639Utils.short2long(lang)
  82. return lang or None
  83. for k, v in (urplayer_data['streamingInfo'].get('sweComplete') or {}).items():
  84. if (k in ('sd', 'hd') or not isinstance(v, dict)):
  85. continue
  86. lang, sttl_url = (v.get(kk) for kk in ('language', 'location', ))
  87. if not sttl_url:
  88. continue
  89. lang = parse_lang_code(lang)
  90. if not lang:
  91. continue
  92. sttl = subtitles.get(lang) or []
  93. sttl.append({'ext': k, 'url': sttl_url, })
  94. subtitles[lang] = sttl
  95. image = urplayer_data.get('image') or {}
  96. thumbnails = []
  97. for k, v in image.items():
  98. t = {
  99. 'id': k,
  100. 'url': v,
  101. }
  102. wh = k.split('x')
  103. if len(wh) == 2:
  104. t.update({
  105. 'width': int_or_none(wh[0]),
  106. 'height': int_or_none(wh[1]),
  107. })
  108. thumbnails.append(t)
  109. series = urplayer_data.get('series') or {}
  110. series_title = dict_get(series, ('seriesTitle', 'title')) or dict_get(urplayer_data, ('seriesTitle', 'mainTitle'))
  111. return {
  112. 'id': video_id,
  113. 'title': '%s : %s' % (series_title, episode) if series_title else episode,
  114. 'description': urplayer_data.get('description'),
  115. 'thumbnails': thumbnails,
  116. 'timestamp': unified_timestamp(urplayer_data.get('publishedAt')),
  117. 'series': series_title,
  118. 'formats': formats,
  119. 'duration': int_or_none(urplayer_data.get('duration')),
  120. 'categories': urplayer_data.get('categories'),
  121. 'tags': urplayer_data.get('keywords'),
  122. 'season': series.get('label'),
  123. 'episode': episode,
  124. 'episode_number': int_or_none(urplayer_data.get('episodeNumber')),
  125. 'age_limit': parse_age_limit(min(try_get(a, lambda x: x['from'], int) or 0
  126. for a in urplayer_data.get('ageRanges', []))),
  127. 'subtitles': subtitles,
  128. }