From ebdddb3c1f530b0877b45107038bd4ca554a5914 Mon Sep 17 00:00:00 2001 From: Ai-desu-2333 <61218958+Ai-desu-2333@users.noreply.github.com> Date: Mon, 20 Mar 2023 00:32:01 +0800 Subject: [PATCH] Update Pixiv2Billfish.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.格式化SQL语句,避免特殊字符转义导致的数据库操作问题#6; 2.其他代码格式的小修改 --- Pixiv2Billfish.py | 53 +++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/Pixiv2Billfish.py b/Pixiv2Billfish.py index c3d4343..a98155f 100644 --- a/Pixiv2Billfish.py +++ b/Pixiv2Billfish.py @@ -1,13 +1,13 @@ # -*- encoding: utf-8 -*- -''' +""" @File : Pixiv2Billfish.py -@Time : 2023/02/4 15:13:37 +@Time : 2023/03/19 23:53:37 @Author : Ai-Desu -@Version : 0.1.2 +@Version : 0.1.3 @Desc : 将pixiv插画的tag信息写入到Billfish中\ 使得在Billfish中也能通过标签查找自己喜欢的作品 参考自 @Coder-Sakura 的 pixiv2eagle -''' +""" import json import re import sqlite3 @@ -15,20 +15,20 @@ import time import requests from loguru import logger + # 强制取消警告 -from requests.packages.urllib3.exceptions import InsecureRequestWarning +requests.packages.urllib3.disable_warnings() -requests.packages.urllib3.disable_warnings(InsecureRequestWarning) from thread_pool import ThreadPool, callback # Billfish 数据库目录 # 注意在目录的" "外添加 r -# eg. DB_PATH = r"C:\pictures\.bf\billfish.db" +# e.g. DB_PATH = r"C:\pictures\.bf\billfish.db" DB_PATH = r"billfish.db" # 选择使用代理链接 # useProxies = 1 使用http代理,useProxies = 0 禁用http代理 -# eg. proxies = {'http': 'http://127.0.0.1:7890', 'https': 'http://127.0.0.1:7890'} +# e.g. proxies = {'http': 'http://127.0.0.1:7890', 'https': 'http://127.0.0.1:7890'} useProxies = 0 proxies = {'http': 'http://host:port', 'https': 'http://host:port'} @@ -193,7 +193,7 @@ def get_tags(pid): tag_list = list(set(tag_list)) tag_list_r = [] for i in tag_list: - i = i.replace("'", "''") + # i = i.replace("'", "''") tag_list_r.append(i) return list(set(tag_list_r)) else: @@ -373,7 +373,7 @@ def close_db(self, conn): except Exception as e: time.sleep(0.3) - #识别数据库版本 + # 识别数据库版本 def is_db_ver_3(self): """ 尝试读取数据库 中的 bf_tag_v2 表来判断所使用billfish版本 @@ -383,7 +383,8 @@ def is_db_ver_3(self): if conn: cursor = conn.cursor() try: - row = cursor.execute("SELECT * FROM sqlite_master WHERE type = 'table' AND tbl_name = 'bf_tag_v2';").fetchall() + row = cursor.execute( + "SELECT * FROM sqlite_master WHERE type = 'table' AND tbl_name = 'bf_tag_v2';").fetchall() except Exception as e: logger.error("Exception:{}".format(e)) return self.is_db_ver_3() @@ -391,7 +392,7 @@ def is_db_ver_3(self): if row: return True else: - return False + return False # 从数据库获取文件名 def get_file_name(self): @@ -407,10 +408,10 @@ def get_file_name(self): count = cursor.execute("select count(*) from bf_file").fetchone() count = count[0] row = cursor.execute( - "SELECT id , name FROM bf_file limit " + str(START_FILE_NUM) + "," + str(count)).fetchall() + "SELECT id , name FROM bf_file limit ?,?", (str(START_FILE_NUM), str(count))).fetchall() else: - row = cursor.execute("SELECT id , name FROM bf_file limit " + str(START_FILE_NUM) + "," + str( - END_FILE_NUM)).fetchall() + row = cursor.execute("SELECT id , name FROM bf_file limit ?,?", (str(START_FILE_NUM), str( + END_FILE_NUM))).fetchall() except Exception as e: logger.error("Exception:{}".format(e)) @@ -512,9 +513,9 @@ def write_tag_db(self, prepare_tag, is_v3_db): self.WRITING_DB = 1 for i in prepare_tag: if is_v3_db: - cursor.execute("INSERT INTO bf_tag_v2 (id,name) VALUES ('" + i["id"] + "','" + i["name"] + "')") + cursor.execute("INSERT INTO bf_tag_v2 (id,name) VALUES(?, ?)", (i["id"], i["name"])) else: - cursor.execute("INSERT INTO bf_tag (id,name) VALUES ('" + i["id"] + "','" + i["name"] + "')") + cursor.execute("INSERT INTO bf_tag (id,name) VALUES(?, ?)", (i["id"], i["name"])) conn.commit() self.WRITING_DB = 0 self.close_db(conn) @@ -523,10 +524,10 @@ def write_tag_db(self, prepare_tag, is_v3_db): self.close_db(conn) self.WRITING_DB = 0 logger.info("Exception:{}".format(e)) - return self.write_tag_db(prepare_tag,is_v3_db) + return self.write_tag_db(prepare_tag, is_v3_db) else: time.sleep(0.3) - return self.write_tag_db(prepare_tag,is_v3_db) + return self.write_tag_db(prepare_tag, is_v3_db) # 写入文件标签 def write_tag_join_file_db(self, prepare_tag_join_file): @@ -543,8 +544,7 @@ def write_tag_join_file_db(self, prepare_tag_join_file): self.WRITING_DB = 1 for i in prepare_tag_join_file: cursor.execute( - "INSERT INTO bf_tag_join_file (file_id,tag_id) VALUES ('" + i["file_id"] + "','" + i[ - "tag_id"] + "')") + "INSERT INTO bf_tag_join_file (file_id,tag_id) VALUES (?,?)", (i["file_id"], i["tag_id"])) conn.commit() self.WRITING_DB = 0 self.close_db(conn) @@ -572,8 +572,8 @@ def write_note(self, prepare_note_join_file): try: self.WRITING_DB = 1 for i in prepare_note_join_file: - conn.cursor().execute("INSERT INTO bf_material_userdata (file_id,note,origin) VALUES ('" + str( - i["file_id"]) + "','" + i["note"] + "','" + i["origin"] + "')") + conn.cursor().execute("INSERT INTO bf_material_userdata (file_id,note,origin) VALUES (?,?,?)", + (str(i["file_id"]), i["note"], i["origin"])) conn.commit() self.WRITING_DB = 0 self.close_db(conn) @@ -649,7 +649,8 @@ def get_artists_id(self): if conn: cursor = conn.cursor() try: - row = cursor.execute("SELECT * FROM bf_tag_v2 WHERE name like 'Artist:%' AND (pid is NULL or pid = 0)").fetchall() + row = cursor.execute( + "SELECT * FROM bf_tag_v2 WHERE name like 'Artist:%' AND (pid is NULL or pid = 0)").fetchall() except Exception as e: logger.error("Exception:{}".format(e)) return self.get_artists_id() @@ -676,7 +677,8 @@ def update_artist_tag(self, artistlist): try: self.WRITING_DB = 1 for i in artistlist: - cursor.execute("UPDATE bf_tag_v2 SET name = '" + i["name"] + "' , pid = '" + i["pid"] + "' WHERE id = '" + i["id"] + "'") + cursor.execute( + "UPDATE bf_tag_v2 SET name = ? , pid = ? WHERE id = ? ", (i["name"], i["pid"], i["id"])) conn.commit() self.WRITING_DB = 0 self.close_db(conn) @@ -690,6 +692,7 @@ def update_artist_tag(self, artistlist): time.sleep(0.3) return self.update_artist_tag(artistlist) + class pixiv2Billfish: # 计数 tag_count = 0