info.py 1022 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from utils import json, os
  2. def Video(obj):
  3. return {
  4. "title": obj.get("title"),
  5. "id": obj.get("id"),
  6. "description": obj.get("description")
  7. }
  8. async def get_video(url, path=None):
  9. "Return a list with a dictionary for each video extracted."
  10. filename = "video.json"
  11. if path is not None:
  12. filename = f"{path}/{filename}"
  13. if not os.path_exists(filename):
  14. command = f'youtube-dl --print-json --skip-download "{url}" > "{filename}"'
  15. await os.system(command)
  16. video = await json.load(filename)
  17. return Video(video)
  18. async def get_playlist(url, path=None):
  19. filename = "playlist.json"
  20. if path is not None:
  21. filename = f"{path}/{filename}"
  22. if not os.path_exists(filename):
  23. command = f'youtube-dl --print-json --skip-download "{url}" > {filename}'
  24. await os.system(command)
  25. content = await os.file_read(filename)
  26. videos = []
  27. for line in content.split("\n"):
  28. if line.strip() == "":
  29. continue
  30. video = json.loads(line)
  31. videos.append(Video(video))
  32. return {
  33. "hash": hash,
  34. "videos": videos
  35. }