rtp.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import base64
  2. import json
  3. import re
  4. import urllib.parse
  5. from .common import InfoExtractor
  6. from ..utils import js_to_json
  7. class RTPIE(InfoExtractor):
  8. _VALID_URL = r'https?://(?:www\.)?rtp\.pt/play/p(?P<program_id>[0-9]+)/(?P<id>[^/?#]+)/?'
  9. _TESTS = [{
  10. 'url': 'http://www.rtp.pt/play/p405/e174042/paixoes-cruzadas',
  11. 'md5': 'e736ce0c665e459ddb818546220b4ef8',
  12. 'info_dict': {
  13. 'id': 'e174042',
  14. 'ext': 'mp3',
  15. 'title': 'Paixões Cruzadas',
  16. 'description': 'As paixões musicais de António Cartaxo e António Macedo',
  17. 'thumbnail': r're:^https?://.*\.jpg',
  18. },
  19. }, {
  20. 'url': 'http://www.rtp.pt/play/p831/a-quimica-das-coisas',
  21. 'only_matching': True,
  22. }]
  23. _RX_OBFUSCATION = re.compile(r'''(?xs)
  24. atob\s*\(\s*decodeURIComponent\s*\(\s*
  25. (\[[0-9A-Za-z%,'"]*\])
  26. \s*\.\s*join\(\s*(?:""|'')\s*\)\s*\)\s*\)
  27. ''')
  28. def __unobfuscate(self, data, *, video_id):
  29. if data.startswith('{'):
  30. data = self._RX_OBFUSCATION.sub(
  31. lambda m: json.dumps(
  32. base64.b64decode(urllib.parse.unquote(
  33. ''.join(self._parse_json(m.group(1), video_id)),
  34. )).decode('iso-8859-1')),
  35. data)
  36. return js_to_json(data)
  37. def _real_extract(self, url):
  38. video_id = self._match_id(url)
  39. webpage = self._download_webpage(url, video_id)
  40. title = self._html_search_meta(
  41. 'twitter:title', webpage, display_name='title', fatal=True)
  42. f, config = self._search_regex(
  43. r'''(?sx)
  44. var\s+f\s*=\s*(?P<f>".*?"|{[^;]+?});\s*
  45. var\s+player1\s+=\s+new\s+RTPPlayer\s*\((?P<config>{(?:(?!\*/).)+?})\);(?!\s*\*/)
  46. ''', webpage,
  47. 'player config', group=('f', 'config'))
  48. f = self._parse_json(
  49. f, video_id,
  50. lambda data: self.__unobfuscate(data, video_id=video_id))
  51. config = self._parse_json(
  52. config, video_id,
  53. lambda data: self.__unobfuscate(data, video_id=video_id))
  54. formats = []
  55. if isinstance(f, dict):
  56. f_hls = f.get('hls')
  57. if f_hls is not None:
  58. formats.extend(self._extract_m3u8_formats(
  59. f_hls, video_id, 'mp4', 'm3u8_native', m3u8_id='hls'))
  60. f_dash = f.get('dash')
  61. if f_dash is not None:
  62. formats.extend(self._extract_mpd_formats(f_dash, video_id, mpd_id='dash'))
  63. else:
  64. formats.append({
  65. 'format_id': 'f',
  66. 'url': f,
  67. 'vcodec': 'none' if config.get('mediaType') == 'audio' else None,
  68. })
  69. subtitles = {}
  70. vtt = config.get('vtt')
  71. if vtt is not None:
  72. for lcode, lname, url in vtt:
  73. subtitles.setdefault(lcode, []).append({
  74. 'name': lname,
  75. 'url': url,
  76. })
  77. return {
  78. 'id': video_id,
  79. 'title': title,
  80. 'formats': formats,
  81. 'description': self._html_search_meta(['description', 'twitter:description'], webpage),
  82. 'thumbnail': config.get('poster') or self._og_search_thumbnail(webpage),
  83. 'subtitles': subtitles,
  84. }