6s
This commit is contained in:
@@ -1,9 +1,19 @@
|
||||
from system_module import db, gelbooru, logger
|
||||
from flask import Flask, render_template
|
||||
from flask import Flask, render_template, jsonify, send_from_directory, render_template_string
|
||||
import json
|
||||
import requests
|
||||
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_PORT = CONFIG_ROOT["port"]
|
||||
GB_ID = CONFIG_ROOT["gb"]["id"]
|
||||
GB_HASH = CONFIG_ROOT["gb"]["hash"]
|
||||
GB_HEADERS = CONFIG_ROOT["gb"]["headers"]
|
||||
|
||||
gb = gelbooru.GB(GB_ID, GB_HASH, GB_HEADERS)
|
||||
log = logger.Logger()
|
||||
database = db.DB()
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
@@ -17,6 +27,39 @@ def analytics():
|
||||
return render_template("analytics.html")
|
||||
|
||||
|
||||
@app.route("/dw_api", methods=['POST'])
|
||||
def dw_api():
|
||||
data = request.get_json()
|
||||
if not data or 'id' not in data:
|
||||
return jsonify({'status': 'error', 'message': 'URL not get'}), 400
|
||||
image_id = data["id"]
|
||||
try:
|
||||
data_get, url_file, file_name = gb.get_from_id(image_id)
|
||||
if os.path.exists("dw/"+file_name):
|
||||
message = f"File '{file_name}' already exists. Skipping."
|
||||
log.send("warning", "all", message)
|
||||
return jsonify({'status': 'skipped', 'message': message})
|
||||
with requests.get(url_file, stream=True, headers=GB_HEADERS, timeout=30) as r:
|
||||
r.raise_for_status()
|
||||
content_type = r.headers.get('Content-Type', '')
|
||||
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:
|
||||
for chunk in r.iter_content(chunk_size=8192):
|
||||
f.write(chunk)
|
||||
database.add_raw(data_get)
|
||||
return jsonify({'status': 'success'})
|
||||
except requests.exceptions.RequestException as e:
|
||||
error_message = f"Network error {image_id}: {e}"
|
||||
log.send("error", "all", error_message)
|
||||
return jsonify({'status': 'error', 'message': error_message}), 500
|
||||
except Exception as e:
|
||||
error_message = f"Unknown error: {e}"
|
||||
log.send("error", "all", error_message)
|
||||
return jsonify({'status': 'error', 'message': error_message}), 500
|
||||
|
||||
|
||||
@app.route("/post/<path:post_id>")
|
||||
def post(post_id):
|
||||
return render_template("post.html")
|
||||
|
||||
Reference in New Issue
Block a user