6s
This commit is contained in:
+4
-1
@@ -1,7 +1,10 @@
|
||||
# ---> Python
|
||||
# Secure data
|
||||
old/
|
||||
config_gb.json
|
||||
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
old/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
||||
|
||||
+9
-1
@@ -1,3 +1,11 @@
|
||||
{
|
||||
"port":8084
|
||||
"port":8084,
|
||||
"gb":{
|
||||
"id":"",
|
||||
"hash":"",
|
||||
"headers":{
|
||||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
||||
"Referer": "https://gelbooru.com/"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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")
|
||||
|
||||
@@ -14,6 +14,10 @@ class DB:
|
||||
def add(self, data_add):
|
||||
self.db.insert(data_add)
|
||||
|
||||
def add_raw(self, data_add):
|
||||
# self.db.insert(data_add)
|
||||
pass
|
||||
|
||||
def get_all(self):
|
||||
return self.db.all()
|
||||
|
||||
|
||||
@@ -1,12 +1,22 @@
|
||||
import requests
|
||||
from datetime import datetime
|
||||
|
||||
class GB:
|
||||
def __init__(self, api_id, api_hash):
|
||||
def __init__(self, api_id, api_hash, headers):
|
||||
self.api_id = api_id
|
||||
self.api_hash = api_hash
|
||||
self.root_url = "https://gelbooru.com/index.php"
|
||||
self.HEADERS = headers
|
||||
|
||||
def get_from_id(self, id_post):
|
||||
params = {"page": "dapi", "s": "post", "q": "index", "json": 1, "id": id_post, "api_key":self.api_id, "user_id":self.api_hash}
|
||||
response = requests.get(self.root_url, params=params, headers=self.HEADERS, timeout=15)
|
||||
url_file = response.json()["post"][0]["file_url"]
|
||||
file_name = response.json()["post"][0]["image"]
|
||||
if os.path.exists("dw/"+file_name):
|
||||
message = f"Файл '{file_name}' уже существует. Пропускаем."
|
||||
print(message)
|
||||
return response.json()["post"][0], url_file, file_name
|
||||
pass
|
||||
|
||||
def get_from_md5(self, md5):
|
||||
@@ -14,3 +24,13 @@ class GB:
|
||||
|
||||
def get_binary_file(self, id_post):
|
||||
pass
|
||||
|
||||
def gelbooru_date_parse(self, date_str):
|
||||
if not self.date_str:
|
||||
return 0, "Unknown"
|
||||
try:
|
||||
dt = datetime.strptime(self.date_str, "%a %b %d %H:%M:%S %z %Y")
|
||||
return dt.timestamp(), dt.strftime("%Y-%m-%d")
|
||||
except Exception as e:
|
||||
print(f"Ошибка парсинга даты '{self.date_str}': {e}")
|
||||
return 0, "Unknown"
|
||||
@@ -1,13 +1,12 @@
|
||||
import logging
|
||||
|
||||
class Logger:
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
def __init__(self):
|
||||
self.logger_file = logging.getLogger(__name__)
|
||||
self.logger_cli = logging.getLogger(__name__)
|
||||
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
|
||||
|
||||
file_handler = logging.FileHandler("logs/"+self.name+".log", encoding="utf-8")
|
||||
file_handler = logging.FileHandler("logs.log", encoding="utf-8")
|
||||
file_handler.setFormatter(formatter)
|
||||
|
||||
console_handler = logging.StreamHandler()
|
||||
|
||||
Reference in New Issue
Block a user