Passed
Branch master (dc2491)
by Anas
03:44 queued 02:00
created

modules.logging.get_chat_info()   A

Complexity

Conditions 5

Size

Total Lines 19
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 17
nop 2
dl 0
loc 19
rs 9.0833
c 0
b 0
f 0
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
from telegram.ext import MessageHandler, Filters
4
from datetime import datetime, timedelta
5
import threading
6
import sqlite3
7
8
9
def module_init(gd):
10
    global c, conn, db_lock
11
    db_lock = threading.Lock()
12
    path = gd.config["path"]
13
    conn  = sqlite3.connect(path+"rikka.db", check_same_thread=False) 
14
    c = conn.cursor()
15
    gd.dp.add_handler(MessageHandler(Filters.all, get_chats), group=1)
16
17
18
def data_entry(table, entry_columns, values):
19
    values_count = ("?, "*len(values))[:-2]
20
    db_lock.acquire()
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable db_lock does not seem to be defined.
Loading history...
21
    c.execute("INSERT INTO "+table+" ("+entry_columns+") VALUES ("+values_count+")", (values))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable c does not seem to be defined.
Loading history...
22
    conn.commit()
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable conn does not seem to be defined.
Loading history...
23
    db_lock.release()
24
25
26
def check_entry(chat_id, table):
27
    c.execute("SELECT chat_id FROM "+table+" WHERE chat_id = %s" %(chat_id))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable c does not seem to be defined.
Loading history...
28
    if c.fetchone() is not None:
29
        return True
30
    else:
31
        return False
32
33
34
def delete_old(table, date):
35
    span = timedelta(days=7)
36
    c.execute("SELECT chat_id FROM "+table)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable c does not seem to be defined.
Loading history...
37
    for row in c.fetchall():
38
        c.execute("SELECT date FROM "+table+" WHERE chat_id = %s" %(row))
39
        old_date = c.fetchone()[0]
40
        old_date_time = datetime.strptime(old_date, "%d.%m.%Y %H:%M:%S")
41
        if date - old_date_time > span:
42
            db_lock.acquire()
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable db_lock does not seem to be defined.
Loading history...
43
            c.execute("DELETE FROM "+table+" WHERE chat_id = %s" %(row))
44
            conn.commit()
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable conn does not seem to be defined.
Loading history...
45
            db_lock.release()
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
    owner = None
59
    if chat.type == "private":
60
        owner = user_name
61
    else:
62
        admins = chat.get_administrators(CREATOR = "creator")
63
        for admin in admins:
64
            if admin.status == "creator":
65
                owner = admin.user.username
66
    return chat_id, chat.type, chat.title, chat.username, chat.description, chat.get_members_count(), owner, user_id, user_name
67
68
69
def get_chats(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
    entry_columns = "date, chat_id, chat_type, chat_title, chat_username, chat_desc, chat_members, owner" 
74
    delete_old(table_name, current_time_obj)
75
    chat_id, chat_type, chat_title, chat_username, chat_desc, chat_members, owner, _, _ = get_chat_info(bot, update)
76
    if check_entry(chat_id, table_name):
77
        return
78
    values = [current_time, chat_id, chat_type, chat_title, chat_username, chat_desc, chat_members, owner]
79
    data_entry(table_name, entry_columns, values)
80
81
82
def log_command(bot, update, date, command):
83
    table_name = "commands"
84
    entry_columns = "date, user_id, user, command, chat_id, chat_title" 
85
    ci = get_chat_info(bot, update)
86
    chat_id, chat_title, user_id, user = ci[0], ci[2], ci[7], ci[8]
87
    values = [date, user_id, user, command, chat_id, chat_title]
88
    data_entry(table_name, entry_columns, values)
89
90
91
def vk(bot, update):
92
    current_time = datetime.strftime(datetime.now(), "%d.%m.%Y %H:%M:%S")
93
    table_name = "vk"
94
    entry_columns = "date, unixtime, post_id"
95
    values = [0, 0, 0]
96
    c.execute("SELECT unixtime FROM vk ORDER BY rowid DESC LIMIT 1")
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable c does not seem to be defined.
Loading history...
97
    fetch = c.fetchone()
98
    if fetch is not None:
99
        old_date = int(fetch[0])
100
        return current_time, old_date
101
    else:
102
        data_entry(table_name, entry_columns, values)
103
        return current_time, 0
104
105
def vk_add(bot, update, date, unixtime, post_id):
106
    table_name = "vk"
107
    entry_columns = "date, unixtime, post_id"
108
    values = [date, unixtime, post_id]
109
    data_entry(table_name, entry_columns, values)
110