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.

70 lines
2.3 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import ExtractorError
  6. class TheOnionIE(InfoExtractor):
  7. _VALID_URL = r'(?x)https?://(?:www\.)?theonion\.com/video/[^,]+,(?P<article_id>[0-9]+)/?'
  8. _TEST = {
  9. 'url': 'http://www.theonion.com/video/man-wearing-mm-jacket-gods-image,36918/',
  10. 'md5': '19eaa9a39cf9b9804d982e654dc791ee',
  11. 'info_dict': {
  12. 'id': '2133',
  13. 'ext': 'mp4',
  14. 'title': 'Man Wearing M&M Jacket Apparently Made In God\'s Image',
  15. 'description': 'md5:cc12448686b5600baae9261d3e180910',
  16. 'thumbnail': 're:^https?://.*\.jpg\?\d+$',
  17. }
  18. }
  19. def _real_extract(self, url):
  20. mobj = re.match(self._VALID_URL, url)
  21. article_id = mobj.group('article_id')
  22. webpage = self._download_webpage(url, article_id)
  23. video_id = self._search_regex(
  24. r'"videoId":\s(\d+),', webpage, 'video ID')
  25. title = self._og_search_title(webpage)
  26. description = self._og_search_description(webpage)
  27. thumbnail = self._og_search_thumbnail(webpage)
  28. sources = re.findall(r'<source src="([^"]+)" type="([^"]+)"', webpage)
  29. if not sources:
  30. raise ExtractorError(
  31. 'No sources found for video %s' % video_id, expected=True)
  32. formats = []
  33. for src, type_ in sources:
  34. if type_ == 'video/mp4':
  35. formats.append({
  36. 'format_id': 'mp4_sd',
  37. 'preference': 1,
  38. 'url': src,
  39. })
  40. elif type_ == 'video/webm':
  41. formats.append({
  42. 'format_id': 'webm_sd',
  43. 'preference': 0,
  44. 'url': src,
  45. })
  46. elif type_ == 'application/x-mpegURL':
  47. formats.extend(
  48. self._extract_m3u8_formats(src, video_id, preference=-1))
  49. else:
  50. self.report_warning(
  51. 'Encountered unexpected format: %s' % type_)
  52. self._sort_formats(formats)
  53. return {
  54. 'id': video_id,
  55. 'title': title,
  56. 'formats': formats,
  57. 'thumbnail': thumbnail,
  58. 'description': description,
  59. }