-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
137 lines (114 loc) · 4.68 KB
/
main.py
File metadata and controls
137 lines (114 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# encoding: utf-8
import io
import sys
import threading
from queue import Queue
from logging.handlers import TimedRotatingFileHandler
from pathlib import Path
import matplotlib.pyplot as plt
from tqdm import tqdm
from client import Pica,acc
from util import *
#校验config.ini文件是否存在
config_dir = './config/config.ini'
if not os.path.isfile('./config/config.ini'):
raise Exception(f"配置文件`{config_dir}`不存在,请参考`./config/config_backup.ini`进行配置")
# 配置日志
log_folder = './logs'
os.makedirs(log_folder, exist_ok=True)
log_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
# 自定义生成日志文件名的函数
def get_log_filename(name):
return os.path.join(log_folder, f'{name}_{datetime.now().strftime("%Y-%m-%d")}.log')
# create a new TimedRotatingFileHandler
log_handler = TimedRotatingFileHandler(
get_log_filename('runing'),
when='midnight', # 每天轮转一次
interval=1, # 轮转周期为1天
backupCount=int(get_cfg('crawl', 'backup_count', 30)) # 保留最近?天的日志文件
)
log_handler.setLevel(logging.INFO)
log_handler.setFormatter(log_formatter)
log_handler.addFilter(InfoWarningFilter())
# create a new ERROR TimedRotatingFileHandler, only logs ERROR level and above!
error_log_handler = TimedRotatingFileHandler(
get_log_filename('ERROR'),
when='midnight', # 每天轮转一次
interval=1, # 轮转周期为1天
backupCount=int(get_cfg('crawl', 'backup_count', 30)) # 保留最近?天的日志文件
)
error_log_handler.setLevel(logging.ERROR) # 只记录 ERROR 级别及以上的日志
error_log_handler.setFormatter(log_formatter)
logging.basicConfig(
level=logging.INFO,
handlers=[log_handler, error_log_handler],
encoding='utf-8'
)
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf8')
sys.stdout = LoggerRedirect(sys.stdout)
# 登录并打卡
pica_server = Pica()
pica_server.login()
pica_server.punch_in()
# 排行榜的漫画
ranked_comics = pica_server.leaderboard()
# 关键词订阅的漫画
searched_comics = []
# 收藏夹的漫画
favourited_comics = pica_server.my_favourite_all()
print('收藏夹共计%d本漫画' % (len(favourited_comics)), flush=True)
# 打印所有收藏的漫画名字
print('收藏的漫画列表:', flush=True)
for index, comic in enumerate(favourited_comics, 1):
print(f'{index}. {comic["title"]}', flush=True)
# 打印漫画标签
print(f'标签: {comic["tags"]}', flush=True)
# 统计标签出现次数
print("\n正在统计标签出现次数...")
tags_dict = {}
for comic in favourited_comics:
for tag in comic["tags"]:
if tag in tags_dict:
tags_dict[tag] += 1
else:
tags_dict[tag] = 1
# 柱状图生成函数
def generate_bar_chart(data_dict, top_n=20, chart_title="标签统计柱状图",
x_label="出现次数", y_label="标签", fig_size=(12, 8)):
"""生成水平柱状图可视化标签统计结果"""
# 解决中文字体显示问题
plt.rcParams["font.family"] = ["SimHei", "WenQuanYi Micro Hei", "Heiti TC"]
plt.rcParams["axes.unicode_minus"] = False
if not data_dict:
print("警告:标签字典为空,请先填充数据!")
return
# 排序并截取前top_n个
sorted_data = sorted(data_dict.items(), key=lambda x: x[1], reverse=True)[:top_n]
categories, values = zip(*sorted_data)
# 创建柱状图
plt.figure(figsize=fig_size)
bars = plt.barh(categories, values, color="#87CEEB", edgecolor="#4682B4", alpha=0.8)
# 添加数值标签
for bar in bars:
bar_width = bar.get_width()
plt.text(bar_width + max(values)*0.01,
bar.get_y() + bar.get_height()/2,
str(int(bar_width)),
va="center", ha="left", fontsize=10)
# 设置轴标签和标题
plt.xlabel(x_label, fontsize=12, fontweight="bold")
plt.ylabel(y_label, fontsize=12, fontweight="bold")
plt.title(chart_title, fontsize=14, fontweight="bold", pad=20)
# 调整布局
plt.gca().invert_yaxis()
plt.tight_layout()
plt.show()
# 调用函数生成图表
print("\n正在生成标签统计图表...")
username = get_cfg('account', 'username')
generate_bar_chart(
tags_dict,
chart_title=f"{acc}的PicaComic本子标签统计 (共{len(favourited_comics)}本)"
)
print(f"收藏夹漫画共计{len(favourited_comics)}")
print("RUN COMPLETED!")