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.

115 lines
4.1 KiB

  1. from __future__ import unicode_literals
  2. import json
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import ExtractorError
  6. class FunnyOrDieIE(InfoExtractor):
  7. _VALID_URL = r'https?://(?:www\.)?funnyordie\.com/(?P<type>embed|articles|videos)/(?P<id>[0-9a-f]+)(?:$|[?#/])'
  8. _TESTS = [{
  9. 'url': 'http://www.funnyordie.com/videos/0732f586d7/heart-shaped-box-literal-video-version',
  10. 'md5': 'bcd81e0c4f26189ee09be362ad6e6ba9',
  11. 'info_dict': {
  12. 'id': '0732f586d7',
  13. 'ext': 'mp4',
  14. 'title': 'Heart-Shaped Box: Literal Video Version',
  15. 'description': 'md5:ea09a01bc9a1c46d9ab696c01747c338',
  16. 'thumbnail': r're:^http:.*\.jpg$',
  17. },
  18. }, {
  19. 'url': 'http://www.funnyordie.com/embed/e402820827',
  20. 'info_dict': {
  21. 'id': 'e402820827',
  22. 'ext': 'mp4',
  23. 'title': 'Please Use This Song (Jon Lajoie)',
  24. 'description': 'Please use this to sell something. www.jonlajoie.com',
  25. 'thumbnail': r're:^http:.*\.jpg$',
  26. },
  27. 'params': {
  28. 'skip_download': True,
  29. },
  30. }, {
  31. 'url': 'http://www.funnyordie.com/articles/ebf5e34fc8/10-hours-of-walking-in-nyc-as-a-man',
  32. 'only_matching': True,
  33. }]
  34. def _real_extract(self, url):
  35. mobj = re.match(self._VALID_URL, url)
  36. video_id = mobj.group('id')
  37. webpage = self._download_webpage(url, video_id)
  38. links = re.findall(r'<source src="([^"]+/v)[^"]+\.([^"]+)" type=\'video', webpage)
  39. if not links:
  40. raise ExtractorError('No media links available for %s' % video_id)
  41. links.sort(key=lambda link: 1 if link[1] == 'mp4' else 0)
  42. m3u8_url = self._search_regex(
  43. r'<source[^>]+src=(["\'])(?P<url>.+?/master\.m3u8[^"\']*)\1',
  44. webpage, 'm3u8 url', group='url')
  45. formats = []
  46. m3u8_formats = self._extract_m3u8_formats(
  47. m3u8_url, video_id, 'mp4', 'm3u8_native',
  48. m3u8_id='hls', fatal=False)
  49. source_formats = list(filter(
  50. lambda f: f.get('vcodec') != 'none' and f.get('resolution') != 'multiple',
  51. m3u8_formats))
  52. bitrates = [int(bitrate) for bitrate in re.findall(r'[,/]v(\d+)(?=[,/])', m3u8_url)]
  53. bitrates.sort()
  54. if source_formats:
  55. self._sort_formats(source_formats)
  56. for bitrate, f in zip(bitrates, source_formats or [{}] * len(bitrates)):
  57. for path, ext in links:
  58. ff = f.copy()
  59. if ff:
  60. if ext != 'mp4':
  61. ff = dict(
  62. [(k, v) for k, v in ff.items()
  63. if k in ('height', 'width', 'format_id')])
  64. ff.update({
  65. 'format_id': ff['format_id'].replace('hls', ext),
  66. 'ext': ext,
  67. 'protocol': 'http',
  68. })
  69. else:
  70. ff.update({
  71. 'format_id': '%s-%d' % (ext, bitrate),
  72. 'vbr': bitrate,
  73. })
  74. ff['url'] = self._proto_relative_url(
  75. '%s%d.%s' % (path, bitrate, ext))
  76. formats.append(ff)
  77. self._check_formats(formats, video_id)
  78. formats.extend(m3u8_formats)
  79. self._sort_formats(
  80. formats, field_preference=('height', 'width', 'tbr', 'format_id'))
  81. subtitles = {}
  82. for src, src_lang in re.findall(r'<track kind="captions" src="([^"]+)" srclang="([^"]+)"', webpage):
  83. subtitles[src_lang] = [{
  84. 'ext': src.split('/')[-1],
  85. 'url': 'http://www.funnyordie.com%s' % src,
  86. }]
  87. post_json = self._search_regex(
  88. r'fb_post\s*=\s*(\{.*?\});', webpage, 'post details')
  89. post = json.loads(post_json)
  90. return {
  91. 'id': video_id,
  92. 'title': post['name'],
  93. 'description': post.get('description'),
  94. 'thumbnail': post.get('picture'),
  95. 'formats': formats,
  96. 'subtitles': subtitles,
  97. }