-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
190 lines (154 loc) · 6.93 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import logging
import os
import sys
from datetime import datetime, timedelta
import json
import argparse
from slack_sdk import WebClient
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger("Stats")
no_days = 30
def start_time() -> float:
old = datetime.today() - timedelta(days=no_days)
return old.timestamp()
class Channel:
def __init__(self, channel_name: str, icon: str, post_message: bool):
self.channel_name = channel_name
self.icon = icon
self.post_message = post_message
class UserStat:
def __init__(self, user_id: str, user_name: str, count: int):
self.user_id = user_id
self.user_name = user_name
self.count = count
class Stats:
def __init__(self):
self.questions = 0
self.active_users = {}
self.replays = 0
self.reactions = 0
def add_message(self, user_id):
if user_id in self.active_users:
self.active_users[user_id] += 1
else:
self.active_users[user_id] = 1
def add_messages(self, user_ids):
for user_id in user_ids:
self.add_message(user_id)
class ChannelStats:
def __init__(self, channel: Channel, stats: Stats, user_stats: list[UserStat]):
self.channel = channel
self.stats = stats
self.user_stats = user_stats
all_channels = [
Channel('android-talks', ':android:', post_message=True),
Channel('ios-talks', ':apple:', post_message=True),
Channel('flutter-talks', ':flutter:', post_message=True),
Channel('frontend-talks', ':spider_web:', post_message=True),
Channel('elixir-talks', ':cloud:', post_message=False),
]
class SlackStatsCalculator:
def __init__(self, client: WebClient, dry_run: bool):
self.client = client
self.dry_run = dry_run
def _find_conversation_id(self, channel_name: str) -> str:
for result in self.client.conversations_list():
for channel in result["channels"]:
if channel["name"] == channel_name:
return channel["id"]
raise Exception(f"Not found conversation {channel_name}")
def _user_stats(self, stats: Stats) -> list[UserStat]:
users = stats.active_users.items()
users = sorted(users, key=lambda user: user[1], reverse=True)
result = []
for user in users:
(user_id, count) = user
if user_id == "USLACKBOT":
logger.debug(f"Skipping slackbot")
continue
user = self.client.users_info(user=user_id)["user"]
if user["is_bot"]:
logger.debug(f"Skipping bot user: {json.dumps(user)}, messages: {count}")
continue
name = user["real_name"]
result.append(UserStat(user_id, name, count))
return result
def _retrieve_messages(self, channel_id: str, oldest: float) -> Stats:
stats = Stats()
for result in self.client.conversations_history(channel=channel_id, oldest=str(oldest)):
conversation_history = result["messages"]
for message in conversation_history:
if "subtype" in message or "bot_id" in message:
logger.debug(f"Skipping message: {message['text']}: {json.dumps(message)}")
else:
logger.debug(f"Counting message: {message['text']}: {json.dumps(message)}")
stats.questions += 1
stats.add_message(message["user"])
if "reply_users" in message:
stats.add_messages(message["reply_users"])
if "reply_count" in message:
stats.replays += message["reply_count"]
if "reactions" in message:
reactions = message["reactions"]
for reaction in reactions:
stats.reactions += reaction["count"]
return stats
def prepare_message(self, root_channel: str, channels_stats: list[ChannelStats]) -> str:
def format_chanel_stats(channel: ChannelStats):
return f"{channel.channel.icon} {channel.stats.questions} tematów, {channel.stats.reactions} emoji, i aż {channel.stats.replays} wiadomości"
channel_stat = next(
channel_stat for channel_stat in channels_stats if channel_stat.channel.channel_name == root_channel)
others_stats = "\n".join(
[f"- {format_chanel_stats(x)}" for x in channels_stats if x.channel.channel_name != root_channel])
message = "\n".join(
[f" - <@{user.user_id}> ({user.count} :heavy_multiplication_x::memo:)" for user in
channel_stat.user_stats[:3]])
return f"""
<!here> *Poniżej :postbox: podsumowanie ostatnich {no_days} dni :calendar::*
Tym razem:
- {format_chanel_stats(channel_stat)}
Dla porównania nasi bracia spłodzili:
{others_stats}
Na {channel_stat.channel.icon} najwięcej napisali:
{message}
_Pamiętajcie, by pisać :writing_hand::skin-tone-5: i dodawać emoji :upside_down_face: pod wiadomościami by piszący nie czuli się samotni :alien:!_
"""
def _get_stats(self, channel: Channel, oldest: float) -> ChannelStats:
conversation_id = self._find_conversation_id(channel.channel_name)
logger.info(f"Found conversation ID: {conversation_id}")
stats = self._retrieve_messages(conversation_id, oldest)
user_stats = self._user_stats(stats)
return ChannelStats(channel, stats, user_stats)
def get_stats(self) -> list[ChannelStats]:
oldest = start_time()
logger.info(f"start: {oldest}")
channels_stats = [self._get_stats(channel, oldest) for channel in all_channels]
return channels_stats
def post(self, root_channel: str, channels_stats: list[ChannelStats]) -> None:
message = self.prepare_message(root_channel, channels_stats)
logger.info(message)
if not self.dry_run:
message_channel = self._find_conversation_id(root_channel)
# overwrite channel for testing
# message_channel = 'GCQ4KBS11'
self.client.chat_postMessage(channel=message_channel, text=message)
def calculate(self) -> None:
channels_stats = self.get_stats()
for channel in all_channels:
if channel.post_message:
self.post(channel.channel_name, channels_stats)
def do_action():
parser = argparse.ArgumentParser(description='Process stats')
parser.add_argument('--dry-run', dest='dry_run', action='store_const',
const=True, default=False,
help='Set the dry run, without posting')
args = parser.parse_args()
auth_token = os.environ.get('SLACK_BOT_TOKEN')
if not auth_token:
sys.stderr.write('You need to provide SLACK_BOT_TOKEN environment variable\n')
sys.exit(1)
client = WebClient(token=auth_token)
calculator = SlackStatsCalculator(client, args.dry_run)
calculator.calculate()
if __name__ == '__main__':
do_action()