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