forked from C64Axel/MonsterBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserreorg.py
executable file
·83 lines (69 loc) · 2.11 KB
/
userreorg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#! /usr/bin/python
import telepot
import pymysql.cursors
import logging
import sys
import datetime
import time
from configobj import ConfigObj
from lib.dbcheck import db_need_update
# Logging
#
def log(msg):
print (msg)
logging.basicConfig(filename="log/userreorg.log", format="%(asctime)s|%(message)s", level=logging.INFO)
logging.info(msg)
##### MAIN #####
def my_excepthook(excType, excValue, traceback, logger=logging):
logging.error("Logging an uncaught exception",
exc_info=(excType, excValue, traceback))
sys.excepthook = my_excepthook
# read inifile
#
try:
inifile = sys.argv[1]
config = ConfigObj(inifile)
db = config['dbname']
dbhost = config['dbhost']
dbport = config.get('dbport', '3306')
dbuser = config['dbuser']
dbpassword = config['dbpassword']
reorgdays = int((config.get('reorgdays', '180')))
except:
log("Inifile not given or missing parameter")
quit()
dryrun = 0
try:
dryrun = sys.argv[2]
if dryrun == '-n':
dryrun = 1
log("Dryrun")
except:
pass
# connect to database
#
try:
connection = pymysql.connect(host=dbhost,
user=dbuser,
password=dbpassword,
db=db,
port=int(dbport),
charset='utf8mb4',
autocommit='True')
cursor = connection.cursor()
except:
log("can not connect to database")
quit()
if db_need_update(cursor):
log("Your DB-Version is to low. Please start dbupdate.py first")
quit()
cursor.execute("select chatid from userstop where timestampdiff(DAY,stopdate,CURRENT_TIMESTAMP) > '%s'" % (reorgdays))
result = cursor.fetchall()
for row in result:
log("Delete Chatid {}".format(row[0]))
if dryrun == 0:
try:
cursor.execute("delete from userassign where chatid = '%s'" % (row[0]))
cursor.execute("delete from userstop where chatid = '%s'" % (row[0]))
except:
log("Error in deleting chatid from userassign")