Passed
Push — master ( 9dd95f...75b147 )
by Anas
01:58
created

modules.logging.vk()   A

Complexity

Conditions 2

Size

Total Lines 13
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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