-
Notifications
You must be signed in to change notification settings - Fork 1
/
userreorg.py
executable file
·69 lines (59 loc) · 1.93 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
import pymysql.cursors
import sys
import argparse
from configobj import ConfigObj
from lib.dbcheck import db_need_update
from lib.logging import logger
##################
# parsing arguments
parser = argparse.ArgumentParser()
parser.add_argument("-d", dest='dry', help="dry run", action='store_true')
parser.add_argument("-c", dest='configfile', help="configfile", type=str, default="config.ini")
args = parser.parse_args()
dryrun = 0
try:
if args.dry:
dryrun = 1
logger.info("Dryrun")
except:
pass
# read inifile
#
try:
config = ConfigObj(args.configfile)
db = config['dbname']
dbhost = config['dbhost']
dbport = config.get('dbport', '3306')
dbuser = config['dbuser']
dbpassword = config['dbpassword']
reorgdays = int((config.get('reorgdays', '180')))
except:
logger.error("Inifile not given or missing parameter")
quit()
# 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:
logger.error("can not connect to database")
quit()
if db_need_update(cursor):
logger.error("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:
logger.info("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:
logger.error("Error in deleting chatid from userassign")