onionstudios.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. determine_ext,
  7. int_or_none,
  8. float_or_none,
  9. )
  10. class OnionStudiosIE(InfoExtractor):
  11. _VALID_URL = r'https?://(?:www\.)?onionstudios\.com/(?:videos/[^/]+-|embed\?.*\bid=)(?P<id>\d+)(?!-)'
  12. _TESTS = [{
  13. 'url': 'http://www.onionstudios.com/videos/hannibal-charges-forward-stops-for-a-cocktail-2937',
  14. 'md5': 'e49f947c105b8a78a675a0ee1bddedfe',
  15. 'info_dict': {
  16. 'id': '2937',
  17. 'ext': 'mp4',
  18. 'title': 'Hannibal charges forward, stops for a cocktail',
  19. 'thumbnail': 're:^https?://.*\.jpg$',
  20. 'uploader': 'The A.V. Club',
  21. 'uploader_id': 'the-av-club',
  22. },
  23. }, {
  24. 'url': 'http://www.onionstudios.com/embed?id=2855&autoplay=true',
  25. 'only_matching': True,
  26. }]
  27. @staticmethod
  28. def _extract_url(webpage):
  29. mobj = re.search(
  30. r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?onionstudios\.com/embed.+?)\1', webpage)
  31. if mobj:
  32. return mobj.group('url')
  33. def _real_extract(self, url):
  34. video_id = self._match_id(url)
  35. video_data = self._download_json(
  36. 'http://www.onionstudios.com/video/%s.json' % video_id, video_id)
  37. title = video_data['title']
  38. formats = []
  39. for source in video_data.get('sources', []):
  40. source_url = source.get('url')
  41. if not source_url:
  42. continue
  43. content_type = source.get('content_type')
  44. ext = determine_ext(source_url)
  45. if content_type == 'application/x-mpegURL' or ext == 'm3u8':
  46. formats.extend(self._extract_m3u8_formats(
  47. source_url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False))
  48. else:
  49. tbr = int_or_none(source.get('bitrate'))
  50. formats.append({
  51. 'format_id': ext + ('-%d' % tbr if tbr else ''),
  52. 'url': source_url,
  53. 'width': int_or_none(source.get('width')),
  54. 'tbr': tbr,
  55. 'ext': ext,
  56. })
  57. self._sort_formats(formats)
  58. return {
  59. 'id': video_id,
  60. 'title': title,
  61. 'thumbnail': video_data.get('poster_url'),
  62. 'uploader': video_data.get('channel_name'),
  63. 'uploader_id': video_data.get('channel_slug'),
  64. 'duration': float_or_none(video_data.get('duration', 1000)),
  65. 'tags': video_data.get('tags'),
  66. 'formats': formats,
  67. }