|
@@ -1,21 +1,27 @@
|
|
|
import logging
|
|
|
|
|
|
+import cachetools
|
|
|
import yt_dlp
|
|
|
from flask import Flask, request
|
|
|
-from tinydb import TinyDB
|
|
|
|
|
|
logging.basicConfig(format='%(asctime)s [%(levelname)s] %(filename)s:%(lineno)d %(message)s',
|
|
|
datefmt='%Y-%m-%d %H:%M:%S', level=logging.INFO)
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
-db = TinyDB('data.json')
|
|
|
-
|
|
|
|
|
|
def get_key(url: str) -> str:
|
|
|
return f"v2:{url}"
|
|
|
|
|
|
|
|
|
+@cachetools.cached(cache=cachetools.TTLCache(maxsize=100000, ttl=60 * 5))
|
|
|
+def fetch_info(url):
|
|
|
+ logging.info(f"fetching: {url}")
|
|
|
+ with yt_dlp.YoutubeDL({"flat-playlist": True, "extract_flat": "flat-playlist"}) as ydl:
|
|
|
+ info = ydl.extract_info(url, download=False)
|
|
|
+ return info
|
|
|
+
|
|
|
+
|
|
|
def convert_dto(info):
|
|
|
thumbnails = []
|
|
|
for item in info.get("thumbnails", []):
|
|
@@ -74,18 +80,10 @@ def convert_dto(info):
|
|
|
|
|
|
@app.route("/extract", methods=["GET", "POST"])
|
|
|
def extract():
|
|
|
- url = request.json.get("url")
|
|
|
+ url: str = request.json.get("url")
|
|
|
logging.info(f"url: {url}")
|
|
|
- key = get_key(url=url)
|
|
|
- result = db.search(lambda x: x.get("key") == key)
|
|
|
- if result:
|
|
|
- logging.info("find from data.json, so return")
|
|
|
- if result[0].get("result"):
|
|
|
- return convert_dto(info=result[0].get("info"))
|
|
|
- with yt_dlp.YoutubeDL({"flat-playlist": True, "extract_flat": "flat-playlist"}) as ydl:
|
|
|
- info = ydl.extract_info(url, download=False)
|
|
|
- db.insert({"info": info, "key": key})
|
|
|
- return convert_dto(info=info)
|
|
|
+ info = fetch_info(url=url)
|
|
|
+ return convert_dto(info=info)
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|