This commit is contained in:
Hell13Cat
2026-06-02 00:15:38 +03:00
parent 2a103d246b
commit 627b374b7f
8 changed files with 436 additions and 84 deletions
+8 -4
View File
@@ -3,19 +3,22 @@ from flask import Flask, render_template, jsonify, send_from_directory, render_t
import json
import requests
from pathlib import Path
import traceback
import os
CONFIG_ROOT = json.load(open("config.json", "r", encoding="utf-8"))
CONFIG_ROOT_S = json.load(open("config_gb.json", "r", encoding="utf-8"))
CONFIG_HOST = CONFIG_ROOT["server"]["host"]
CONFIG_PORT = CONFIG_ROOT["server"]["port"]
PAGINATION_COUNT = CONFIG_ROOT["server"]["count_page"]
GB_ID = CONFIG_ROOT_S["gb"]["id"]
GB_HASH = CONFIG_ROOT_S["gb"]["hash"]
GB_HEADERS = CONFIG_ROOT["gb"]["headers"]
FILE_FOLDERS = CONFIG_ROOT["file"]["repos"]
FILE_DW_FOLDERS = CONFIG_ROOT["file"]["dw"]
THUMB_FOLDER = "data_test/thumb"
gb = gelbooru.GB(GB_ID, GB_HASH, GB_HEADERS)
gb = gelbooru.GB(GB_ID, GB_HASH, GB_HEADERS, FILE_DW_FOLDERS)
log = logger.Logger()
database = db.DB(True)
app = Flask(__name__)
@@ -58,7 +61,7 @@ def dw_api():
image_id = data["id"]
try:
data_get, url_file, file_name = gb.get_from_id(image_id)
if os.path.exists("dw/"+file_name):
if database.check_uni(data_get["md5"]):
message = f"File '{file_name}' already exists. Skipping."
log.send("warning", "all", message)
return jsonify({'status': 'skipped', 'message': message})
@@ -68,7 +71,7 @@ def dw_api():
if 'text/html' in content_type:
log.send("error", "all", "The server returned HTML instead of an image. Bot protection.")
return False
with open("dw/"+file_name, 'wb') as f:
with open(FILE_DW_FOLDERS+"/"+file_name, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
database.add_raw(data_get)
@@ -78,6 +81,7 @@ def dw_api():
log.send("error", "all", error_message)
return jsonify({'status': 'error', 'message': error_message}), 500
except Exception as e:
traceback.print_exc()
error_message = f"Unknown error: {e}"
log.send("error", "all", error_message)
return jsonify({'status': 'error', 'message': error_message}), 500
@@ -115,4 +119,4 @@ def api():
if __name__ == "__main__":
app.run(host="0.0.0.0", port=CONFIG_PORT)
app.run(host=CONFIG_HOST, port=CONFIG_PORT)