theonion.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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' % (self.IE_NAME, video_id),
  32. expected=True)
  33. formats = []
  34. for src, type_ in sources:
  35. if type_ == 'video/mp4':
  36. formats.append({
  37. 'format_id': 'mp4_sd',
  38. 'preference': 1,
  39. 'url': src,
  40. })
  41. elif type_ == 'video/webm':
  42. formats.append({
  43. 'format_id': 'webm_sd',
  44. 'preference': 0,
  45. 'url': src,
  46. })
  47. elif type_ == 'application/x-mpegURL':
  48. formats.extend(
  49. self._extract_m3u8_formats(src, video_id, preference=-1))
  50. else:
  51. self.report_warning(
  52. 'Encountered unexpected format: %s' % type_)
  53. self._sort_formats(formats)
  54. return {
  55. 'id': video_id,
  56. 'title': title,
  57. 'formats': formats,
  58. 'thumbnail': thumbnail,
  59. 'description': description,
  60. }