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.

76 lines
2.2 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import json
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. js_to_json,
  7. ExtractorError,
  8. )
  9. from ..compat import compat_urlparse
  10. class UDNEmbedIE(InfoExtractor):
  11. IE_DESC = '聯合影音'
  12. _PROTOCOL_RELATIVE_VALID_URL = r'//video\.udn\.com/(?:embed|play)/news/(?P<id>\d+)'
  13. _VALID_URL = r'https?:' + _PROTOCOL_RELATIVE_VALID_URL
  14. _TESTS = [{
  15. 'url': 'http://video.udn.com/embed/news/300040',
  16. 'md5': 'de06b4c90b042c128395a88f0384817e',
  17. 'info_dict': {
  18. 'id': '300040',
  19. 'ext': 'mp4',
  20. 'title': '生物老師男變女 全校挺"做自己"',
  21. 'thumbnail': 're:^https?://.*\.jpg$',
  22. }
  23. }, {
  24. 'url': 'https://video.udn.com/embed/news/300040',
  25. 'only_matching': True,
  26. }, {
  27. # From https://video.udn.com/news/303776
  28. 'url': 'https://video.udn.com/play/news/303776',
  29. 'only_matching': True,
  30. }]
  31. def _real_extract(self, url):
  32. video_id = self._match_id(url)
  33. page = self._download_webpage(url, video_id)
  34. options = json.loads(js_to_json(self._html_search_regex(
  35. r'var options\s*=\s*([^;]+);', page, 'video urls dictionary')))
  36. video_urls = options['video']
  37. if video_urls.get('youtube'):
  38. return self.url_result(video_urls.get('youtube'), 'Youtube')
  39. try:
  40. del video_urls['youtube']
  41. except KeyError:
  42. pass
  43. formats = [{
  44. 'url': self._download_webpage(
  45. compat_urlparse.urljoin(url, api_url), video_id,
  46. 'retrieve url for %s video' % video_type),
  47. 'format_id': video_type,
  48. 'preference': 0 if video_type == 'mp4' else -1,
  49. } for video_type, api_url in video_urls.items() if api_url]
  50. if not formats:
  51. raise ExtractorError('No videos found', expected=True)
  52. self._sort_formats(formats)
  53. thumbnail = None
  54. if options.get('gallery') and len(options['gallery']):
  55. thumbnail = options['gallery'][0].get('original')
  56. return {
  57. 'id': video_id,
  58. 'formats': formats,
  59. 'title': options['title'],
  60. 'thumbnail': thumbnail
  61. }