app.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import logging
  2. import cachetools
  3. import yt_dlp
  4. from flask import Flask, request
  5. logging.basicConfig(format='%(asctime)s [%(levelname)s] %(filename)s:%(lineno)d %(message)s',
  6. datefmt='%Y-%m-%d %H:%M:%S', level=logging.INFO)
  7. app = Flask(__name__)
  8. def get_key(url: str) -> str:
  9. return f"v2:{url}"
  10. @cachetools.cached(cache=cachetools.TTLCache(maxsize=100000, ttl=60 * 5))
  11. def fetch_info(url):
  12. logging.info(f"fetching: {url}")
  13. with yt_dlp.YoutubeDL({"flat-playlist": True, "extract_flat": "flat-playlist"}) as ydl:
  14. info = ydl.extract_info(url, download=False)
  15. return info
  16. def convert_dto(info):
  17. thumbnails = []
  18. for item in info.get("thumbnails", []):
  19. if item.get("width"):
  20. thumbnails.append({
  21. "url": item.get("url", ""),
  22. "width": f"{item.get('width', 0)}",
  23. "height": f"{item.get('height', 0)}",
  24. })
  25. formats = []
  26. for item in info.get("formats", []):
  27. if item.get("resolution") != "audio only" and item.get("url") and item.get("acodec") and item.get(
  28. "acodec") != "none" and item.get("vcodec"):
  29. formats.append({
  30. "width": f"{item.get('width', 0)}",
  31. "height": f"{item.get('height', 0)}",
  32. "type": item.get("format", ""),
  33. "quality": f'{item.get("format_note", "")}',
  34. "itag": 0,
  35. "fps": "0",
  36. "bitrate": "0",
  37. "url": item.get("url", ""),
  38. "ext": item.get("ext"),
  39. "vcodec": item.get("vcodec", ""),
  40. "acodec": item.get("acodec", ""),
  41. "vbr": "0",
  42. "abr": "0",
  43. "container": item.get("container")
  44. })
  45. result = {
  46. "code": 200,
  47. "msg": "",
  48. "data": {
  49. "videoDetails": {
  50. "isLiveContent": info.get("is_live", False),
  51. "title": info.get("title", ""),
  52. "thumbnails": thumbnails,
  53. "description": info.get("description", ""),
  54. "lengthSeconds": f"{int(info.get('duration', 0) / 100)}",
  55. "viewCount": f"{info.get('view_count', 0)}",
  56. "keywords": [],
  57. "author": info.get("uploader", info.get("channel", "")),
  58. "channelID": info.get("channel_id", ""),
  59. "recommendInfo": [],
  60. "channelURL": info.get("channel_url", ""),
  61. "videoId": info.get("display_id", "")
  62. },
  63. "streamingData": {
  64. "formats": formats
  65. }
  66. },
  67. "id": "MusicDetailViewModel_detail_url"
  68. }
  69. return result
  70. @app.route("/extract", methods=["GET", "POST"])
  71. def extract():
  72. url: str = request.json.get("url")
  73. logging.info(f"url: {url}")
  74. info = fetch_info(url=url)
  75. return convert_dto(info=info)
  76. if __name__ == '__main__':
  77. app.run(host='0.0.0.0', port=80, debug=True)