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.

90 lines
3.2 KiB

  1. from __future__ import unicode_literals
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. int_or_none,
  5. qualities,
  6. )
  7. class TestTubeIE(InfoExtractor):
  8. _VALID_URL = r'https?://testtube\.com/[^/?#]+/(?P<id>[^/?#]+)'
  9. _TESTS = [{
  10. 'url': 'https://testtube.com/dnews/5-weird-ways-plants-can-eat-animals?utm_source=FB&utm_medium=DNews&utm_campaign=DNewsSocial',
  11. 'info_dict': {
  12. 'id': '60163',
  13. 'display_id': '5-weird-ways-plants-can-eat-animals',
  14. 'duration': 275,
  15. 'ext': 'webm',
  16. 'title': '5 Weird Ways Plants Can Eat Animals',
  17. 'description': 'Why have some plants evolved to eat meat?',
  18. 'thumbnail': 're:^https?://.*\.jpg$',
  19. 'uploader': 'DNews',
  20. 'uploader_id': 'dnews',
  21. },
  22. }, {
  23. 'url': 'https://testtube.com/iflscience/insane-jet-ski-flipping',
  24. 'info_dict': {
  25. 'id': 'fAGfJ4YjVus',
  26. 'ext': 'mp4',
  27. 'title': 'Flipping Jet-Ski Skills | Outrageous Acts of Science',
  28. 'uploader': 'Science Channel',
  29. 'uploader_id': 'ScienceChannel',
  30. 'upload_date': '20150203',
  31. 'description': 'md5:e61374030015bae1d2e22f096d4769d6',
  32. }
  33. }]
  34. def _real_extract(self, url):
  35. display_id = self._match_id(url)
  36. webpage = self._download_webpage(url, display_id)
  37. youtube_url = self._html_search_regex(
  38. r'<iframe[^>]+src="((?:https?:)?//www.youtube.com/embed/[^"]+)"',
  39. webpage, 'youtube iframe', default=None)
  40. if youtube_url:
  41. return self.url_result(youtube_url, 'Youtube', video_id=display_id)
  42. video_id = self._search_regex(
  43. r"player\.loadRevision3Item\('video_id',\s*([0-9]+)\);",
  44. webpage, 'video ID')
  45. all_info = self._download_json(
  46. 'https://testtube.com/api/getPlaylist.json?api_key=ba9c741bce1b9d8e3defcc22193f3651b8867e62&codecs=h264,vp8,theora&video_id=%s' % video_id,
  47. video_id)
  48. info = all_info['items'][0]
  49. formats = []
  50. for vcodec, fdatas in info['media'].items():
  51. for name, fdata in fdatas.items():
  52. formats.append({
  53. 'format_id': '%s-%s' % (vcodec, name),
  54. 'url': fdata['url'],
  55. 'vcodec': vcodec,
  56. 'tbr': fdata.get('bitrate'),
  57. })
  58. self._sort_formats(formats)
  59. duration = int_or_none(info.get('duration'))
  60. images = info.get('images')
  61. thumbnails = None
  62. preference = qualities(['mini', 'small', 'medium', 'large'])
  63. if images:
  64. thumbnails = [{
  65. 'id': thumbnail_id,
  66. 'url': img_url,
  67. 'preference': preference(thumbnail_id)
  68. } for thumbnail_id, img_url in images.items()]
  69. return {
  70. 'id': video_id,
  71. 'display_id': display_id,
  72. 'title': info['title'],
  73. 'description': info.get('summary'),
  74. 'thumbnails': thumbnails,
  75. 'uploader': info.get('show', {}).get('name'),
  76. 'uploader_id': info.get('show', {}).get('slug'),
  77. 'duration': duration,
  78. 'formats': formats,
  79. }