-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.py
More file actions
537 lines (467 loc) · 22.4 KB
/
tasks.py
File metadata and controls
537 lines (467 loc) · 22.4 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
from celery import Celery
import time
import vk_api
from vk_api.keyboard import VkKeyboard, VkKeyboardColor
import json
import requests
import tempfile
from flask import request
from pymongo import MongoClient
import pymongo
from flask import Flask
import time
from datetime import datetime, date, timedelta
from datetime import time as timed
import collections
import random
import os
import glob
from pydub import AudioSegment
client = MongoClient(connect=False)
db = client.vkstats2
celery = Celery('tasks', broker='pyamqp://guest@localhost//')
# settings
vk_api_key = os.environ.get('vk_api_key')
webhook_return_code = "2adc56cf"
webhook_group_id = "169308824"
webhook_group_type = "club"
webhook_group_name = "Статистика Конфы"
sounds_folder = "sounds"
admin_id = 10399749
class EventAdder:
def __init__(self):
self.users = db.users
self.users_settings = db.users_settings
self.messages = db.messages
self.debug = False
self.audio_changer = True
def add_user(self, id):
# if users name is not found in db, get one from vk
if id < 0:
return None
user = self.users.find_one({"id": id})
if not user:
user = vk.users.get(user_ids=id)
self.users.insert_one(user[0])
return user[0]
else:
return user
def process_msg(self, message):
user = self.get_user(message["from_id"])
if self.debug:
print(message)
else:
if user:
print("[{}] {} {}: {}".format(message["peer_id"] - 2000000000, user["first_name"], user["last_name"], message.get("text", None)))
else:
print("{}: {}".format(message["from_id"], message.get("text", None)))
if message["peer_id"] == message["from_id"]:
self.message_from_cp(message)
else:
self.message_from_group(message)
self.add_message(message)
def add_message(self, message):
# Get user from userid and save message to db
if str(message["from_id"]).startswith("-"):
return
user = self.get_user(message["from_id"])
message_struct = {
"id":message["conversation_message_id"],
"date":message["date"],
"from_id":message["from_id"],
"group_id":message["peer_id"] - 2000000000,
"first_name":user["first_name"],
"last_name":user["last_name"],
"text":message.get("text", None),
"attachments":message.get("attachments", None),
"fwd_messages":message.get("fwd_messages", None)
}
msg = self.messages.insert_one(message_struct)
def message_from_group(self, message):
user = self.users_settings.find_one({"user_id": message["from_id"]})
if user:
if user["on"] == False:
return
self.process_switches(message)
self.process_stats(message)
if self.messages.find({"group_id":message["peer_id"]-2000000000}).count() < 1:
pass
#self.send_intro(message)
if self.audio_changer:
if message.get("attachments", None):
self.process_audio(message)
if message["attachments"][0]["type"] == "sticker":
if message["attachments"][0]["sticker"]["sticker_id"] == 4452:
pudge_list = ['15']
pudge = random.choice(pudge_list)
audio_msg = upload.audio_message("Pud_ability_devour_{}.mp3".format(pudge), group_id="169308824")
attachm = "doc{}_{}".format(audio_msg[0]["owner_id"], audio_msg[0]["id"])
vk.messages.send(peer_id=message["peer_id"], attachment=attachm)
elif message.get("fwd_messages", None):
if message["fwd_messages"][0].get("attachments", None):
if message["text"] == "":
self.process_audio(message, message["peer_id"])
return True
def message_from_cp(self, message):
# checking if it's first time messaging
user = self.users_settings.find_one({"user_id": message["from_id"]})
if not user:
initial_payload = {
"user_id": message["from_id"],
"state": 1,
"current_bassboost": 0,
"current_pitch": -0.5,
"on" : True
}
msg = self.users_settings.insert_one(initial_payload)
# TODO: send first time msg
processed = self.process_keyboard(message)
user = self.users_settings.find_one({"user_id": message["from_id"]})
msg_to_user = '''
НАСТРОЙКИ ДЛЯ КОНФЫ
on = {}
bassboost = {}
pitch = {}
'''.format(user["on"], user["current_bassboost"], user["current_pitch"])
if not processed:
keyboard = self.keyboard_init(message)
vk.messages.send(peer_id=message["peer_id"], message=msg_to_user, keyboard=keyboard)
else:
vk.messages.send(peer_id=message["peer_id"], message=msg_to_user)
pass
#vk.messages.send(peer_id=message["peer_id"], message=msg_to_user)
def keyboard_init(self, message):
keyboard = VkKeyboard(one_time=False)
keyboard.add_button("on", color=VkKeyboardColor.PRIMARY)
keyboard.add_button("off", color=VkKeyboardColor.PRIMARY)
keyboard.add_line()
keyboard.add_button('bassboost+', color=VkKeyboardColor.POSITIVE)
keyboard.add_button('bassboost-', color=VkKeyboardColor.NEGATIVE)
keyboard.add_line()
keyboard.add_button('pitch+', color=VkKeyboardColor.POSITIVE)
keyboard.add_button('pitch-', color=VkKeyboardColor.NEGATIVE)
return keyboard.get_keyboard()
def process_keyboard(self, message):
text = message["text"]
if text == "bassboost+":
self.change(message, "bassboost", "+")
return True
elif text == "bassboost-":
self.change(message, "bassboost", "-")
return True
elif text == "pitch+":
self.change(message, "pitch", "+")
return True
elif text == "pitch-":
self.change(message, "pitch", "-")
return True
elif text == "on":
self.change(message, "on", "true")
return True
elif text == "off":
self.change(message, "on", "false")
return True
elif text.startswith("msgconf"):
if message["from_id"] != admin_id:
return False
empty, empty2, text_to_conf = text.partition(' ')
vk.messages.send(message=text_to_conf, peer_id=2000000001)
return True
elif text.startswith("audioconf"):
if message["from_id"] != admin_id:
return False
empty, empty2, text_to_conf = text.partition(' ')
group_id = text_to_conf.split()[0]
if group_id == "list":
mp3list = ""
for file in glob.glob("*.mp3"):
mp3list += file + "\n"
vk.messages.send(peer_id=message["peer_id"], message=mp3list)
return True
mp3file = text_to_conf.split()[1]
audio_msg = upload.audio_message("{}.mp3".format(mp3file), group_id="169308824")
attachm = "doc{}_{}".format(audio_msg[0]["owner_id"], audio_msg[0]["id"])
vk.messages.send(peer_id=2000000000+int(group_id), attachment=attachm)
elif text.startswith("rr"):
if message["from_id"] != admin_id:
return False
empty, empty2, text_to_conf = text.partition(' ')
group_id = text_to_conf.split()[0]
text_to_send = text_to_conf.split(' ', 1)[1]
vk.messages.send(peer_id=2000000000+int(group_id), message=text_to_send)
elif text.startswith("listconf"):
all_confs = self.messages.find({"from_id":message["from_id"]}).distinct("group_id")
vk.messages.send(peer_id=message["peer_id"], message=str(all_confs))
return True
else:
return False
def change(self, message, key, value):
user = self.users_settings.find_one({"user_id": message["from_id"]})
defaults = {}
# minimum, maximum, step
min_max = {"bassboost": (-30, 30, 10),
"pitch": (-2, 2, 0.5)}
if key == "bassboost":
current_bassboost = user["current_bassboost"]
mins = min_max["bassboost"]
if value == "+":
if current_bassboost + mins[2] > mins[1]:
self.send_q(message, "already at max")
return
self.users_settings.update_one({
'_id': user["_id"],
}, {
'$set': {
"current_bassboost": current_bassboost + mins[2]
}
})
if value == "-":
if current_bassboost - mins[2] < mins[0]:
self.send_q(message, "already at minimum")
return
self.users_settings.update_one({
'_id': user["_id"],
}, {
'$set': {
"current_bassboost": current_bassboost - mins[2]
}
})
if key == "pitch":
current_pitch = user["current_pitch"]
mins = min_max["pitch"]
if value == "+":
if current_pitch + mins[2] > mins[1]:
self.send_q(message, "already at max")
return
self.users_settings.update_one({
'_id': user["_id"],
}, {
'$set': {
"current_pitch": current_pitch + mins[2]
}
})
if value == "-":
if current_pitch - mins[2] < mins[0]:
self.send_q(message, "already at minimum")
return
self.users_settings.update_one({
'_id': user["_id"],
}, {
'$set': {
"current_pitch": current_pitch - mins[2]
}
})
if key == "on":
if value == "true":
self.users_settings.update_one({
'_id': user["_id"],
}, {
'$set': {
"on": True
}
})
if value == "false":
self.users_settings.update_one({
'_id': user["_id"],
}, {
'$set': {
"on": False
}
})
def send_intro(self, message):
intro_msg = '''Приветствую вас, свежее мясцо!
Pudge может замедлять, ускорять и бассбустить аудиосообщения. Чтобы поменять значения, напишите ему в лс. Также Pudge отзывается на слова, словарь потихоньку будет пополняться со временем.
Ещё Pudge может показывать статистику конфы, для этого напишите "стата". Со временем будут добавляться новые фичи. Если будут какие-то вопросы, пиши в комментариях.
'''
intro_photo = upload.photo_messages("intro_msg.png")
attachm = "photo{}_{}".format(intro_photo[0]["owner_id"], intro_photo[0]["id"])
#print("msg send")
vk.messages.send(peer_id=message["peer_id"], message=intro_msg, attachment=attachm)
def send_audio(self, message, mp3):
audio_msg = upload.audio_message("{}/{}.mp3".format(sounds_folder, mp3), group_id="169308824")
attachm = "doc{}_{}".format(audio_msg[0]["owner_id"], audio_msg[0]["id"])
vk.messages.send(peer_id=message["peer_id"], attachment=attachm)
def send_q(self, message, text):
vk.messages.send(peer_id=message["peer_id"], message=text)
def process_audio(self, message, peer_id=None):
orig_msg = message
if (message["peer_id"] - 2000000000) == 254:
return
if message.get("fwd_messages",None):
message = message["fwd_messages"][0]
if message.get("attachments", None):
if message["attachments"][0]["type"] == "doc":
if message["attachments"][0]["doc"].get("preview", None).get("audio_msg", None):
if message["attachments"][0]["doc"]["preview"]["audio_msg"]["duration"] > 200:
return
audio_message_url = message["attachments"][0]["doc"]["url"]
r = requests.get(audio_message_url)
with open('audio_file.ogg', 'wb') as f:
f.write(r.content)
sound = AudioSegment.from_file("audio_file.ogg", format="ogg")
user_settings = self.users_settings.find_one({"user_id": orig_msg["from_id"]})
if user_settings:
octaves = user_settings["current_pitch"]
sound = sound + user_settings["current_bassboost"]
else:
octaves = -0.5
new_sample_rate = int(sound.frame_rate * (2.0 ** octaves))
lowpitch_sound = sound._spawn(sound.raw_data, overrides={'frame_rate': new_sample_rate})
lowpitch_sound.export("edited.ogg", format="ogg")
audio_msg = upload.audio_message("edited.ogg", group_id="169308824")
attachm = "doc{}_{}".format(audio_msg[0]["owner_id"], audio_msg[0]["id"])
if peer_id == None:
peer_id = message["peer_id"]
try:
vk.messages.send(peer_id=peer_id, attachment=attachm)
except Exception as e:
print("msg send fail: {}".format(e))
def get_user(self, id):
return self.add_user(id)
def process_switches(self, message):
lower = message["text"].lower()
if message["text"] == "debug":
if int(message["from_id"]) == 10399749:
current_time = time.time()
time_for = current_time + 24*60*60
debug_info = '''time_for = {}
current_time = {}
message time = {}'''.format(time_for, current_time, message["date"])
vk.messages.send(peer_id=message["peer_id"], message=debug_info)
if message["text"] == "[club169308824|@bot_groupstats] debug on":
if int(message["from_id"]) == 10399749:
self.debug = True
vk.messages.send(peer_id=message["peer_id"], message="debug on")
if message["text"] == "[club169308824|@bot_groupstats] debug off":
if int(message["from_id"]) == 10399749:
self.debug = False
vk.messages.send(peer_id=message["peer_id"], message="debug off")
if message["text"] == "[club169308824|@bot_groupstats] audio on":
if int(message["from_id"]) == 10399749:
self.audio_changer = True
vk.messages.send(peer_id=message["peer_id"], message="audio on")
if message["text"] == "[club169308824|@bot_groupstats] audio off":
if int(message["from_id"]) == 10399749:
self.audio_changer = False
vk.messages.send(peer_id=message["peer_id"], message="audio off")
if message["text"].startswith("[club169308824|@bot_groupstats] ban"):
if int(message["from_id"]) == 10399749:
ban_id = message["text"].split()[2]
self.users_settings.update_one({
'user_id': ban_id,
}, {
'$set': {
"on": False
}
})
vk.messages.send(peer_id=message["peer_id"], message=" zabanen 😂🤙🏻")
if message["text"].startswith("[club169308824|@bot_groupstats] unban"):
if int(message["from_id"]) == 10399749:
ban_id = message["text"].split()[2]
self.users_settings.update_one({
'user_id': ban_id,
}, {
'$set': {
"on": True
}
})
vk.messages.send(peer_id=message["peer_id"], message=" razbanen 😂🤙🏻")
if "нормально" in lower:
r = random.randint(0, 1)
if r == 0:
audio_msg = self.send_audio(message, "Pud_thanks_02_ru")
else:
audio_msg = self.send_audio(message, "Pud_thanks_02_ru_no")
return
if "чин чопа" in lower:
self.send_audio(message, "Pud_respawn_05")
return
if "ножки" in lower:
self.send_audio(message, "Pud_respawn_06_ru")
return
if "фортнайт" in lower:
self.send_audio(message, "fortnite_earrape")
return
if "хочу жрать" in lower:
self.send_audio(message, "Pud_rare_05_ru")
return
if "бухло" in lower:
self.send_audio(message, "Pud_rival_10_ru")
return
if "шаверма" in lower or "шаурма" in lower:
self.send_audio(message, "Pud_respawn_04_ru")
return
if "мясо" in lower:
r = random.randint(0, 1)
if r == 0:
audio_msg = self.send_audio(message, "Pud_ability_devour_03_ru")
else:
audio_msg = self.send_audio(message, "Pud_rival_01_ru")
return
if "битбокс" in lower:
self.send_audio(message, "d09edca8fe2a06")
return
if "гучи мейн" in lower:
self.send_audio(message, "gimmeloot")
return
def process_stats(self, message):
if message["text"] == "стата":
stats = self.generate_stats(message)
vk.messages.send(peer_id=message["peer_id"], message=stats)
def generate_stats(self, message):
group_id = message["peer_id"] - 2000000000
current_time = int(time.time())
time_for = current_time - 24*60*60
# get start of today
dt = datetime.combine(date.today(), timed(0, 0, 0))
# start of yesterday = one day before start of today
sday_timestamp = int((dt - timedelta(days=1)).timestamp())
# end of yesterday = one second before start of today
eday_timestamp = int((dt - timedelta(seconds=1)).timestamp())
yesterday_messages = db.messages.find({'group_id':group_id, 'date':{'$gt':sday_timestamp, '$lt':eday_timestamp}}).count()
all_messages = db.messages.find({'group_id':group_id, 'date':{'$gt':time_for}}).sort([("from_id", pymongo.DESCENDING)])
result = "💬За последние 24 часа: {}\n За вчерашний день: {}\Написали мне тут:\n".format(all_messages.count(), yesterday_messages)
segregate_users = all_messages.limit(10).distinct("from_id")
top_users = []
for user in segregate_users:
msgs_from_user = db.messages.find({'group_id':group_id, 'date':{'$gt':time_for}, 'from_id':user})
text_list = []
msgs_from_user_text = db.messages.find({'group_id':group_id, 'date':{'$gt':time_for}, 'from_id':user})
for msgs in msgs_from_user_text:
if msgs["text"]:
splitted_msg = msgs["text"].split()
y = [s for s in splitted_msg if len(s) > 3]
text_list.extend(y)
counter = collections.Counter(text_list)
top_users.append((user, msgs_from_user.count(), msgs_from_user, counter.most_common()))
sorted_top_users = sorted(top_users, key=lambda tup: tup[1], reverse=True)
for i in sorted_top_users[:10]:
#print(i[3])
try:
result += "{} {}.: {} сбщ. ({} - {} раз, {} - {} раз, {} - {} раз)\n".format(i[2][0]["first_name"], i[2][0]["last_name"][0], i[1], i[3][0][0][:100], i[3][0][1], i[3][1][0][:100], i[3][1][1], i[3][2][0][:100], i[3][2][1])
except IndexError:
pass
last_hour = current_time - 60*60
top1hour_messages = db.messages.find({'group_id':group_id, 'date':{'$gt':last_hour}}).sort([("from_id", pymongo.DESCENDING)])
if top1hour_messages:
top_users_segregation = top1hour_messages.limit(5).distinct("from_id")
top_users_1hr = []
for user in segregate_users:
msgs_from_user = db.messages.find({'group_id':group_id, 'date':{'$gt':last_hour}, 'from_id':user})
top_users_1hr.append((user, msgs_from_user.count(), msgs_from_user))
sorted_1hr = sorted(top_users_1hr, key=lambda tup: tup[1], reverse=True)
try:
result += "За последний час больше всего написал: {} {} ({} сбщ)".format(sorted_1hr[0][2][0]["first_name"], sorted_1hr[0][2][0]["last_name"], sorted_1hr[0][1])
except Exception:
pass
return result
event = EventAdder()
vk_session = vk_api.VkApi(token=vk_api_key)
vk = vk_session.get_api()
upload = vk_api.VkUpload(vk_session)
@celery.task
def add(content):
if content["type"] == "message_new":
message = content["object"]
event.add_user(message["from_id"])
event.process_msg(message)