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.

72 lines
2.5 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': 'mp4',
  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. def _real_extract(self, url):
  24. display_id = self._match_id(url)
  25. webpage = self._download_webpage(url, display_id)
  26. video_id = self._search_regex(
  27. r"player\.loadRevision3Item\('video_id',\s*([0-9]+)\);",
  28. webpage, 'video ID')
  29. all_info = self._download_json(
  30. 'https://testtube.com/api/getPlaylist.json?api_key=ba9c741bce1b9d8e3defcc22193f3651b8867e62&codecs=h264,vp8,theora&video_id=%s' % video_id,
  31. video_id)
  32. info = all_info['items'][0]
  33. formats = []
  34. for vcodec, fdatas in info['media'].items():
  35. for name, fdata in fdatas.items():
  36. formats.append({
  37. 'format_id': '%s-%s' % (vcodec, name),
  38. 'url': fdata['url'],
  39. 'vcodec': vcodec,
  40. 'tbr': fdata.get('bitrate'),
  41. })
  42. self._sort_formats(formats)
  43. duration = int_or_none(info.get('duration'))
  44. images = info.get('images')
  45. thumbnails = None
  46. preference = qualities(['mini', 'small', 'medium', 'large'])
  47. if images:
  48. thumbnails = [{
  49. 'id': thumbnail_id,
  50. 'url': img_url,
  51. 'preference': preference(thumbnail_id)
  52. } for thumbnail_id, img_url in images.items()]
  53. return {
  54. 'id': video_id,
  55. 'display_id': display_id,
  56. 'title': info['title'],
  57. 'description': info.get('summary'),
  58. 'thumbnails': thumbnails,
  59. 'uploader': info.get('show', {}).get('name'),
  60. 'uploader_id': info.get('show', {}).get('slug'),
  61. 'duration': duration,
  62. 'formats': formats,
  63. }