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.

96 lines
3.0 KiB

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