Skip to content

Commit

Permalink
添加手动添加图片缓存的功能,添加几条日志
Browse files Browse the repository at this point in the history
  • Loading branch information
thep0y committed Jul 23, 2023
1 parent 9989d9a commit 923c138
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
15 changes: 15 additions & 0 deletions up2b/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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="""
Expand Down
20 changes: 14 additions & 6 deletions up2b/up2b_lib/cache.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion up2b/up2b_lib/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions up2b/up2b_lib/up2b_api/imgtg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
)
Expand Down

0 comments on commit 923c138

Please sign in to comment.