Completed
Push — master ( 37c2c2...001739 )
by Anas
29s
created

check_entry()   A

Complexity

Conditions 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
dl 0
loc 13
rs 9.2
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
    commands = gd.config["commands"]
0 ignored issues
show
Unused Code introduced by
The variable commands seems to be unused.
Loading history...
12
    conn  = sqlite3.connect(path+"rikka.db", check_same_thread=False) 
13
    c = conn.cursor()
14
    #c.execute("DROP TABLE IF EXISTS chats")
15
    gd.dp.add_handler(MessageHandler(Filters.all, get_chats), group=1)
16
17
18
def create_table(name, columns):
19
    c.execute("CREATE TABLE IF NOT EXISTS "+name+"("+columns+")")
20
21
22
def data_entry(table, entry_columns, values):
23
    columns = str(values).strip('[]')
0 ignored issues
show
Unused Code introduced by
The variable columns seems to be unused.
Loading history...
24
    values_count = ("?, "*len(values))[:-2]
25
    c.execute("INSERT INTO "+table+" ("+entry_columns+") VALUES ("+values_count+")", (values))
26
    conn.commit()
27
28
29
def check_entry(chat_id, table, date):
30
    span = timedelta(days=0, seconds=10)
31
    c.execute("SELECT chat_id FROM "+table)
32
    for row in c.fetchall():
33
        if chat_id in row:
34
            c.execute("SELECT date FROM "+table+" WHERE chat_id = %s" %(chat_id))
35
            old_date = c.fetchone()[0]
36
            old_date_time = datetime.strptime(old_date, "%d.%m.%Y %H:%M:%S")
37
            if date - old_date_time > span:
38
                c.execute("DELETE FROM "+table+" WHERE chat_id = %s" %(chat_id))
39
                return False
40
            else:
41
                return True
42
43
44
def get_chats(bot, update):
45
    current_time = datetime.strftime(datetime.now(), "%d.%m.%Y %H:%M:%S")
46
    current_time_obj = datetime.strptime(current_time, "%d.%m.%Y %H:%M:%S")
47
    table_name = "chats"
48
    creation_columns = "date TEXT, chat_id INTEGER, chat_type TEXT, chat_title TEXT, chat_username TEXT, chat_desc TEXT, chat_members INTEGER, owner TEXT"
49
    entry_columns = "date, chat_id, chat_type, chat_title, chat_username, chat_desc, chat_members, owner" 
50
    create_table(table_name, creation_columns)
51
52
    chat_id = update.effective_message.chat_id
53
    if check_entry(chat_id, table_name, current_time_obj):
54
        return
55
    chat = bot.getChat(chat_id)
56
    chat_type = chat.type
57
    chat_title = chat.title
58
    chat_username = chat.username
59
    chat_desc = chat.description
60
    chat_members = chat.get_members_count()
61
    if chat_type == "private":
62
        owner = update.effective_message.from_user.name
63
        if owner.startswith("@"):
64
            owner = owner[1:]
65
    else:
66
        admins = chat.get_administrators(CREATOR = "creator")
67
        for admin in admins:
68
            if admin.status == "creator":
69
                owner = admin.user.username
70
    values = [current_time, chat_id, chat_type, chat_title, chat_username, chat_desc, chat_members, owner]
71
    data_entry(table_name, entry_columns, values)
72
73
74
def log_command(update, date, command):
75
    table_name = "commands"
76
    creation_columns = "date TEXT, user TEXT, command TEXT, chat_id INTEGER, chat_title TEXT"
77
    entry_columns = "date, user, command, chat_id, chat_title" 
78
    create_table(table_name, creation_columns)
79
    if update.callback_query is not None:
80
        user = update.callback_query.message.chat.username
81
    else:
82
        user = update.effective_message.from_user.name[1:]
83
    chat_id = update.effective_message.chat_id
84
    chat_title = update.effective_message.chat.title
85
86
    values = [date, user, command, chat_id, chat_title]
87
    data_entry(table_name, entry_columns, values)
88