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.

162 lines
5.7 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. ExtractorError,
  6. float_or_none,
  7. int_or_none,
  8. unified_timestamp,
  9. )
  10. class FunnyOrDieIE(InfoExtractor):
  11. _VALID_URL = r'https?://(?:www\.)?funnyordie\.com/(?P<type>embed|articles|videos)/(?P<id>[0-9a-f]+)(?:$|[?#/])'
  12. _TESTS = [{
  13. 'url': 'http://www.funnyordie.com/videos/0732f586d7/heart-shaped-box-literal-video-version',
  14. 'md5': 'bcd81e0c4f26189ee09be362ad6e6ba9',
  15. 'info_dict': {
  16. 'id': '0732f586d7',
  17. 'ext': 'mp4',
  18. 'title': 'Heart-Shaped Box: Literal Video Version',
  19. 'description': 'md5:ea09a01bc9a1c46d9ab696c01747c338',
  20. 'thumbnail': r're:^http:.*\.jpg$',
  21. 'uploader': 'DASjr',
  22. 'timestamp': 1317904928,
  23. 'upload_date': '20111006',
  24. 'duration': 318.3,
  25. },
  26. }, {
  27. 'url': 'http://www.funnyordie.com/embed/e402820827',
  28. 'info_dict': {
  29. 'id': 'e402820827',
  30. 'ext': 'mp4',
  31. 'title': 'Please Use This Song (Jon Lajoie)',
  32. 'description': 'Please use this to sell something. www.jonlajoie.com',
  33. 'thumbnail': r're:^http:.*\.jpg$',
  34. 'timestamp': 1398988800,
  35. 'upload_date': '20140502',
  36. },
  37. 'params': {
  38. 'skip_download': True,
  39. },
  40. }, {
  41. 'url': 'http://www.funnyordie.com/articles/ebf5e34fc8/10-hours-of-walking-in-nyc-as-a-man',
  42. 'only_matching': True,
  43. }]
  44. def _real_extract(self, url):
  45. mobj = re.match(self._VALID_URL, url)
  46. video_id = mobj.group('id')
  47. webpage = self._download_webpage(url, video_id)
  48. links = re.findall(r'<source src="([^"]+/v)[^"]+\.([^"]+)" type=\'video', webpage)
  49. if not links:
  50. raise ExtractorError('No media links available for %s' % video_id)
  51. links.sort(key=lambda link: 1 if link[1] == 'mp4' else 0)
  52. m3u8_url = self._search_regex(
  53. r'<source[^>]+src=(["\'])(?P<url>.+?/master\.m3u8[^"\']*)\1',
  54. webpage, 'm3u8 url', group='url')
  55. formats = []
  56. m3u8_formats = self._extract_m3u8_formats(
  57. m3u8_url, video_id, 'mp4', 'm3u8_native',
  58. m3u8_id='hls', fatal=False)
  59. source_formats = list(filter(
  60. lambda f: f.get('vcodec') != 'none', m3u8_formats))
  61. bitrates = [int(bitrate) for bitrate in re.findall(r'[,/]v(\d+)(?=[,/])', m3u8_url)]
  62. bitrates.sort()
  63. if source_formats:
  64. self._sort_formats(source_formats)
  65. for bitrate, f in zip(bitrates, source_formats or [{}] * len(bitrates)):
  66. for path, ext in links:
  67. ff = f.copy()
  68. if ff:
  69. if ext != 'mp4':
  70. ff = dict(
  71. [(k, v) for k, v in ff.items()
  72. if k in ('height', 'width', 'format_id')])
  73. ff.update({
  74. 'format_id': ff['format_id'].replace('hls', ext),
  75. 'ext': ext,
  76. 'protocol': 'http',
  77. })
  78. else:
  79. ff.update({
  80. 'format_id': '%s-%d' % (ext, bitrate),
  81. 'vbr': bitrate,
  82. })
  83. ff['url'] = self._proto_relative_url(
  84. '%s%d.%s' % (path, bitrate, ext))
  85. formats.append(ff)
  86. self._check_formats(formats, video_id)
  87. formats.extend(m3u8_formats)
  88. self._sort_formats(
  89. formats, field_preference=('height', 'width', 'tbr', 'format_id'))
  90. subtitles = {}
  91. for src, src_lang in re.findall(r'<track kind="captions" src="([^"]+)" srclang="([^"]+)"', webpage):
  92. subtitles[src_lang] = [{
  93. 'ext': src.split('/')[-1],
  94. 'url': 'http://www.funnyordie.com%s' % src,
  95. }]
  96. timestamp = unified_timestamp(self._html_search_meta(
  97. 'uploadDate', webpage, 'timestamp', default=None))
  98. uploader = self._html_search_regex(
  99. r'<h\d[^>]+\bclass=["\']channel-preview-name[^>]+>(.+?)</h',
  100. webpage, 'uploader', default=None)
  101. title, description, thumbnail, duration = [None] * 4
  102. medium = self._parse_json(
  103. self._search_regex(
  104. r'jsonMedium\s*=\s*({.+?});', webpage, 'JSON medium',
  105. default='{}'),
  106. video_id, fatal=False)
  107. if medium:
  108. title = medium.get('title')
  109. duration = float_or_none(medium.get('duration'))
  110. if not timestamp:
  111. timestamp = unified_timestamp(medium.get('publishDate'))
  112. post = self._parse_json(
  113. self._search_regex(
  114. r'fb_post\s*=\s*(\{.*?\});', webpage, 'post details',
  115. default='{}'),
  116. video_id, fatal=False)
  117. if post:
  118. if not title:
  119. title = post.get('name')
  120. description = post.get('description')
  121. thumbnail = post.get('picture')
  122. if not title:
  123. title = self._og_search_title(webpage)
  124. if not description:
  125. description = self._og_search_description(webpage)
  126. if not duration:
  127. duration = int_or_none(self._html_search_meta(
  128. ('video:duration', 'duration'), webpage, 'duration', default=False))
  129. return {
  130. 'id': video_id,
  131. 'title': title,
  132. 'description': description,
  133. 'thumbnail': thumbnail,
  134. 'uploader': uploader,
  135. 'timestamp': timestamp,
  136. 'duration': duration,
  137. 'formats': formats,
  138. 'subtitles': subtitles,
  139. }