yahoo.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. import hashlib
  2. import itertools
  3. import urllib.parse
  4. from .common import InfoExtractor, SearchInfoExtractor
  5. from .youtube import YoutubeIE
  6. from ..utils import (
  7. ExtractorError,
  8. clean_html,
  9. int_or_none,
  10. join_nonempty,
  11. mimetype2ext,
  12. parse_iso8601,
  13. traverse_obj,
  14. try_get,
  15. url_or_none,
  16. )
  17. class YahooIE(InfoExtractor):
  18. IE_DESC = 'Yahoo screen and movies'
  19. _VALID_URL = r'(?P<url>https?://(?:(?P<country>[a-zA-Z]{2}(?:-[a-zA-Z]{2})?|malaysia)\.)?(?:[\da-zA-Z_-]+\.)?yahoo\.com/(?:[^/]+/)*(?P<id>[^?&#]*-[0-9]+(?:-[a-z]+)?)\.html)'
  20. _EMBED_REGEX = [r'<iframe[^>]+?src=(["\'])(?P<url>https?://(?:screen|movies)\.yahoo\.com/.+?\.html\?format=embed)\1']
  21. _TESTS = [{
  22. 'url': 'http://screen.yahoo.com/julian-smith-travis-legg-watch-214727115.html',
  23. 'info_dict': {
  24. 'id': '2d25e626-2378-391f-ada0-ddaf1417e588',
  25. 'ext': 'mp4',
  26. 'title': 'Julian Smith & Travis Legg Watch Julian Smith',
  27. 'description': 'Julian and Travis watch Julian Smith',
  28. 'duration': 6863,
  29. 'timestamp': 1369812016,
  30. 'upload_date': '20130529',
  31. },
  32. 'skip': 'No longer exists',
  33. }, {
  34. 'url': 'https://screen.yahoo.com/community/community-sizzle-reel-203225340.html?format=embed',
  35. 'md5': '7993e572fac98e044588d0b5260f4352',
  36. 'info_dict': {
  37. 'id': '4fe78544-8d48-39d8-97cd-13f205d9fcdb',
  38. 'ext': 'mp4',
  39. 'title': "Yahoo Saves 'Community'",
  40. 'description': 'md5:4d4145af2fd3de00cbb6c1d664105053',
  41. 'duration': 170,
  42. 'timestamp': 1406838636,
  43. 'upload_date': '20140731',
  44. },
  45. 'skip': 'Unfortunately, this video is not available in your region',
  46. }, {
  47. 'url': 'https://uk.screen.yahoo.com/editor-picks/cute-raccoon-freed-drain-using-091756545.html',
  48. 'md5': '71298482f7c64cbb7fa064e4553ff1c1',
  49. 'info_dict': {
  50. 'id': 'b3affa53-2e14-3590-852b-0e0db6cd1a58',
  51. 'ext': 'webm',
  52. 'title': 'Cute Raccoon Freed From Drain\u00a0Using Angle Grinder',
  53. 'description': 'md5:f66c890e1490f4910a9953c941dee944',
  54. 'duration': 97,
  55. 'timestamp': 1414489862,
  56. 'upload_date': '20141028',
  57. },
  58. 'skip': 'No longer exists',
  59. }, {
  60. 'url': 'http://news.yahoo.com/video/china-moses-crazy-blues-104538833.html',
  61. 'md5': '88e209b417f173d86186bef6e4d1f160',
  62. 'info_dict': {
  63. 'id': 'f885cf7f-43d4-3450-9fac-46ac30ece521',
  64. 'ext': 'mp4',
  65. 'title': 'China Moses Is Crazy About the Blues',
  66. 'description': 'md5:9900ab8cd5808175c7b3fe55b979bed0',
  67. 'duration': 128,
  68. 'timestamp': 1385722202,
  69. 'upload_date': '20131129',
  70. },
  71. }, {
  72. 'url': 'https://www.yahoo.com/movies/v/true-story-trailer-173000497.html',
  73. 'md5': '2a9752f74cb898af5d1083ea9f661b58',
  74. 'info_dict': {
  75. 'id': '071c4013-ce30-3a93-a5b2-e0413cd4a9d1',
  76. 'ext': 'mp4',
  77. 'title': '\'True Story\' Trailer',
  78. 'description': 'True Story',
  79. 'duration': 150,
  80. 'timestamp': 1418919206,
  81. 'upload_date': '20141218',
  82. },
  83. }, {
  84. 'url': 'https://gma.yahoo.com/pizza-delivery-man-surprised-huge-tip-college-kids-195200785.html',
  85. 'only_matching': True,
  86. }, {
  87. 'note': 'NBC Sports embeds',
  88. 'url': 'http://sports.yahoo.com/blogs/ncaab-the-dagger/tyler-kalinoski-s-buzzer-beater-caps-davidson-s-comeback-win-185609842.html?guid=nbc_cbk_davidsonbuzzerbeater_150313',
  89. 'info_dict': {
  90. 'id': '9CsDKds0kvHI',
  91. 'ext': 'flv',
  92. 'description': 'md5:df390f70a9ba7c95ff1daace988f0d8d',
  93. 'title': 'Tyler Kalinoski hits buzzer-beater to lift Davidson',
  94. 'upload_date': '20150313',
  95. 'uploader': 'NBCU-SPORTS',
  96. 'timestamp': 1426270238,
  97. },
  98. }, {
  99. 'url': 'https://tw.news.yahoo.com/-100120367.html',
  100. 'only_matching': True,
  101. }, {
  102. # Query result is embedded in webpage, but explicit request to video API fails with geo restriction
  103. 'url': 'https://screen.yahoo.com/community/communitary-community-episode-1-ladders-154501237.html',
  104. 'md5': '4fbafb9c9b6f07aa8f870629f6671b35',
  105. 'info_dict': {
  106. 'id': '1f32853c-a271-3eef-8cb6-f6d6872cb504',
  107. 'ext': 'mp4',
  108. 'title': 'Communitary - Community Episode 1: Ladders',
  109. 'description': 'md5:8fc39608213295748e1e289807838c97',
  110. 'duration': 1646,
  111. 'timestamp': 1440436550,
  112. 'upload_date': '20150824',
  113. 'series': 'Communitary',
  114. 'season_number': 6,
  115. 'episode_number': 1,
  116. },
  117. 'skip': 'No longer exists',
  118. }, {
  119. # ytwnews://cavideo/
  120. 'url': 'https://tw.video.yahoo.com/movie-tw/單車天使-中文版預-092316541.html',
  121. 'info_dict': {
  122. 'id': 'ba133ff2-0793-3510-b636-59dfe9ff6cff',
  123. 'ext': 'mp4',
  124. 'title': '單車天使 - 中文版預',
  125. 'description': '中文版預',
  126. 'timestamp': 1476696196,
  127. 'upload_date': '20161017',
  128. },
  129. 'params': {
  130. 'skip_download': True,
  131. },
  132. }, {
  133. # Contains both a Yahoo hosted video and multiple Youtube embeds
  134. 'url': 'https://www.yahoo.com/entertainment/gwen-stefani-reveals-the-pop-hit-she-passed-on-assigns-it-to-her-voice-contestant-instead-033045672.html',
  135. 'info_dict': {
  136. 'id': '46c5d95a-528f-3d03-b732-732fcadd51de',
  137. 'title': 'Gwen Stefani reveals the pop hit she passed on, assigns it to her \'Voice\' contestant instead',
  138. 'description': 'Gwen decided not to record this hit herself, but she decided it was the perfect fit for Kyndall Inskeep.',
  139. },
  140. 'playlist': [{
  141. 'info_dict': {
  142. 'id': '966d4262-4fd1-3aaa-b45b-049ca6e38ba6',
  143. 'ext': 'mp4',
  144. 'title': 'Gwen Stefani reveals she turned down one of Sia\'s best songs',
  145. 'description': 'On "The Voice" Tuesday, Gwen Stefani told Taylor Swift which Sia hit was almost hers.',
  146. 'timestamp': 1572406500,
  147. 'upload_date': '20191030',
  148. },
  149. }, {
  150. 'info_dict': {
  151. 'id': '352CFDOQrKg',
  152. 'ext': 'mp4',
  153. 'title': 'Kyndal Inskeep "Performs the Hell Out of" Sia\'s "Elastic Heart" - The Voice Knockouts 2019',
  154. 'description': 'md5:7fe8e3d5806f96002e55f190d1d94479',
  155. 'uploader': 'The Voice',
  156. 'uploader_id': 'NBCTheVoice',
  157. 'upload_date': '20191029',
  158. },
  159. }],
  160. 'params': {
  161. 'playlistend': 2,
  162. },
  163. 'expected_warnings': ['HTTP Error 404', 'Ignoring subtitle tracks'],
  164. }, {
  165. 'url': 'https://malaysia.news.yahoo.com/video/bystanders-help-ontario-policeman-bust-190932818.html',
  166. 'only_matching': True,
  167. }, {
  168. 'url': 'https://es-us.noticias.yahoo.com/es-la-puerta-irrompible-que-110539379.html',
  169. 'only_matching': True,
  170. }, {
  171. 'url': 'https://www.yahoo.com/entertainment/v/longtime-cbs-news-60-minutes-032036500-cbs.html',
  172. 'only_matching': True,
  173. }]
  174. def _extract_yahoo_video(self, video_id, country):
  175. video = self._download_json(
  176. f'https://{country}.yahoo.com/_td/api/resource/VideoService.videos;view=full;video_ids=["{video_id}"]',
  177. video_id, 'Downloading video JSON metadata')[0]
  178. title = video['title']
  179. if country == 'malaysia':
  180. country = 'my'
  181. is_live = video.get('live_state') == 'live'
  182. fmts = ('m3u8',) if is_live else ('webm', 'mp4')
  183. urls = []
  184. formats = []
  185. subtitles = {}
  186. for fmt in fmts:
  187. media_obj = self._download_json(
  188. 'https://video-api.yql.yahoo.com/v1/video/sapi/streams/' + video_id,
  189. video_id, f'Downloading {fmt} JSON metadata',
  190. headers=self.geo_verification_headers(), query={
  191. 'format': fmt,
  192. 'region': country.upper(),
  193. })['query']['results']['mediaObj'][0]
  194. msg = media_obj.get('status', {}).get('msg')
  195. for s in media_obj.get('streams', []):
  196. host = s.get('host')
  197. path = s.get('path')
  198. if not host or not path:
  199. continue
  200. s_url = host + path
  201. if s.get('format') == 'm3u8':
  202. formats.extend(self._extract_m3u8_formats(
  203. s_url, video_id, 'mp4', m3u8_id='hls', fatal=False))
  204. continue
  205. tbr = int_or_none(s.get('bitrate'))
  206. formats.append({
  207. 'url': s_url,
  208. 'format_id': join_nonempty(fmt, tbr),
  209. 'width': int_or_none(s.get('width')),
  210. 'height': int_or_none(s.get('height')),
  211. 'tbr': tbr,
  212. 'fps': int_or_none(s.get('framerate')),
  213. })
  214. for cc in media_obj.get('closedcaptions', []):
  215. cc_url = cc.get('url')
  216. if not cc_url or cc_url in urls:
  217. continue
  218. urls.append(cc_url)
  219. subtitles.setdefault(cc.get('lang') or 'en-US', []).append({
  220. 'url': cc_url,
  221. 'ext': mimetype2ext(cc.get('content_type')),
  222. })
  223. streaming_url = video.get('streaming_url')
  224. if streaming_url and not is_live:
  225. formats.extend(self._extract_m3u8_formats(
  226. streaming_url, video_id, 'mp4',
  227. 'm3u8_native', m3u8_id='hls', fatal=False))
  228. if not formats and msg == 'geo restricted':
  229. self.raise_geo_restricted(metadata_available=True)
  230. thumbnails = []
  231. for thumb in video.get('thumbnails', []):
  232. thumb_url = thumb.get('url')
  233. if not thumb_url:
  234. continue
  235. thumbnails.append({
  236. 'id': thumb.get('tag'),
  237. 'url': thumb.get('url'),
  238. 'width': int_or_none(thumb.get('width')),
  239. 'height': int_or_none(thumb.get('height')),
  240. })
  241. series_info = video.get('series_info') or {}
  242. return {
  243. 'id': video_id,
  244. 'title': title,
  245. 'formats': formats,
  246. 'thumbnails': thumbnails,
  247. 'description': clean_html(video.get('description')),
  248. 'timestamp': parse_iso8601(video.get('publish_time')),
  249. 'subtitles': subtitles,
  250. 'duration': int_or_none(video.get('duration')),
  251. 'view_count': int_or_none(video.get('view_count')),
  252. 'is_live': is_live,
  253. 'series': video.get('show_name'),
  254. 'season_number': int_or_none(series_info.get('season_number')),
  255. 'episode_number': int_or_none(series_info.get('episode_number')),
  256. }
  257. def _real_extract(self, url):
  258. url, country, display_id = self._match_valid_url(url).groups()
  259. if not country:
  260. country = 'us'
  261. else:
  262. country = country.split('-')[0]
  263. items = self._download_json(
  264. f'https://{country}.yahoo.com/caas/content/article', display_id,
  265. 'Downloading content JSON metadata', query={
  266. 'url': url,
  267. })['items'][0]
  268. item = items['data']['partnerData']
  269. if item.get('type') != 'video':
  270. entries = []
  271. cover = item.get('cover') or {}
  272. if cover.get('type') == 'yvideo':
  273. cover_url = cover.get('url')
  274. if cover_url:
  275. entries.append(self.url_result(
  276. cover_url, 'Yahoo', cover.get('uuid')))
  277. for e in (item.get('body') or []):
  278. if e.get('type') == 'videoIframe':
  279. iframe_url = e.get('url')
  280. if iframe_url:
  281. entries.append(self.url_result(iframe_url))
  282. if item.get('type') == 'storywithleadvideo':
  283. iframe_url = try_get(item, lambda x: x['meta']['player']['url'])
  284. if iframe_url:
  285. entries.append(self.url_result(iframe_url))
  286. else:
  287. self.report_warning("Yahoo didn't provide an iframe url for this storywithleadvideo")
  288. if items.get('markup'):
  289. entries.extend(
  290. self.url_result(yt_url) for yt_url in YoutubeIE._extract_embed_urls(url, items['markup']))
  291. return self.playlist_result(
  292. entries, item.get('uuid'),
  293. item.get('title'), item.get('summary'))
  294. info = self._extract_yahoo_video(item['uuid'], country)
  295. info['display_id'] = display_id
  296. return info
  297. class YahooSearchIE(SearchInfoExtractor):
  298. IE_DESC = 'Yahoo screen search'
  299. _MAX_RESULTS = 1000
  300. IE_NAME = 'screen.yahoo:search'
  301. _SEARCH_KEY = 'yvsearch'
  302. def _search_results(self, query):
  303. for pagenum in itertools.count(0):
  304. result_url = f'http://video.search.yahoo.com/search/?p={urllib.parse.quote_plus(query)}&fr=screen&o=js&gs=0&b={pagenum * 30}'
  305. info = self._download_json(result_url, query,
  306. note='Downloading results page ' + str(pagenum + 1))
  307. yield from (self.url_result(result['rurl']) for result in info['results'])
  308. if info['m']['last'] >= info['m']['total'] - 1:
  309. break
  310. class YahooJapanNewsIE(InfoExtractor):
  311. IE_NAME = 'yahoo:japannews'
  312. IE_DESC = 'Yahoo! Japan News'
  313. _VALID_URL = r'https?://news\.yahoo\.co\.jp/(?:articles|feature)/(?P<id>[a-zA-Z0-9]+)'
  314. _GEO_COUNTRIES = ['JP']
  315. _TESTS = [{
  316. 'url': 'https://news.yahoo.co.jp/articles/a70fe3a064f1cfec937e2252c7fc6c1ba3201c0e',
  317. 'info_dict': {
  318. 'id': 'a70fe3a064f1cfec937e2252c7fc6c1ba3201c0e',
  319. 'ext': 'mp4',
  320. 'title': '【独自】安倍元総理「国葬」中止求め“脅迫メール”…「子ども誘拐」“送信者”を追跡',
  321. 'description': 'md5:1c06974575f930f692d8696fbcfdc546',
  322. 'thumbnail': r're:https://.+',
  323. },
  324. 'params': {
  325. 'skip_download': True,
  326. },
  327. }, {
  328. 'url': 'https://news.yahoo.co.jp/feature/1356',
  329. 'only_matching': True,
  330. }]
  331. def _extract_formats(self, json_data, content_id):
  332. formats = []
  333. for vid in traverse_obj(json_data, ('ResultSet', 'Result', ..., 'VideoUrlSet', 'VideoUrl', ...)) or []:
  334. delivery = vid.get('delivery')
  335. url = url_or_none(vid.get('Url'))
  336. if not delivery or not url:
  337. continue
  338. elif delivery == 'hls':
  339. formats.extend(
  340. self._extract_m3u8_formats(
  341. url, content_id, 'mp4', 'm3u8_native',
  342. m3u8_id='hls', fatal=False))
  343. else:
  344. bitrate = int_or_none(vid.get('bitrate'))
  345. formats.append({
  346. 'url': url,
  347. 'format_id': join_nonempty('http', bitrate),
  348. 'height': int_or_none(vid.get('height')),
  349. 'width': int_or_none(vid.get('width')),
  350. 'tbr': bitrate,
  351. })
  352. self._remove_duplicate_formats(formats)
  353. return formats
  354. def _real_extract(self, url):
  355. video_id = self._match_id(url)
  356. webpage = self._download_webpage(url, video_id)
  357. preloaded_state = self._search_json(r'__PRELOADED_STATE__\s*=', webpage, 'preloaded state', video_id)
  358. content_id = traverse_obj(
  359. preloaded_state, ('articleDetail', 'paragraphs', ..., 'objectItems', ..., 'video', 'vid'),
  360. get_all=False, expected_type=int)
  361. if content_id is None:
  362. raise ExtractorError('This article does not contain a video', expected=True)
  363. HOST = 'news.yahoo.co.jp'
  364. space_id = traverse_obj(preloaded_state, ('pageData', 'spaceId'), expected_type=str)
  365. json_data = self._download_json(
  366. f'https://feapi-yvpub.yahooapis.jp/v1/content/{content_id}',
  367. video_id, query={
  368. 'appid': 'dj0zaiZpPVZMTVFJR0FwZWpiMyZzPWNvbnN1bWVyc2VjcmV0Jng9YjU-',
  369. 'output': 'json',
  370. 'domain': HOST,
  371. 'ak': hashlib.md5('_'.join((space_id, HOST)).encode()).hexdigest() if space_id else '',
  372. 'device_type': '1100',
  373. })
  374. title = (
  375. traverse_obj(preloaded_state,
  376. ('articleDetail', 'headline'), ('pageData', 'pageParam', 'title'),
  377. expected_type=str)
  378. or self._html_search_meta(('og:title', 'twitter:title'), webpage, 'title', default=None)
  379. or self._html_extract_title(webpage))
  380. description = (
  381. traverse_obj(preloaded_state, ('pageData', 'description'), expected_type=str)
  382. or self._html_search_meta(
  383. ('og:description', 'description', 'twitter:description'),
  384. webpage, 'description', default=None))
  385. thumbnail = (
  386. traverse_obj(preloaded_state, ('pageData', 'ogpImage'), expected_type=str)
  387. or self._og_search_thumbnail(webpage, default=None)
  388. or self._html_search_meta('twitter:image', webpage, 'thumbnail', default=None))
  389. return {
  390. 'id': video_id,
  391. 'title': title,
  392. 'description': description,
  393. 'thumbnail': thumbnail,
  394. 'formats': self._extract_formats(json_data, video_id),
  395. }