Passed
Push — master ( 1eef4e...280449 )
by Anas
02:26
created

modules.logging.logging_decorator()   A

Complexity

Conditions 1

Size

Total Lines 17
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
nop 1
dl 0
loc 17
rs 9.65
c 0
b 0
f 0
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
from telegram.ext import CommandHandler
4
from datetime import datetime
5
import threading
6
import sqlite3
7
8
9
def logging_decorator(command_name):
10
    def decorator(func):
11
        def wrapper(bot, update, *args, **kwargs):
12
            current_time = datetime.strftime(datetime.now(), "%d.%m.%Y %H:%M:%S")
13
            data = func(bot, update, *args, **kwargs)
14
            print(
15
                "{} > /{} > {} > {} > {}".format(
16
                    current_time,
17
                    command_name,
18
                    update.message.from_user.username,
19
                    update.message.from_user.id,
20
                    data
21
                )
22
            )
23
            log_command(bot, update, current_time, command_name)
24
        return wrapper
25
    return decorator
26
27
28
def module_init(gd):
29
    global c, conn, db_lock
30
    db_lock = threading.Lock()
31
    path = gd.config["path"]
32
    conn  = sqlite3.connect(path+"rikka.db", check_same_thread=False)
33
    c = conn.cursor()
34
35
36
def data_entry(table, entry_columns, values):
37
    values_count = ("?, "*len(values))[:-2]
38
    db_lock.acquire()
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable db_lock does not seem to be defined.
Loading history...
39
    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...
40
    conn.commit()
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable conn does not seem to be defined.
Loading history...
41
    db_lock.release()
42
43
44
def check_entry(chat_id, table):
45
    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...
46
    if c.fetchone() is not None:
47
        return True
48
    else:
49
        return False
50
51
52
def get_chat_info(bot, update):
53
    if update.callback_query is not None:
54
        user_name = update.callback_query.message.chat.username
55
        chat_id = update.callback_query.message.chat_id
56
        user_id = update.callback_query.from_user.id
57
    else:
58
        user_name = update.effective_message.from_user.name[1:]
59
        chat_id = update.effective_message.chat_id
60
        user_id = update.effective_message.from_user.id
61
    chat = bot.getChat(chat_id)
62
    return chat_id, chat.title, user_id, user_name
63
64
65
def log_command(bot, update, date, command):
66
    table_name = "commands"
67
    entry_columns = "date, user_id, user, command, chat_id, chat_title" 
68
    chat_id, chat_title, user_id, user = get_chat_info(bot, update)
69
    values = [date, user_id, user, command, chat_id, chat_title]
70
    data_entry(table_name, entry_columns, values)
71