diff --git a/cloud.py b/cloud.py index b7ffb35..12b0951 100644 --- a/cloud.py +++ b/cloud.py @@ -112,54 +112,67 @@ def makeclouds(gid): shutil.move(f"{today}-{gid}.png",load_in_path) else: try: - shutil.move(f"{today}.png",load_in_path) - except: - os.remove(load_in_path+f"\\{today}.png") - shutil.move(f"{today}.png",load_in_path) - - -def makecloudsb(gid): - global loadpath - bot = nonebot.get_bot() + gid = ev['group_id'] + month = int(match.group(1)) + day = int(match.group(2)) + await bot.send(ev,MessageSegment.image(f'file:///{load_in_path}//2021-{month:02}-{day:02}.png')) + +@sv.on_fullmatch('生成今日词云') +async def getciyun(bot, ev: CQEvent): + gid = ev['group_id'] + if not hoshino.priv.check_priv(ev, hoshino.priv.ADMIN): + await bot.send(ev,f"为避免频繁使用刷屏,只能群管理员使用哦~",at_sender = True) + return + await bot.send(ev,message = '正在生成本群今日词云,请耐心等待',at_sender = True) + makeclouds(gid) today = datetime.date.today().__format__('%Y-%m-%d') + await bot.send(ev,MessageSegment.image(f'file:///{load_in_path}//{today}-{gid}.png')) + + +@sv.on_fullmatch('生成昨日词云') +async def getciyunb(bot, ev: CQEvent): + gid = ev['group_id'] + if not hoshino.priv.check_priv(ev, hoshino.priv.ADMIN): + await bot.send(ev,f"为避免频繁使用刷屏,只能群管理员使用哦~",at_sender = True) + return + await bot.send(ev,message = '正在生成本群昨日词云,请耐心等待',at_sender = True) + makecloudsb(gid) yesterday = (datetime.date.today() + datetime.timedelta(-1)).__format__('%Y-%m-%d') - gida = str(gid) - f = open(loadpath + f"\\{yesterday}.log", "r", encoding="utf-8") - f.seek(0) - msg='' - for line in f.readlines(): #删除前缀和自己的发言 - if self_id in line or gida not in line: - continue - try: - o = line.split("的消息: ")[1] - msg += o - except: - pass - msg = re.sub('''[a-zA-Z0-9'!"#$%&\'()*+,-./:;<=>?@,。?★、…【】《》?“”‘'![\\]^_`{|}~\s]+''', "", msg) - msg = re.sub('[\001\002\003\004\005\006\007\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a]+', '', msg) - banword = []#此处为不显示的删除禁词 - ls = jieba.lcut(msg)#制作分词 - stopwords = set() - content = [line.strip() for line in open(load_in_path+f"\\tyc.txt",encoding='utf-8').readlines()] - stopwords.update(content) - txt = " ".join(ls) - w = wordcloud.WordCloud(font_path=load_in_path+f"\\SimHei.ttf",\ - max_words=10000, width=1000, height=700,\ - background_color='white',stopwords=stopwords,\ - relative_scaling=0.5,min_word_length=2,\ - color_func=random_color_func#词汇上限,宽,高,背景颜色去除停用词(tyc.txt),频次与大小相关度,最小词长,调色 - ) - w.generate(txt) - w.to_file(f"{yesterday}.png") - if gid: - try: - shutil.move(f"{yesterday}-{gid}.png",load_in_path) - except: - os.remove(load_in_path+f"\\{yesterday}-{gid}.png") - shutil.move(f"{yesterday}-{gid}.png",load_in_path) + await bot.send(ev,MessageSegment.image(f'file:///{load_in_path}//{yesterday}-{gid}.png')) + +def countFile(dir): + tmp = 0 + for item in os.listdir(dir): + if os.path.isfile(os.path.join(dir, item)): + tmp += 1 + else: + tmp += countFile(os.path.join(dir, item)) + return tmp + +@sv.on_fullmatch('检查词云') +async def checkwordcloud(bot, ev: CQEvent): + image_api = await bot.can_send_image() + image_check = image_api.get('yes') + image_all_num = countFile(str(load_in_path)) + fnnum = image_all_num - 2 + text = f"【发送权限检查】:\n是否能发送图片:{image_check}\n当前已生成的词云图片数:{fnnum}\n词云背景图片:" + if not priv.check_priv(ev, priv.SUPERUSER): + await bot.send(ev, '权限不足。') + return else: - try: - shutil.move(f"{yesterday}.png",load_in_path) - except: - os.remove(load_in_path+f"\\{yesterday}.png") - shutil.move(f"{yesterday}.png",load_in_path) + await bot.send(ev, text) + await bot.send(ev, MessageSegment.image(f'file:///{wd_img}')) + +@sv.on_fullmatch(['清除词云','清理词云']) +async def del_wordcloud(bot, ev: CQEvent): + image_all_num = countFile(str(load_in_path)) + fnnum = image_all_num - 2 + if not priv.check_priv(ev, priv.SUPERUSER): + await bot.send(ev, '权限不足。') + return + else: + if not fnnum == 0: + del_files(load_in_path) + await bot.send(ev, "词云图片清除成功!") + else: + await bot.send(ev, "无需清理") diff --git a/wd.PNG b/wd.PNG new file mode 100644 index 0000000..b6b9184 Binary files /dev/null and b/wd.PNG differ