This commit is contained in:
机器人总动员 2021-05-08 10:59:27 +08:00
parent 01850f5fba
commit 9c38377809
2 changed files with 106 additions and 79 deletions

View File

@ -3,7 +3,7 @@ import time
import connector
import telegram
from telegram.ext import CommandHandler, MessageHandler, Filters
from config import TOKEN, LIMIT_COUNT, EX_TIME
from config import TOKEN, LIMIT_COUNT
import schedule
from task import add_task
@ -37,6 +37,7 @@ def rank(update, context):
# 限制为群组
if chat_type != "supergroup":
update.message.reply_text("此命令只有在群组中有效")
return
if r.exists("{}_frequency_limit".format(chat_id)):
r.setrange("{}_frequency_limit".format(chat_id), 0, int(r.get("{}_frequency_limit".format(chat_id))) + 1)
else:

80
task.py
View File

@ -1,6 +1,5 @@
import re
import queue
import threading
import jieba
import jieba.posseg as pseg
import wordcloud
@ -76,6 +75,7 @@ def generate(group):
# 生成词云图片
jieba.enable_paddle() # 启动paddle模式。 0.40版之后开始支持,早期版本不支持
chat_content = r.get("{}_chat_content".format(group))
if chat_content is None:
print("数据库中不存在此群组数据")
try:
@ -86,16 +86,35 @@ def generate(group):
except Exception as e:
print("群组: {} | 机器人发送信息失败".format(group))
return
words = pseg.cut(chat_content, use_paddle=True) # paddle模式
word_list = []
try:
words = pseg.cut(chat_content, use_paddle=True) # paddle模式
for word, flag in words:
# print(word + "\t" + flag)
if flag in ["n", "nr", "nz", "PER", "f", "ns", "LOC", "s", "nt", "ORG", "nw"]:
# 判断该词是否有效,不为空格
if re.match(r"^\s+?$", word) is None:
word_list.append(word)
except Exception as e:
print(e)
bot.send_message(
chat_id=group,
text="当前聊天数据量过小,无法生成词云,嗨起来吧~\n"
)
# print(word_list)
# 获取消息总数
total_message_amount = r.get("{}_total_message_amount".format(group))
# print("总发言数: " + total_message_amount)
# 获取发言用户数
user_amount = len(r.hkeys("{}_user_message_amount".format(group)))
# 获取所有用户发言数字典
user_message_amount = r.hgetall("{}_user_message_amount".format(group))
user_message_amount = sorted(user_message_amount.items(), key=lambda kv: (kv[1]), reverse=True)
if len(word_list) > 0:
# 分析高频词
word_amount = {}
# print(word_amount)
@ -111,6 +130,7 @@ def generate(group):
word_amount[word] = 1
# print(word_amount)
word_amount = sorted(word_amount.items(), key=lambda kv: (kv[1]), reverse=True)
if len(word_amount) > 0:
# print("排序后的热词:" + str(word_amount))
hot_word_string = ""
# 默认展示前5位少于5个则全部展示
@ -118,28 +138,6 @@ def generate(group):
hot_word_string += "\t\t\t\t\t\t\t\t" + "`" + str(word_amount[i][0]) + "`" + ": " + str(
word_amount[i][1]) + "\n"
# print(hot_word_string)
# 获取消息总数
total_message_amount = r.get("{}_total_message_amount".format(group))
# print("总发言数: " + total_message_amount)
# 获取发言用户数
user_amount = len(r.hkeys("{}_user_message_amount".format(group)))
# 获取所有用户发言数字典
user_message_amount = r.hgetall("{}_user_message_amount".format(group))
user_message_amount = sorted(user_message_amount.items(), key=lambda kv: (kv[1]), reverse=True)
# print("排序后的用户:" + str(user_message_amount))
top_5_user = ""
# 默认展示前5位少于5个则全部展示
for i in range(min(5, len(user_message_amount))):
top_5_user += "\t\t\t\t\t\t\t\t" + "🎖`" + str(user_message_amount[i][0]) + "`" + " 贡献: " + str(
user_message_amount[i][1]) + "\n"
# print(top_5_user)
string = " ".join(word_list)
# 将string变量传入w的generate()方法,给词云输入文字
w.generate(string)
# 将词云图片导出到当前文件夹
w.to_file('{}_chat_word_cloud.png'.format(group))
bot.send_message(
chat_id=group,
text="🎤 今日话题榜 🎤\n"
@ -156,6 +154,25 @@ def generate(group):
hot_word_string),
parse_mode="Markdown"
)
else:
bot.send_message(
chat_id=group,
text="当前聊天数据量过小,嗨起来吧~"
)
if len(user_message_amount) > 0:
# print("排序后的用户:" + str(user_message_amount))
top_5_user = ""
# 默认展示前5位少于5个则全部展示
for i in range(min(5, len(user_message_amount))):
top_5_user += "\t\t\t\t\t\t\t\t" + "🎖`" + str(user_message_amount[i][0]) + "`" + " 贡献: " + str(
user_message_amount[i][1]) + "\n"
# print(top_5_user)
string = " ".join(word_list)
# 将string变量传入w的generate()方法,给词云输入文字
w.generate(string)
# 将词云图片导出到当前文件夹
w.to_file('{}_chat_word_cloud.png'.format(group))
bot.send_message(
chat_id=group,
@ -170,18 +187,27 @@ def generate(group):
top_5_user),
parse_mode="Markdown"
)
else:
bot.send_message(
chat_id=group,
text="当前聊天数据量过小,嗨起来吧~"
)
try:
bot.send_photo(
chat_id=group,
photo=open("{}_chat_word_cloud.png".format(group), "rb")
)
os.remove("{}_chat_word_cloud.png".format(group))
except Exception as e:
print(e)
bot.send_message(
chat_id=group,
text="当前聊天数据量过小,嗨起来吧~"
)
def flush_redis():
r = connector.get_connection()
r.flushall()
print("已清空数据库")