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.

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