wsj.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. int_or_none,
  6. float_or_none,
  7. unified_strdate,
  8. )
  9. class WSJIE(InfoExtractor):
  10. _VALID_URL = r'''(?x)
  11. (?:
  12. https?://video-api\.wsj\.com/api-video/player/iframe\.html\?guid=|
  13. https?://(?:www\.)?wsj\.com/video/[^/]+/|
  14. wsj:
  15. )
  16. (?P<id>[a-zA-Z0-9-]+)'''
  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. def _real_extract(self, url):
  36. video_id = self._match_id(url)
  37. api_url = (
  38. 'http://video-api.wsj.com/api-video/find_all_videos.asp?'
  39. 'type=guid&count=1&query=%s&fields=type,hls,videoMP4List,'
  40. 'thumbnailList,author,description,name,duration,videoURL,'
  41. 'titletag,formattedCreationDate,keywords,editor' % video_id)
  42. info = self._download_json(api_url, video_id)['items'][0]
  43. title = info.get('name', info.get('titletag'))
  44. formats = []
  45. f4m_url = info.get('videoURL')
  46. if f4m_url:
  47. formats.extend(self._extract_f4m_formats(
  48. f4m_url, video_id, f4m_id='hds', fatal=False))
  49. m3u8_url = info.get('hls')
  50. if m3u8_url:
  51. formats.extend(self._extract_m3u8_formats(
  52. info['hls'], video_id, ext='mp4',
  53. entry_protocol='m3u8_native', m3u8_id='hls', fatal=False))
  54. for v in info.get('videoMP4List', []):
  55. mp4_url = v.get('url')
  56. if not mp4_url:
  57. continue
  58. tbr = int_or_none(v.get('bitrate'))
  59. formats.append({
  60. 'url': mp4_url,
  61. 'format_id': 'http' + ('-%d' % tbr if tbr else ''),
  62. 'tbr': tbr,
  63. 'width': int_or_none(v.get('width')),
  64. 'height': int_or_none(v.get('height')),
  65. 'fps': float_or_none(v.get('fps')),
  66. })
  67. self._sort_formats(formats)
  68. return {
  69. 'id': video_id,
  70. 'formats': formats,
  71. # Thumbnails are conveniently in the correct format already
  72. 'thumbnails': info.get('thumbnailList'),
  73. 'creator': info.get('author'),
  74. 'uploader_id': info.get('editor'),
  75. 'duration': int_or_none(info.get('duration')),
  76. 'upload_date': unified_strdate(info.get(
  77. 'formattedCreationDate'), day_first=False),
  78. 'title': title,
  79. 'categories': info.get('keywords'),
  80. }
  81. class WSJArticleIE(InfoExtractor):
  82. _VALID_URL = r'(?i)https?://(?:www\.)?wsj\.com/articles/(?P<id>\w[^/]+)'
  83. _TESTS = [{
  84. 'url': 'https://www.wsj.com/articles/dont-like-china-no-pandas-for-you-1490366939?',
  85. 'info_dict': {
  86. 'id': '4B13FA62-1D8C-45DB-8EA1-4105CB20B362',
  87. 'ext': 'mp4',
  88. 'upload_date': '20170221',
  89. 'uploader_id': 'ralcaraz',
  90. 'title': 'Bao Bao the Panda Leaves for China',
  91. }
  92. }]
  93. def _real_extract(self, url):
  94. article_id = self._match_id(url)
  95. webpage = self._download_webpage(url, article_id)
  96. video_id = self._search_regex(r'data-src=["\']([A-Z0-9\-]+)',
  97. webpage, 'video id')
  98. return self.url_result('wsj:%s' % video_id, WSJIE.ie_key(), video_id)