peertubetorrent.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env python
  2. import requests
  3. import sys
  4. import argparse
  5. import os
  6. # If you prefer torrents write "torrent"
  7. type = "magnet"
  8. download = "aria2c"
  9. if len(sys.argv) == 1 or sys.argv[1] == "-h" or sys.argv[1] == "--help":
  10. print(f"{sys.argv[0]} PEERTUBE_VIDEO_URL QUALITY")
  11. elif len(sys.argv) == 3:
  12. link = sys.argv[1]
  13. link = link.split("/w/")
  14. instance = link[0]
  15. vidid = link[1]
  16. quality = sys.argv[2]
  17. data = requests.get(f"{instance}/api/v1/videos/{vidid}")
  18. if data.status_code != 200:
  19. print("Could not find this video.")
  20. sys.exit(1)
  21. else:
  22. data = data.json()
  23. qualities = []
  24. for torrent in data["files"]:
  25. label = torrent["resolution"]["label"]
  26. qualities.append(label)
  27. if quality in qualities:
  28. if type == "torrent":
  29. link = data['files'][qualities.index(quality)]['torrentDownloadUrl']
  30. print(f"{label} {link}")
  31. os.system(f"{download} {link}")
  32. elif type == "magnet":
  33. link = data['files'][qualities.index(quality)]['torrentDownloadUrl']
  34. print(f"{label} {link}")
  35. os.system(f"{download} {link}")
  36. else:
  37. print("Could not find this resolution, try 360p, 480p, 1080p")
  38. sys.exit(1)
  39. else:
  40. print("Not enouph arguments, read the help (-h/--help).")