Browse Source

add: cache

Ben 11 months ago
parent
commit
5a8d8412cc
3 changed files with 13 additions and 2 deletions
  1. 11 1
      app.py
  2. 0 0
      data.json
  3. 2 1
      requirements.txt

+ 11 - 1
app.py

@@ -2,13 +2,21 @@ import logging
 
 import yt_dlp
 from flask import Flask, request
+from tinydb import TinyDB
+from tinydb.table import Document
 
 app = Flask(__name__)
 
+db = TinyDB('data.json')
+
 
 @app.route("/extract", methods=["GET", "POST"])
 def extract():
     url = request.json.get("url")
+    result = db.get(doc_id=url)
+    if result:
+        logging.info("find from data.json, so return")
+        return result
     logging.info(f"url: ${url}")
     with yt_dlp.YoutubeDL({"flat-playlist": True, "extract_flat": "flat-playlist"}) as ydl:
         info = ydl.extract_info(url, download=False)
@@ -40,7 +48,7 @@ def extract():
                     "abr": 0,
                     "container": item.get("container")
                 })
-        return {
+        result = {
             "code": 200,
             "msg": "",
             "data": {
@@ -63,6 +71,8 @@ def extract():
                 }
             }
         }
+        db.upsert(Document(value=result, doc_id=url))
+        return result
 
 
 if __name__ == '__main__':

File diff suppressed because it is too large
+ 0 - 0
data.json


+ 2 - 1
requirements.txt

@@ -2,4 +2,5 @@ beautifulsoup4==4.12.2
 Flask==2.3.2
 Flask_Cors==3.0.10
 Requests==2.31.0
-yt_dlp==2024.4.9
+yt_dlp==2024.4.9
+tinydb==4.8.0

Some files were not shown because too many files changed in this diff