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
|
|
|
chat_params = ["local", "all"] |
21
|
|
|
graph_params = ["user", "command"] |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
def module_init(gd): |
25
|
|
|
global c, conn, path, graph_logo |
26
|
|
|
path = gd.config["path"] |
27
|
|
|
db_path = gd.config["db_path"] |
28
|
|
|
graph_logo = gd.config["graph_logo"] |
29
|
|
|
commands = gd.config["commands"] |
30
|
|
|
for command in commands: |
31
|
|
|
gd.dp.add_handler(CommandHandler(command, graph, pass_args=True)) |
32
|
|
|
conn = sqlite3.connect(db_path+"rikka.db", check_same_thread=False) |
33
|
|
|
c = conn.cursor() |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
def graph(bot, update, args): |
37
|
|
|
current_time = datetime.strftime(datetime.now(), "%d.%m.%Y %H:%M:%S") |
38
|
|
|
update.message.chat.send_action(ChatAction.UPLOAD_PHOTO) |
39
|
|
|
chat_id = update.message.chat.id |
40
|
|
|
for i in chat_params: |
41
|
|
|
for k in graph_params: |
42
|
|
|
if i in "".join(args): |
43
|
|
|
chat_mode = i |
44
|
|
|
else: |
45
|
|
|
chat_mode = "local" |
46
|
|
|
if k in "".join(args): |
47
|
|
|
graph_mode = k |
48
|
|
|
else: |
49
|
|
|
graph_mode = "user" |
50
|
|
|
labels, counts, graph_title = get_settings(graph_mode, chat_mode, update) |
|
|
|
|
51
|
|
|
_, ax = plt.subplots(figsize=(1000/my_dpi, 1000/my_dpi)) |
52
|
|
|
pie, _, _ = ax.pie(counts, radius=1.6, labels=labels, autopct="%1.0f%%", pctdistance=0.8, labeldistance=1.05, shadow=False, colors=cs) |
53
|
|
|
plt.setp(pie, edgecolor='w', zorder=1) |
54
|
|
|
pie_logo = ax.pie(["1"], radius=1) |
55
|
|
|
plt.setp(pie_logo, zorder=-10) |
56
|
|
|
wedge = pie_logo[0][0] |
57
|
|
|
logo_path = os.path.abspath(graph_logo) |
58
|
|
|
image_file = cbook.get_sample_data(logo_path, asfileobj=False) |
59
|
|
|
image = plt.imread(image_file) |
60
|
|
|
wedge_path = wedge.get_path() |
61
|
|
|
patch = PathPatch(wedge_path, facecolor="w") |
62
|
|
|
ax.add_patch(patch) |
63
|
|
|
imagebox = OffsetImage(image, zoom=0.73, interpolation="lanczos", clip_path=patch, zorder=-10) |
64
|
|
|
ab = AnnotationBbox(imagebox, (0, 0), xycoords="data", pad=0, frameon=False) |
65
|
|
|
ax.add_artist(ab) |
66
|
|
|
ax.axis("equal") |
67
|
|
|
plt.title(graph_title) |
68
|
|
|
plt.tight_layout() |
69
|
|
|
graph_filename = path + str(chat_id) + "-graph.png" |
70
|
|
|
plt.savefig(graph_filename, format="png", bbox_inches="tight", pad_inches=0.2, dpi=my_dpi, facecolor="w") |
71
|
|
|
with open(graph_filename, "rb") as f: |
72
|
|
|
update.message.reply_photo(f) |
73
|
|
|
print(current_time, ">", "/graph", ">", update.message.from_user.username) |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
def get_settings(graph_mode, chat_mode, update): |
77
|
|
|
chat_id = update.message.chat.id |
78
|
|
|
if update.message.chat.title is not None: |
79
|
|
|
chat_title = update.message.chat.title |
80
|
|
|
else: |
81
|
|
|
chat_title = "this chat" |
82
|
|
|
if graph_mode == "user": |
83
|
|
|
title_mode = "active users" |
84
|
|
|
if chat_mode == "local": |
85
|
|
|
c.execute("SELECT user, COUNT(*) FROM commands WHERE chat_id = %s GROUP BY user" % (chat_id)) |
86
|
|
|
title_chat = chat_title |
87
|
|
|
elif chat_mode == "all": |
88
|
|
|
c.execute("SELECT user, COUNT(*) FROM commands GROUP BY user") |
89
|
|
|
title_chat = "all chats" |
90
|
|
|
elif graph_mode == "command": |
91
|
|
|
title_mode = "used commands" |
92
|
|
|
if chat_mode == "local": |
93
|
|
|
c.execute("SELECT command, COUNT(*) FROM commands WHERE chat_id = %s GROUP BY command" % (chat_id)) |
94
|
|
|
title_chat = chat_title |
95
|
|
|
elif chat_mode == "all": |
96
|
|
|
c.execute("SELECT command, COUNT(*) FROM commands GROUP BY command") |
97
|
|
|
title_chat = "all chats" |
98
|
|
|
else: |
99
|
|
|
return |
100
|
|
|
r = c.fetchall() |
101
|
|
|
items = [] |
102
|
|
|
counts = [] |
103
|
|
|
for i in r: |
104
|
|
|
items.append(i[0]) |
105
|
|
|
counts.append(i[1]) |
106
|
|
|
graph_title = "Most "+title_mode+" in "+title_chat |
107
|
|
|
return items, counts, graph_title |
108
|
|
|
|