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.

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