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

modules.logging   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 54
dl 0
loc 71
rs 10
c 0
b 0
f 0
wmc 8

6 Functions

Rating   Name   Duplication   Size   Complexity  
A log_command() 0 6 1
A module_init() 0 6 1
A get_chat_info() 0 11 2
A data_entry() 0 6 1
A check_entry() 0 6 2
A logging_decorator() 0 17 1
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