0.1.9s
This commit is contained in:
+92
-22
@@ -2,13 +2,13 @@ import os
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
def find_file(filename, file_folders):
|
||||
def find_file(filename: str, file_folders: list[str], is_folder: bool = True):
|
||||
for folder in file_folders:
|
||||
folder_path = Path(folder)
|
||||
if not folder_path.exists():
|
||||
continue
|
||||
for file_path in folder_path.rglob(filename):
|
||||
return folder_path
|
||||
return str(folder_path) if is_folder else str(file_path)
|
||||
return None
|
||||
|
||||
def get_size_format(b, factor=1024, suffix="B"):
|
||||
@@ -36,9 +36,10 @@ def get_file_list_size(file_list: list[str]):
|
||||
|
||||
def get_size_edge(type_searсh: bool, file_list: list[str]):
|
||||
if type_searсh:
|
||||
return min(file_list, key=os.path.getsize)
|
||||
result = min(file_list, key=lambda f: Path(f).stat().st_size)
|
||||
else:
|
||||
return max(file_list, key=os.path.getsize)
|
||||
result = max(file_list, key=lambda f: Path(f).stat().st_size)
|
||||
return str(result)
|
||||
|
||||
def calculate_analytic(database, config):
|
||||
all_db = database.get_all()
|
||||
@@ -50,8 +51,9 @@ def calculate_analytic(database, config):
|
||||
type_list_file_path = {"images":[], "videos":[], "gifs":[]}
|
||||
files_not_found = 0
|
||||
for one_post in all_db:
|
||||
file_path = find_file(one_post["local_filename"], config["file"]["repos"])
|
||||
file_path = find_file(one_post["local_filename"], config["file"]["repos"], is_folder=False)
|
||||
if file_path != None:
|
||||
file_path = str(file_path)
|
||||
rating_list_file_path[one_post["rating"]].append(file_path)
|
||||
if one_post["is_gif"] == 1:
|
||||
type_list_file_path["gifs"].append(file_path)
|
||||
@@ -88,11 +90,78 @@ def calculate_analytic(database, config):
|
||||
}
|
||||
}
|
||||
total_file_data_size = file_size_dict["general"]+file_size_dict["questionable"]+file_size_dict["sensitive"]+file_size_dict["explicit"]
|
||||
total_thumb_size = get_dir_size(config[["file"]["thumb"]])
|
||||
total_thumb_size = get_dir_size(config["file"]["thumb"])
|
||||
total_log_size = get_file_list_size(["logs.log"])
|
||||
total_db_size = get_file_list_size([os.path.join("data", "db.sqlite")])
|
||||
weight_stats_smallest = get_size_edge(True, type_list_file_path["gifs"]+type_list_file_path["images"]+type_list_file_path["videos"])
|
||||
weight_stats_largest = get_size_edge(False, type_list_file_path["gifs"]+type_list_file_path["images"]+type_list_file_path["videos"])
|
||||
total_db_size = get_file_list_size([os.path.join(config["file"]["data"], "db.sqlite")])
|
||||
weight_stats_smallest_file = get_size_edge(True, type_list_file_path["gifs"]+type_list_file_path["images"]+type_list_file_path["videos"])
|
||||
weight_stats_largest_file = get_size_edge(False, type_list_file_path["gifs"]+type_list_file_path["images"]+type_list_file_path["videos"])
|
||||
weight_stats_data = {
|
||||
"smallest":{
|
||||
"file":"",
|
||||
"md5":"",
|
||||
"size":0
|
||||
},
|
||||
"largest":{
|
||||
"file":"",
|
||||
"md5":"",
|
||||
"size":0
|
||||
}
|
||||
}
|
||||
resolution_stats_data = {
|
||||
"smallest":{
|
||||
"file":"",
|
||||
"md5":"",
|
||||
"res":{
|
||||
"width":0,
|
||||
"height":0,
|
||||
"total":99999999999999999999999999
|
||||
}
|
||||
},
|
||||
"largest":{
|
||||
"file":"",
|
||||
"md5":"",
|
||||
"res":{
|
||||
"width":0,
|
||||
"height":0,
|
||||
"total":0
|
||||
}
|
||||
}
|
||||
}
|
||||
for one_post_scan in all_db:
|
||||
if one_post_scan["local_filename"] == os.path.basename(weight_stats_smallest_file):
|
||||
weight_stats_data["smallest"] = {
|
||||
"file":os.path.basename(weight_stats_smallest_file),
|
||||
"md5":one_post_scan["md5"],
|
||||
"size":get_file_list_size([weight_stats_smallest_file])
|
||||
}
|
||||
if one_post_scan["local_filename"] == os.path.basename(weight_stats_largest_file):
|
||||
weight_stats_data["largest"] = {
|
||||
"file":os.path.basename(weight_stats_largest_file),
|
||||
"md5":one_post_scan["md5"],
|
||||
"size":get_file_list_size([weight_stats_largest_file])
|
||||
}
|
||||
print(get_file_list_size([os.path.basename(weight_stats_largest_file)]))
|
||||
one_post_res_total = one_post_scan["width"]*one_post_scan["height"]
|
||||
if resolution_stats_data["largest"]["res"]["total"] < one_post_res_total:
|
||||
resolution_stats_data["largest"] = {
|
||||
"file":one_post_scan["local_filename"],
|
||||
"md5":one_post_scan["md5"],
|
||||
"res":{
|
||||
"width":one_post_scan["width"],
|
||||
"height":one_post_scan["height"],
|
||||
"total":one_post_res_total
|
||||
}
|
||||
}
|
||||
if resolution_stats_data["smallest"]["res"]["total"] > one_post_res_total:
|
||||
resolution_stats_data["smallest"] = {
|
||||
"file":one_post_scan["local_filename"],
|
||||
"md5":one_post_scan["md5"],
|
||||
"res":{
|
||||
"width":one_post_scan["width"],
|
||||
"height":one_post_scan["height"],
|
||||
"total":one_post_res_total
|
||||
}
|
||||
}
|
||||
result = {
|
||||
"total_images_analyzed": len(all_db),
|
||||
"files_not_found":files_not_found,
|
||||
@@ -117,29 +186,30 @@ def calculate_analytic(database, config):
|
||||
"ratings_stats": ratings_stats,
|
||||
"resolution_stats": {
|
||||
"largest": {
|
||||
"filename": max_res["filename"],
|
||||
"md5": max_res["md5"],
|
||||
"resolution": f'{max_res["width"]}x{max_res["height"]}'
|
||||
"filename": resolution_stats_data["largest"]["file"],
|
||||
"md5": resolution_stats_data["largest"]["md5"],
|
||||
"resolution": str(resolution_stats_data["largest"]["res"]["width"]) + " x " + str(resolution_stats_data["largest"]["res"]["height"])
|
||||
},
|
||||
"smallest": {
|
||||
"filename": min_res["filename"],
|
||||
"md5": min_res["md5"],
|
||||
"resolution": f'{min_res["width"]}x{min_res["height"]}'
|
||||
"filename": resolution_stats_data["smallest"]["file"],
|
||||
"md5": resolution_stats_data["smallest"]["md5"],
|
||||
"resolution": str(resolution_stats_data["smallest"]["res"]["width"]) + " x " + str(resolution_stats_data["smallest"]["res"]["height"])
|
||||
}
|
||||
},
|
||||
"weight_stats": {
|
||||
"largest": {
|
||||
"filename": weight_stats_largest,
|
||||
"md5": max_weight["md5"],
|
||||
"size_formatted": get_size_format(max_weight["size"])
|
||||
"filename": os.path.basename(weight_stats_data["largest"]["file"]),
|
||||
"md5": weight_stats_data["largest"]["md5"],
|
||||
"size_formatted": get_size_format(weight_stats_data["largest"]["size"])
|
||||
},
|
||||
"smallest": {
|
||||
"filename": weight_stats_smallest,
|
||||
"md5": min_weight["md5"],
|
||||
"size_formatted": get_size_format(min_weight["size"])
|
||||
"filename": os.path.basename(weight_stats_data["smallest"]["file"]),
|
||||
"md5": weight_stats_data["smallest"]["md5"],
|
||||
"size_formatted": get_size_format(weight_stats_data["smallest"]["size"])
|
||||
}
|
||||
},
|
||||
"top_tag":database.get_top_tag("")
|
||||
}
|
||||
with open(os.path.join(config["file"]["data"], "analytic.json"), 'w', encoding='utf-8') as out_f:
|
||||
json.dump(result, out_f, indent=4, ensure_ascii=False)
|
||||
json.dump(result, out_f, indent=4, ensure_ascii=False)
|
||||
return True
|
||||
Reference in New Issue
Block a user