wsj.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. from .common import InfoExtractor
  2. from ..utils import (
  3. float_or_none,
  4. int_or_none,
  5. join_nonempty,
  6. unified_strdate,
  7. )
  8. class WSJIE(InfoExtractor):
  9. _VALID_URL = r'''(?x)
  10. (?:
  11. https?://video-api\.wsj\.com/api-video/player/iframe\.html\?.*?\bguid=|
  12. https?://(?:www\.)?(?:wsj|barrons)\.com/video/(?:[^/]+/)+|
  13. wsj:
  14. )
  15. (?P<id>[a-fA-F0-9-]{36})
  16. '''
  17. IE_DESC = 'Wall Street Journal'
  18. _TESTS = [{
  19. 'url': 'http://video-api.wsj.com/api-video/player/iframe.html?guid=1BD01A4C-BFE8-40A5-A42F-8A8AF9898B1A',
  20. 'md5': 'e230a5bb249075e40793b655a54a02e4',
  21. 'info_dict': {
  22. 'id': '1BD01A4C-BFE8-40A5-A42F-8A8AF9898B1A',
  23. 'ext': 'mp4',
  24. 'upload_date': '20150202',
  25. 'uploader_id': 'jdesai',
  26. 'creator': 'jdesai',
  27. 'categories': list, # a long list
  28. 'duration': 90,
  29. 'title': 'Bills Coach Rex Ryan Updates His Old Jets Tattoo',
  30. },
  31. }, {
  32. 'url': 'http://www.wsj.com/video/can-alphabet-build-a-smarter-city/359DDAA8-9AC1-489C-82E6-0429C1E430E0.html',
  33. 'only_matching': True,
  34. }, {
  35. 'url': 'http://www.barrons.com/video/capitalism-deserves-more-respect-from-millennials/F301217E-6F46-43AE-B8D2-B7180D642EE9.html',
  36. 'only_matching': True,
  37. }, {
  38. 'url': 'https://www.wsj.com/video/series/a-brief-history-of/the-modern-cell-carrier-how-we-got-here/980E2187-401D-48A1-B82B-1486CEE06CB9',
  39. 'only_matching': True,
  40. }]
  41. def _real_extract(self, url):
  42. video_id = self._match_id(url)
  43. info = self._download_json(
  44. 'http://video-api.wsj.com/api-video/find_all_videos.asp', video_id,
  45. query={
  46. 'type': 'guid',
  47. 'count': 1,
  48. 'query': video_id,
  49. 'fields': ','.join((
  50. 'type', 'hls', 'videoMP4List', 'thumbnailList', 'author',
  51. 'description', 'name', 'duration', 'videoURL', 'titletag',
  52. 'formattedCreationDate', 'keywords', 'editor')),
  53. })['items'][0]
  54. title = info.get('name', info.get('titletag'))
  55. formats = []
  56. f4m_url = info.get('videoURL')
  57. if f4m_url:
  58. formats.extend(self._extract_f4m_formats(
  59. f4m_url, video_id, f4m_id='hds', fatal=False))
  60. m3u8_url = info.get('hls')
  61. if m3u8_url:
  62. formats.extend(self._extract_m3u8_formats(
  63. info['hls'], video_id, ext='mp4',
  64. entry_protocol='m3u8_native', m3u8_id='hls', fatal=False))
  65. for v in info.get('videoMP4List', []):
  66. mp4_url = v.get('url')
  67. if not mp4_url:
  68. continue
  69. tbr = int_or_none(v.get('bitrate'))
  70. formats.append({
  71. 'url': mp4_url,
  72. 'format_id': join_nonempty('http', tbr),
  73. 'tbr': tbr,
  74. 'width': int_or_none(v.get('width')),
  75. 'height': int_or_none(v.get('height')),
  76. 'fps': float_or_none(v.get('fps')),
  77. })
  78. return {
  79. 'id': video_id,
  80. 'formats': formats,
  81. # Thumbnails are conveniently in the correct format already
  82. 'thumbnails': info.get('thumbnailList'),
  83. 'creator': info.get('author'),
  84. 'uploader_id': info.get('editor'),
  85. 'duration': int_or_none(info.get('duration')),
  86. 'upload_date': unified_strdate(info.get(
  87. 'formattedCreationDate'), day_first=False),
  88. 'title': title,
  89. 'categories': info.get('keywords'),
  90. }
  91. class WSJArticleIE(InfoExtractor):
  92. _VALID_URL = r'(?i)https?://(?:www\.)?wsj\.com/articles/(?P<id>[^/?#&]+)'
  93. _TEST = {
  94. 'url': 'https://www.wsj.com/articles/dont-like-china-no-pandas-for-you-1490366939?',
  95. 'info_dict': {
  96. 'id': '4B13FA62-1D8C-45DB-8EA1-4105CB20B362',
  97. 'ext': 'mp4',
  98. 'upload_date': '20170221',
  99. 'uploader_id': 'ralcaraz',
  100. 'title': 'Bao Bao the Panda Leaves for China',
  101. },
  102. }
  103. def _real_extract(self, url):
  104. article_id = self._match_id(url)
  105. webpage = self._download_webpage(url, article_id)
  106. video_id = self._search_regex(
  107. r'(?:id=["\']video|video-|iframe\.html\?guid=|data-src=["\'])([a-fA-F0-9-]{36})',
  108. webpage, 'video id')
  109. return self.url_result(f'wsj:{video_id}', WSJIE.ie_key(), video_id)