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.

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