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.

64 lines
1.9 KiB

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