diff --git a/up2b/__init__.py b/up2b/__init__.py index f3923de..e9213e1 100644 --- a/up2b/__init__.py +++ b/up2b/__init__.py @@ -7,6 +7,7 @@ # @Modified At: 2023-04-19 14:45:15 # @Modified By: thepoy +from pathlib import Path import sys import json import click @@ -22,6 +23,7 @@ CACHE_PATH, CONF_FILE, IMAGE_BEDS_HELP_MESSAGE, + IMAGE_BEDS_NAME, ImageBedCode, ) from up2b.up2b_lib.utils import check_paths, read_conf @@ -198,6 +200,19 @@ def upload( ib.upload_images(*paths) +@cli.command( + short_help="手动添加缓存", help="某些图床禁止上传重复图片,但在上传时又不返回旧图片地址,此时需要你手动获取原图片地址并执行此命令添加到缓存中。" +) +@click.argument( + "image_path", + type=click.Path(exists=True, file_okay=True, dir_okay=False, path_type=Path), +) +@click.argument("url", type=str) +def add_cache(image_path: Path, url: str): + ib = _read_image_bed() + ib.cache.add(image_path, url, IMAGE_BEDS_NAME[ib.image_bed_code]) + + @cli.command( short_help="配置文字水印", help=""" diff --git a/up2b/up2b_lib/cache.py b/up2b/up2b_lib/cache.py index 2aa8fd7..7653e29 100644 --- a/up2b/up2b_lib/cache.py +++ b/up2b/up2b_lib/cache.py @@ -1,11 +1,5 @@ #!/usr/bin/env python3 # -*- coding:utf-8 -*- -# @Author: thepoy -# @Email: thepoy@163.com -# @File Name: cache.py -# @Created At: 2023-02-28 22:16:22 -# @Modified At: 2023-03-06 20:34:24 -# @Modified By: thepoy import sqlite3 import hashlib @@ -152,6 +146,20 @@ def save(self, md5: str, image_bed: str, url: str, force: bool = False): return self.execute(sql, url, md5, image_bed) + def add(self, image_path: Path, url: str, image_bed: str): + md5 = file_md5(image_path) + exists = self.is_exists(md5, image_bed) + if exists: + logger.warning("缓存中已有此图片,无需重复添加") + return + + sql = """ + INSERT INTO cache (url, hash, image_bed) VALUES (?, ?, ?); + """ + self.execute(sql, url, md5, image_bed) + + logger.info("已手动添加缓存", image=image_path, url=url, image_bed=image_bed) + def execute(self, sql: str, *params: Any): c = self.conn.cursor() return c.execute(sql, params) diff --git a/up2b/up2b_lib/log.py b/up2b/up2b_lib/log.py index 519d691..d55b2c4 100644 --- a/up2b/up2b_lib/log.py +++ b/up2b/up2b_lib/log.py @@ -8,7 +8,7 @@ # @Modified By: thepoy from colorful_logger import get_logger, child_logger as cl -from colorful_logger.logger import is_debug +from colorful_logger.logger import is_debug, get_level_from_env from up2b.up2b_lib.constants import CONFIG_FOLDER_PATH log_file_path = None @@ -26,6 +26,7 @@ def child_logger(name: str): logger = get_logger( "up2b", show=show, + level=get_level_from_env(), file_path=log_file_path, add_file_path=False, disable_line_number_filter=True, diff --git a/up2b/up2b_lib/up2b_api/imgtg.py b/up2b/up2b_lib/up2b_api/imgtg.py index 737bdeb..6c33d8d 100644 --- a/up2b/up2b_lib/up2b_api/imgtg.py +++ b/up2b/up2b_lib/up2b_api/imgtg.py @@ -190,11 +190,16 @@ def __upload( "source": (filename, img_buffer, mime_type), } + logger.debug("请求头", header=self.headers) + resp = requests.post( url, headers=self.headers, data=data, files=files, timeout=self.timeout # type: ignore ) resp.encoding = "utf-8" + logger.debug("实际请求头", header=resp.request.headers) + logger.trace("请求体", body=resp.request.body) + try: json_resp = resp.json() except json.decoder.JSONDecodeError: @@ -231,6 +236,8 @@ def __upload( return self.__upload(image, retries + 1) else: + logger.debug("上传出错", error=resp.json()) + logger.error("imgtg 禁止上传重复图片,请检查你之前是否已上传过此图片", image=image) return UploadErrorResponse( resp.status_code, resp.json()["error"]["message"], str(image) )