reuters.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import re
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. int_or_none,
  5. js_to_json,
  6. unescapeHTML,
  7. )
  8. class ReutersIE(InfoExtractor):
  9. _WORKING = False
  10. _VALID_URL = r'https?://(?:www\.)?reuters\.com/.*?\?.*?videoId=(?P<id>[0-9]+)'
  11. _TEST = {
  12. 'url': 'http://www.reuters.com/video/2016/05/20/san-francisco-police-chief-resigns?videoId=368575562',
  13. 'md5': '8015113643a0b12838f160b0b81cc2ee',
  14. 'info_dict': {
  15. 'id': '368575562',
  16. 'ext': 'mp4',
  17. 'title': 'San Francisco police chief resigns',
  18. },
  19. }
  20. def _real_extract(self, url):
  21. video_id = self._match_id(url)
  22. webpage = self._download_webpage(
  23. f'http://www.reuters.com/assets/iframe/yovideo?videoId={video_id}', video_id)
  24. video_data = js_to_json(self._search_regex(
  25. r'(?s)Reuters\.yovideo\.drawPlayer\(({.*?})\);',
  26. webpage, 'video data'))
  27. def get_json_value(key, fatal=False):
  28. return self._search_regex(rf'"{key}"\s*:\s*"([^"]+)"', video_data, key, fatal=fatal)
  29. title = unescapeHTML(get_json_value('title', fatal=True))
  30. mmid, fid = re.search(r',/(\d+)\?f=(\d+)', get_json_value('flv', fatal=True)).groups()
  31. mas_data = self._download_json(
  32. f'http://mas-e.cds1.yospace.com/mas/{mmid}/{fid}?trans=json',
  33. video_id, transform_source=js_to_json)
  34. formats = []
  35. for f in mas_data:
  36. f_url = f.get('url')
  37. if not f_url:
  38. continue
  39. method = f.get('method')
  40. if method == 'hls':
  41. formats.extend(self._extract_m3u8_formats(
  42. f_url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False))
  43. else:
  44. container = f.get('container')
  45. ext = '3gp' if method == 'mobile' else container
  46. formats.append({
  47. 'format_id': ext,
  48. 'url': f_url,
  49. 'ext': ext,
  50. 'container': container if method != 'mobile' else None,
  51. })
  52. return {
  53. 'id': video_id,
  54. 'title': title,
  55. 'thumbnail': get_json_value('thumb'),
  56. 'duration': int_or_none(get_json_value('seconds')),
  57. 'formats': formats,
  58. }