版本比较
QQ Message Fetcher 使用说明
正文
增加 46 行删除 31 行
51 行未改动525354555656575859604 行未改动6566676869707169707172757677737475767778798079818283828485868788898490919293949 行未改动1041051061071021031081091101051111121071131141151161111121131171181191201211221231241251261271281291301311321241331341351271361371381391402 行未改动14314414514614713813914814915014114214315115215315415515615714715815916016116214915016316416516616716816915615717017117217316016117417517617717817918074 行未改动51 行未改动
class MessageFetcher(NcatBotPlugin): name = "message_fetcher" version = "1.0.0"
async def on_load(self): print(f"{self.name} 插件启动成功")
@admin_filter4 行未改动 @param(name="message_seq", help="开始爬取的消息id,不填即为最后一条消息开始爬取") async def fetch_count(self, event: BaseMessageEvent, gid: str, cnt: int = 20, save: bool = True, message_seq: str = 0): print(f"已收到爬取群聊 {gid} 的消息的请求,爬取消息数量为 {cnt},是否保存到本地为 {save},开始爬取的消息 id 为 {message_seq}。") try: msgs = await self.api.get_group_msg_history(group_id = gid, message_seq = int(message_seq), count = cnt, reverseOrder = True) print(msgs)
if save: # 生成时间戳,避免覆盖文件 now = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") txt_all_file = f"group_{gid}_msgs_{now}_all.txt" txt_file = f"group_{gid}_msgs_{now}.txt" await event.reply(f"消息将被保存到:\n文本文件:{txt_file}") raw_msg_file = f"group_{gid}_msgs_{now}_raw.txt" await event.reply(f"消息将被保存到:\n文本文件:{txt_all_file}\n纯消息:{raw_msg_file}")
msgs = await self.api.get_group_msg_history(group_id = gid, message_seq = int(message_seq), count = cnt, reverseOrder = True) # print(msgs)
if save: # 保存为文本文件 with open(txt_file, "w", encoding="utf-8") as f: with open(txt_all_file, "w", encoding="utf-8") as f: f.write(f"爬取群聊 {gid} 的消息,爬取消息数量为 {cnt},是否保存到本地为 {save},开始爬取的消息 id 为 {message_seq}。\n\n") for msg in msgs: f.write(str(msg) + "\n") # 每条消息占一行 f.write(f"time={timestamp_to_readable_format(int(msg.time))}, timestamp={int(msg.time)}, " + str(msg) + "\n") # 每条消息占一行 with open(raw_msg_file, "w", encoding="utf-8") as f: f.write(f"爬取群聊 {gid} 的消息,爬取消息数量为 {cnt},是否保存到本地为 {save},开始爬取的消息 id 为 {message_seq}。\n\n") for msg in msgs: f.write(timestamp_to_readable_format(int(msg.time)) + " " + msg.raw_message + "\n") # 每条消息占一行
print(int(msgs[0].message_id)) # print(int(msgs[0].message_id))
await event.reply("已成功爬取消息") except Exception as e: tb_str = traceback.format_exc()9 行未改动 @option(short_name="s", long_name="save", help="是否保存到本地,默认为真") @param(name="message_seq", help="开始爬取的消息id,不填即为最后一条消息开始爬取") async def fetch_time(self, event: BaseMessageEvent, gid: str, start_ts: int, end_ts: int = 10000000000000000000, save: bool = True, message_seq: str = 0): print(f"收到爬取群聊 {gid} 从 {start_ts} 到 {end_ts} 的消息请求,是否保存到本地为 {save},开始爬取的消息 id 为 {message_seq}。") all_msgs = []
all_msgs_sz = 0 last_seq = message_seq # 从 0 开始抓取 batch_size = 10 # 每次抓取条数,可调整 batch_size = 20 # 每次抓取条数,可调整 next_time_batch_size = batch_size
if save: # 生成时间戳,避免覆盖文件 now = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") txt_file = f"group_{gid}_msgs_{now}.txt" await event.reply(f"消息将被保存到:\n文本文件:{txt_file}") with open(txt_file, "a", encoding="utf-8") as f: txt_all_file = f"group_{gid}_msgs_{now}_all.txt" raw_msg_file = f"group_{gid}_msgs_{now}_raw.txt" await event.reply(f"消息将被保存到:\n文本文件:{txt_all_file}\n纯消息:{raw_msg_file}") with open(txt_all_file, "a", encoding="utf-8") as f: f.write(f"爬取群聊 {gid} 从 {start_ts}({timestamp_to_readable_format(start_ts)}) 到 {"最后一条消息" if end_ts == 10000000000000000000 else f"{end_ts}({timestamp_to_readable_format(end_ts)})"} 的消息,是否保存到本地为 {save},开始爬取的消息 id 为 {message_seq}。\n\n") with open(raw_msg_file, "a", encoding="utf-8") as f: f.write(f"爬取群聊 {gid} 从 {start_ts}({timestamp_to_readable_format(start_ts)}) 到 {"最后一条消息" if end_ts == 10000000000000000000 else f"{end_ts}({timestamp_to_readable_format(end_ts)})"} 的消息,是否保存到本地为 {save},开始爬取的消息 id 为 {message_seq}。\n\n")
try: while True: msgs = await self.api.get_group_msg_history( group_id=gid, message_seq=last_seq, count=next_time_batch_size, reverseOrder=True )
if not msgs: break # 没有更多消息
# 更新 last_seq,防止重复抓取 if int(msgs[0].message_id) == last_seq: await event.reply(f"爬取时出现 bug,尝试爬取连续的 {next_time_batch_size} 条消息时,前后两次爬取都卡在同一条消息了。这可能是由于有较多条连续的戳一戳导致的,现在将尝试增大一次爬取的连续消息数量以趟过这些消息。如果您多次看到这条消息,请终止程序(直接退出终端即可)。") next_time_batch_size += 52 行未改动 last_seq = int(msgs[0].message_id)
# 根据时间戳筛选 filtered = [m for m in msgs if start_ts <= m.time <= end_ts and m.message_id != last_seq] filtered.reverse() lst_all_msgs_sz = len(all_msgs) all_msgs.extend(filtered) lst_all_msgs_sz = all_msgs_sz all_msgs_sz += len(filtered)
print(f"已爬取 {len(all_msgs)} 条消息") if len(all_msgs) // 1000 > lst_all_msgs_sz // 1000: await event.reply(f"已爬取 {len(all_msgs)} 条消息,目前爬取到的最后一条消息的时间为 {timestamp_to_readable_format(int(msgs[0].time))}") # print(f"已爬取 {all_msgs_sz} 条消息") if all_msgs_sz // 1000 > lst_all_msgs_sz // 1000: print(f"已爬取 {all_msgs_sz} 条消息,目前爬取到的最后一条消息的时间为 {timestamp_to_readable_format(int(msgs[0].time))} ") # await event.reply(f"已爬取 {all_msgs_sz} 条消息,目前爬取到的最后一条消息的时间为 {timestamp_to_readable_format(int(msgs[0].time))}")
if save: # 追加写入文本文件 with open(txt_file, "a", encoding="utf-8") as f: with open(txt_all_file, "a", encoding="utf-8") as f: for msg in filtered: f.write(f"time={timestamp_to_readable_format(int(msg.time))}, timestamp={int(msg.time)}, " + str(msg) + "\n") # 每条消息占一行 with open(raw_msg_file, "a", encoding="utf-8") as f: for msg in filtered: f.write(str(msg) + "\n") f.write(timestamp_to_readable_format(int(msg.time)) + " " + msg.raw_message + "\n") # 每条消息占一行
# print(msgs[0].time) # 如果倒序,且最后一条消息小于 start_ts,停止 if int(msgs[0].time) < start_ts: break
print(f"共抓取到 {len(all_msgs)} 条符合条件的消息") await event.reply(f"已成功爬取 {len(all_msgs)} 条消息") print(f"共抓取到 {all_msgs_sz} 条符合条件的消息") await event.reply(f"已成功爬取 {all_msgs_sz} 条消息")
if save: await event.reply(f"消息已保存:\n文本文件:{txt_file}") # await event.reply(f"消息已保存:\n文本文件:{txt_file}") await event.reply(f"消息已保存到:\n文本文件:{txt_all_file}\n纯消息:{raw_msg_file}")
except Exception as e: import traceback tb_str = traceback.format_exc() error_msg = f"[warn] 无法爬取群 {gid} 的消息: \n{tb_str}"74 行未改动