You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.5 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. determine_ext,
  7. int_or_none,
  8. js_to_json,
  9. )
  10. from ..compat import compat_urlparse
  11. class UDNEmbedIE(InfoExtractor):
  12. IE_DESC = '聯合影音'
  13. _PROTOCOL_RELATIVE_VALID_URL = r'//video\.udn\.com/(?:embed|play)/news/(?P<id>\d+)'
  14. _VALID_URL = r'https?:' + _PROTOCOL_RELATIVE_VALID_URL
  15. _TESTS = [{
  16. 'url': 'http://video.udn.com/embed/news/300040',
  17. 'info_dict': {
  18. 'id': '300040',
  19. 'ext': 'mp4',
  20. 'title': '生物老師男變女 全校挺"做自己"',
  21. 'thumbnail': r're:^https?://.*\.jpg$',
  22. },
  23. 'params': {
  24. # m3u8 download
  25. 'skip_download': True,
  26. },
  27. 'expected_warnings': ['Failed to parse JSON Expecting value'],
  28. }, {
  29. 'url': 'https://video.udn.com/embed/news/300040',
  30. 'only_matching': True,
  31. }, {
  32. # From https://video.udn.com/news/303776
  33. 'url': 'https://video.udn.com/play/news/303776',
  34. 'only_matching': True,
  35. }]
  36. def _real_extract(self, url):
  37. video_id = self._match_id(url)
  38. page = self._download_webpage(url, video_id)
  39. options_str = self._html_search_regex(
  40. r'var\s+options\s*=\s*([^;]+);', page, 'options')
  41. trans_options_str = js_to_json(options_str)
  42. options = self._parse_json(trans_options_str, 'options', fatal=False) or {}
  43. if options:
  44. video_urls = options['video']
  45. title = options['title']
  46. poster = options.get('poster')
  47. else:
  48. video_urls = self._parse_json(self._html_search_regex(
  49. r'"video"\s*:\s*({.+?})\s*,', trans_options_str, 'video urls'), 'video urls')
  50. title = self._html_search_regex(
  51. r"title\s*:\s*'(.+?)'\s*,", options_str, 'title')
  52. poster = self._html_search_regex(
  53. r"poster\s*:\s*'(.+?)'\s*,", options_str, 'poster', default=None)
  54. if video_urls.get('youtube'):
  55. return self.url_result(video_urls.get('youtube'), 'Youtube')
  56. formats = []
  57. for video_type, api_url in video_urls.items():
  58. if not api_url:
  59. continue
  60. video_url = self._download_webpage(
  61. compat_urlparse.urljoin(url, api_url), video_id,
  62. note='retrieve url for %s video' % video_type)
  63. ext = determine_ext(video_url)
  64. if ext == 'm3u8':
  65. formats.extend(self._extract_m3u8_formats(
  66. video_url, video_id, ext='mp4', m3u8_id='hls'))
  67. elif ext == 'f4m':
  68. formats.extend(self._extract_f4m_formats(
  69. video_url, video_id, f4m_id='hds'))
  70. else:
  71. mobj = re.search(r'_(?P<height>\d+)p_(?P<tbr>\d+)\.mp4', video_url)
  72. a_format = {
  73. 'url': video_url,
  74. # video_type may be 'mp4', which confuses YoutubeDL
  75. 'format_id': 'http-' + video_type,
  76. }
  77. if mobj:
  78. a_format.update({
  79. 'height': int_or_none(mobj.group('height')),
  80. 'tbr': int_or_none(mobj.group('tbr')),
  81. })
  82. formats.append(a_format)
  83. self._sort_formats(formats)
  84. return {
  85. 'id': video_id,
  86. 'formats': formats,
  87. 'title': title,
  88. 'thumbnail': poster,
  89. }