app.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import logging
  2. import boto3
  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. def fetch_info(url):
  11. logging.info(f"fetching: {url}")
  12. with yt_dlp.YoutubeDL({
  13. "flat-playlist": True,
  14. "extract_flat": "flat-playlist"
  15. }) as ydl:
  16. info = ydl.extract_info(url, download=False)
  17. return info
  18. def convert_dto(info):
  19. thumbnails = []
  20. for item in info.get("thumbnails", []):
  21. if item.get("width"):
  22. thumbnails.append({
  23. "url": item.get("url", ""),
  24. "width": f"{item.get('width', 0)}",
  25. "height": f"{item.get('height', 0)}",
  26. })
  27. formats = []
  28. for item in info.get("formats", []):
  29. if item.get("resolution") != "audio only" and item.get("url") and item.get("acodec") and item.get(
  30. "acodec") != "none" and item.get("vcodec"):
  31. formats.append({
  32. "width": f"{item.get('width', 0)}",
  33. "height": f"{item.get('height', 0)}",
  34. "type": item.get("format", ""),
  35. "quality": f'{item.get("format_note", "")}',
  36. "itag": 0,
  37. "fps": "0",
  38. "bitrate": "0",
  39. "url": item.get("url", ""),
  40. "ext": item.get("ext"),
  41. "vcodec": item.get("vcodec", ""),
  42. "acodec": item.get("acodec", ""),
  43. "vbr": "0",
  44. "abr": "0",
  45. "container": item.get("container")
  46. })
  47. result = {
  48. "code": 200,
  49. "msg": "",
  50. "data": {
  51. "videoDetails": {
  52. "isLiveContent": info.get("is_live", False),
  53. "title": info.get("title", ""),
  54. "thumbnails": thumbnails,
  55. "description": info.get("description", ""),
  56. "lengthSeconds": f"{int(info.get('duration', 0) / 100)}",
  57. "viewCount": f"{info.get('view_count', 0)}",
  58. "keywords": [],
  59. "author": info.get("uploader", info.get("channel", "")),
  60. "channelID": info.get("channel_id", ""),
  61. "recommendInfo": [],
  62. "channelURL": info.get("channel_url", ""),
  63. "videoId": info.get("display_id", "")
  64. },
  65. "streamingData": {
  66. "formats": formats
  67. }
  68. },
  69. "id": "MusicDetailViewModel_detail_url"
  70. }
  71. return result
  72. @app.route("/extract", methods=["GET", "POST"])
  73. def extract():
  74. url: str = request.json.get("url")
  75. logging.info(f"url: {url}")
  76. info = fetch_info(url=url)
  77. return convert_dto(info=info)
  78. @app.route("/health", methods=["GET"])
  79. def health():
  80. return {
  81. "status": 1
  82. }
  83. @app.route("/refresh", methods=["GET"])
  84. def refresh():
  85. autoscaling = boto3.client(
  86. 'autoscaling',
  87. region_name="us-east-1",
  88. aws_access_key_id="AKIA2TBT2JUNG6X3W737",
  89. aws_secret_access_key="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")
  90. response = autoscaling.start_instance_refresh(
  91. AutoScalingGroupName="be-ytb-as",
  92. Strategy="Rolling",
  93. Preferences={
  94. "InstanceWarmup": 30
  95. })
  96. return {
  97. "response": response
  98. }
  99. if __name__ == '__main__':
  100. app.run(host='0.0.0.0', port=80, debug=True)