|
1
|
|
|
#!/usr/bin/python |
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
|
|
from telegram.ext import MessageHandler, Filters |
|
4
|
|
|
from datetime import datetime, timedelta |
|
5
|
|
|
import sqlite3 |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
def module_init(gd): |
|
9
|
|
|
global c, conn |
|
10
|
|
|
path = gd.config["path"] |
|
11
|
|
|
conn = sqlite3.connect(path+"rikka.db", check_same_thread=False) |
|
12
|
|
|
c = conn.cursor() |
|
13
|
|
|
gd.dp.add_handler(MessageHandler(Filters.all, get_chats), group=1) |
|
14
|
|
|
#gd.dp.add_handler(MessageHandler(Filters.all, get_chat_info), group=2) |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
def create_table(name, columns): |
|
18
|
|
|
c.execute("CREATE TABLE IF NOT EXISTS "+name+"("+columns+")") |
|
19
|
|
|
conn.commit() |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
def data_entry(table, entry_columns, values): |
|
23
|
|
|
values_count = ("?, "*len(values))[:-2] |
|
24
|
|
|
c.execute("INSERT INTO "+table+" ("+entry_columns+") VALUES ("+values_count+")", (values)) |
|
25
|
|
|
conn.commit() |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
def check_entry(chat_id, table): |
|
29
|
|
|
c.execute("SELECT chat_id FROM "+table+" WHERE chat_id = %s" %(chat_id)) |
|
30
|
|
|
if c.fetchone() is not None: |
|
31
|
|
|
return True |
|
32
|
|
|
else: |
|
33
|
|
|
return False |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
def delete_old(table, date): |
|
37
|
|
|
span = timedelta(days=7) |
|
38
|
|
|
c.execute("SELECT chat_id FROM "+table) |
|
39
|
|
|
for row in c.fetchall(): |
|
40
|
|
|
c.execute("SELECT date FROM "+table+" WHERE chat_id = %s" %(row)) |
|
41
|
|
|
old_date = c.fetchone()[0] |
|
42
|
|
|
old_date_time = datetime.strptime(old_date, "%d.%m.%Y %H:%M:%S") |
|
43
|
|
|
if date - old_date_time > span: |
|
44
|
|
|
c.execute("DELETE FROM "+table+" WHERE chat_id = %s" %(row)) |
|
45
|
|
|
conn.commit() |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
def get_chat_info(bot, update): |
|
49
|
|
|
if update.callback_query is not None: |
|
50
|
|
|
user_name = update.callback_query.message.chat.username |
|
51
|
|
|
chat_id = update.callback_query.message.chat_id |
|
52
|
|
|
user_id = update.callback_query.from_user.id |
|
53
|
|
|
else: |
|
54
|
|
|
user_name = update.effective_message.from_user.name[1:] |
|
55
|
|
|
chat_id = update.effective_message.chat_id |
|
56
|
|
|
user_id = update.effective_message.from_user.id |
|
57
|
|
|
chat = bot.getChat(chat_id) |
|
58
|
|
|
if chat.type == "private": |
|
59
|
|
|
owner = user_name |
|
60
|
|
|
else: |
|
61
|
|
|
admins = chat.get_administrators(CREATOR = "creator") |
|
62
|
|
|
for admin in admins: |
|
63
|
|
|
if admin.status == "creator": |
|
64
|
|
|
owner = admin.user.username |
|
65
|
|
|
return chat_id, chat.type, chat.title, chat.username, chat.description, chat.get_members_count(), owner, user_id, user_name |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
def get_chats(bot, update): |
|
69
|
|
|
get_message(bot, update) |
|
70
|
|
|
current_time = datetime.strftime(datetime.now(), "%d.%m.%Y %H:%M:%S") |
|
71
|
|
|
current_time_obj = datetime.strptime(current_time, "%d.%m.%Y %H:%M:%S") |
|
72
|
|
|
table_name = "chats" |
|
73
|
|
|
creation_columns = "date TEXT, chat_id INTEGER, chat_type TEXT, chat_title TEXT, chat_username TEXT, chat_desc TEXT, chat_members INTEGER, owner TEXT" |
|
74
|
|
|
entry_columns = "date, chat_id, chat_type, chat_title, chat_username, chat_desc, chat_members, owner" |
|
75
|
|
|
create_table(table_name, creation_columns) |
|
76
|
|
|
delete_old(table_name, current_time_obj) |
|
77
|
|
|
chat_id, chat_type, chat_title, chat_username, chat_desc, chat_members, owner, _, user = get_chat_info(bot, update) |
|
|
|
|
|
|
78
|
|
|
if check_entry(chat_id, table_name): |
|
79
|
|
|
return |
|
80
|
|
|
values = [current_time, chat_id, chat_type, chat_title, chat_username, chat_desc, chat_members, owner] |
|
81
|
|
|
data_entry(table_name, entry_columns, values) |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
def log_command(bot, update, date, command): |
|
85
|
|
|
table_name = "commands" |
|
86
|
|
|
creation_columns = "date TEXT, user_id INTEGER, user TEXT, command TEXT, chat_id INTEGER, chat_title TEXT" |
|
87
|
|
|
entry_columns = "date, user_id, user, command, chat_id, chat_title" |
|
88
|
|
|
create_table(table_name, creation_columns) |
|
89
|
|
|
ci = get_chat_info(bot, update) |
|
90
|
|
|
chat_id, chat_title, user_id, user = ci[0], ci[2], ci[7], ci[8] |
|
91
|
|
|
values = [date, user_id, user, command, chat_id, chat_title] |
|
92
|
|
|
data_entry(table_name, entry_columns, values) |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
def get_message(bot, update): |
|
96
|
|
|
current_time = datetime.strftime(datetime.now(), "%d.%m.%Y %H:%M:%S") |
|
97
|
|
|
table_name = "messages" |
|
98
|
|
|
creation_columns = "date TEXT, chat_id INTEGER, chat_type TEXT, chat_title TEXT, user_id INTEGER, user TEXT, message TEXT, photo BLOB" |
|
99
|
|
|
entry_columns = "date, chat_id, chat_type, chat_title, user_id, user, message, photo" |
|
100
|
|
|
create_table(table_name, creation_columns) |
|
101
|
|
|
ci = get_chat_info(bot, update) |
|
102
|
|
|
chat_id, chat_type, chat_title, user_id, user = ci[0], ci[1], ci[2], ci[7], ci[8] |
|
103
|
|
|
message = update.message.text |
|
104
|
|
|
try: |
|
105
|
|
|
img = bot.getFile(update.message.photo[-1].file_id)["file_path"] |
|
106
|
|
|
except: |
|
107
|
|
|
img = None |
|
108
|
|
|
values = [current_time, chat_id, chat_type, chat_title, user_id, user, message, img] |
|
109
|
|
|
data_entry(table_name, entry_columns, values) |