|
1
|
|
|
#!/usr/bin/python |
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
|
|
from matplotlib.offsetbox import OffsetImage, AnnotationBbox |
|
4
|
|
|
from matplotlib.patches import PathPatch |
|
5
|
|
|
from telegram.ext import CommandHandler |
|
6
|
|
|
import matplotlib.cbook as cbook |
|
7
|
|
|
from telegram import ChatAction |
|
8
|
|
|
import matplotlib.pyplot as plt |
|
9
|
|
|
from datetime import datetime |
|
10
|
|
|
from matplotlib import style |
|
11
|
|
|
import sqlite3 |
|
12
|
|
|
import os |
|
13
|
|
|
|
|
14
|
|
|
style.use("fivethirtyeight") |
|
15
|
|
|
cs = ["#1f77b4", "#aec7e8", "#ff7f0e", "#ffbb78", "#2ca02c", |
|
16
|
|
|
"#98df8a", "#d62728", "#ff9896", "#9467bd", "#c5b0d5", |
|
17
|
|
|
"#8c564b", "#c49c94", "#e377c2", "#f7b6d2", "#7f7f7f", |
|
18
|
|
|
"#c7c7c7", "#bcbd22", "#dbdb8d", "#17becf", "#9edae5"] |
|
19
|
|
|
my_dpi = 100 |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
def module_init(gd): |
|
23
|
|
|
global c, conn, path, graph_logo |
|
24
|
|
|
path = gd.config["path"] |
|
25
|
|
|
db_path = gd.config["db_path"] |
|
26
|
|
|
graph_logo = gd.config["graph_logo"] |
|
27
|
|
|
commands_activity = gd.config["commands_activity"] |
|
28
|
|
|
commands_usage = gd.config["commands_usage"] |
|
29
|
|
|
for command in commands_activity: |
|
30
|
|
|
gd.dp.add_handler(CommandHandler(command, activity, pass_args=True)) |
|
31
|
|
|
for command in commands_usage: |
|
32
|
|
|
gd.dp.add_handler(CommandHandler(command, usage, pass_args=True)) |
|
33
|
|
|
conn = sqlite3.connect(db_path+"rikka.db", check_same_thread=False) |
|
34
|
|
|
c = conn.cursor() |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
def usage(bot, update, args): |
|
38
|
|
|
func_name = "usage" |
|
39
|
|
|
current_time = datetime.strftime(datetime.now(), "%d.%m.%Y %H:%M:%S") |
|
40
|
|
|
if "all" in "".join(args): |
|
41
|
|
|
chat_mode = "all" |
|
42
|
|
|
else: |
|
43
|
|
|
chat_mode = "local" |
|
44
|
|
|
labels, counts, graph_title = usage_settings(chat_mode, update, func_name) |
|
45
|
|
|
plot(update, labels, counts, graph_title) |
|
46
|
|
|
print(current_time, ">", "/usage", ">", update.message.from_user.username) |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
def activity(bot, update, args): |
|
50
|
|
|
func_name = "activity" |
|
51
|
|
|
current_time = datetime.strftime(datetime.now(), "%d.%m.%Y %H:%M:%S") |
|
52
|
|
|
chat_mode = "local" |
|
53
|
|
|
labels, counts, graph_title = usage_settings(chat_mode, update, func_name) |
|
54
|
|
|
plot(update, labels, counts, graph_title) |
|
55
|
|
|
print(current_time, ">", "/activity", ">", update.message.from_user.username) |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
def usage_settings(chat_mode, update, func_name): |
|
59
|
|
|
chat_id = update.message.chat.id |
|
60
|
|
|
if update.message.chat.title is not None: |
|
61
|
|
|
chat_title = update.message.chat.title |
|
62
|
|
|
else: |
|
63
|
|
|
chat_title = "this chat" |
|
64
|
|
|
if func_name is "usage": |
|
65
|
|
|
title_mode = "used commands" |
|
66
|
|
|
if chat_mode == "local": |
|
67
|
|
|
c.execute("SELECT command, COUNT(*) FROM commands WHERE chat_id = %s GROUP BY command" % (chat_id)) |
|
68
|
|
|
title_chat = chat_title |
|
69
|
|
|
elif chat_mode == "all": |
|
70
|
|
|
c.execute("SELECT command, COUNT(*) FROM commands GROUP BY command") |
|
71
|
|
|
title_chat = "all chats" |
|
72
|
|
|
elif func_name is "activity": |
|
73
|
|
|
title_mode = "active bot users" |
|
74
|
|
|
c.execute("SELECT user, COUNT(*) FROM commands WHERE chat_id = %s GROUP BY user" % (chat_id)) |
|
75
|
|
|
title_chat = chat_title |
|
76
|
|
|
r = c.fetchall() |
|
77
|
|
|
items = [] |
|
78
|
|
|
counts = [] |
|
79
|
|
|
for i in r: |
|
80
|
|
|
items.append(i[0]) |
|
81
|
|
|
counts.append(i[1]) |
|
82
|
|
|
graph_title = "Most "+title_mode+" in "+title_chat |
|
83
|
|
|
return items, counts, graph_title |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
def plot(update, labels, counts, graph_title): |
|
87
|
|
|
update.message.chat.send_action(ChatAction.UPLOAD_PHOTO) |
|
88
|
|
|
chat_id = update.message.chat.id |
|
89
|
|
|
_, ax = plt.subplots(figsize=(1000/my_dpi, 1000/my_dpi)) |
|
90
|
|
|
pie, _, _ = ax.pie(counts, radius=1.6, labels=labels, autopct="%1.0f%%", pctdistance=0.8, labeldistance=1.05, shadow=False, colors=cs) |
|
91
|
|
|
plt.setp(pie, edgecolor='w', zorder=1) |
|
92
|
|
|
pie_logo = ax.pie(["1"], radius=1) |
|
93
|
|
|
plt.setp(pie_logo, zorder=-10) |
|
94
|
|
|
wedge = pie_logo[0][0] |
|
95
|
|
|
logo_path = os.path.abspath(graph_logo) |
|
96
|
|
|
image_file = cbook.get_sample_data(logo_path, asfileobj=False) |
|
97
|
|
|
image = plt.imread(image_file) |
|
98
|
|
|
wedge_path = wedge.get_path() |
|
99
|
|
|
patch = PathPatch(wedge_path, facecolor="w") |
|
100
|
|
|
ax.add_patch(patch) |
|
101
|
|
|
imagebox = OffsetImage(image, zoom=0.73, interpolation="lanczos", clip_path=patch, zorder=-10) |
|
102
|
|
|
ab = AnnotationBbox(imagebox, (0, 0), xycoords="data", pad=0, frameon=False) |
|
103
|
|
|
ax.add_artist(ab) |
|
104
|
|
|
ax.axis("equal") |
|
105
|
|
|
plt.title(graph_title) |
|
106
|
|
|
plt.tight_layout() |
|
107
|
|
|
graph_filename = path + str(chat_id) + "-graph.png" |
|
108
|
|
|
plt.savefig(graph_filename, format="png", bbox_inches="tight", pad_inches=0.2, dpi=my_dpi, facecolor="w") |
|
109
|
|
|
with open(graph_filename, "rb") as f: |
|
110
|
|
|
update.message.reply_photo(f) |
|
111
|
|
|
|