dtube.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import json
  2. from socket import timeout
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. int_or_none,
  6. parse_iso8601,
  7. )
  8. class DTubeIE(InfoExtractor):
  9. _VALID_URL = r'https?://(?:www\.)?d\.tube/(?:#!/)?v/(?P<uploader_id>[0-9a-z.-]+)/(?P<id>[0-9a-z]{8})'
  10. _TEST = {
  11. 'url': 'https://d.tube/#!/v/broncnutz/x380jtr1',
  12. 'md5': '9f29088fa08d699a7565ee983f56a06e',
  13. 'info_dict': {
  14. 'id': 'x380jtr1',
  15. 'ext': 'mp4',
  16. 'title': 'Lefty 3-Rings is Back Baby!! NCAA Picks',
  17. 'description': 'md5:60be222088183be3a42f196f34235776',
  18. 'uploader_id': 'broncnutz',
  19. 'upload_date': '20190107',
  20. 'timestamp': 1546854054,
  21. },
  22. 'params': {
  23. 'format': '480p',
  24. },
  25. }
  26. def _real_extract(self, url):
  27. uploader_id, video_id = self._match_valid_url(url).groups()
  28. result = self._download_json('https://api.steemit.com/', video_id, data=json.dumps({
  29. 'jsonrpc': '2.0',
  30. 'method': 'get_content',
  31. 'params': [uploader_id, video_id],
  32. }).encode())['result']
  33. metadata = json.loads(result['json_metadata'])
  34. video = metadata['video']
  35. content = video['content']
  36. info = video.get('info', {})
  37. title = info.get('title') or result['title']
  38. def canonical_url(h):
  39. if not h:
  40. return None
  41. return 'https://video.dtube.top/ipfs/' + h
  42. formats = []
  43. for q in ('240', '480', '720', '1080', ''):
  44. video_url = canonical_url(content.get('video%shash' % q))
  45. if not video_url:
  46. continue
  47. format_id = (q + 'p') if q else 'Source'
  48. try:
  49. self.to_screen('%s: Checking %s video format URL' % (video_id, format_id))
  50. self._downloader._opener.open(video_url, timeout=5).close()
  51. except timeout:
  52. self.to_screen(
  53. '%s: %s URL is invalid, skipping' % (video_id, format_id))
  54. continue
  55. formats.append({
  56. 'format_id': format_id,
  57. 'url': video_url,
  58. 'height': int_or_none(q),
  59. 'ext': 'mp4',
  60. })
  61. return {
  62. 'id': video_id,
  63. 'title': title,
  64. 'description': content.get('description'),
  65. 'thumbnail': canonical_url(info.get('snaphash')),
  66. 'tags': content.get('tags') or metadata.get('tags'),
  67. 'duration': info.get('duration'),
  68. 'formats': formats,
  69. 'timestamp': parse_iso8601(result.get('created')),
  70. 'uploader_id': uploader_id,
  71. }