-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHelper.py
132 lines (114 loc) · 4.44 KB
/
Helper.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#-*- coding: utf-8 -*-
import re,time,datetime
import config
from lib.SQLite import SQLite
def log(mesg):
try:
print mesg.encode("utf-8")
except:
print mesg
def sql_escape(string):
return string.replace('\\', '\\\\').replace('\'', '\'\'')
def set_app_value(cfg_name, cfg_value):
"""set cfg_name's value."""
sql = "UPDATE sw_app SET cfg_value='%s' WHERE cfg_name='%s'" % (cfg_value, cfg_name)
db = SQLite(config.DB_FILE)
db.do_sql(sql)
def get_app_value(cfg_name):
"""get cfg_name's value."""
sql = "SELECT cfg_value FROM sw_app WHERE cfg_name='%s'" % cfg_name
db = SQLite(config.DB_FILE)
rows = db.fetch_sql(sql)
return rows[0][0]
def is_user_exists(screen_name):
sql = "SELECT * FROM sw_users WHERE screen_name='%s'" % screen_name
db = SQLite(config.DB_FILE)
rows = db.fetch_sql(sql)
if rows:
return True
else:
return False
def add_user(user):
if not is_user_exists(user["name"]):
sql = "INSERT INTO sw_users(user_id, screen_name, sex, school) VALUES('%s', '%s', '%s', '%s')" \
% (user["id"],user["name"],user["sex"],user["school"])
db = SQLite(config.DB_FILE)
db.do_sql(sql)
log("add user %s!" % user["name"])
return True
else:
return False
def add_command_log(screen_name, command, text, time):
sql = "INSERT INTO sw_log(screen_name,command,text, time) VALUES('%s', '%s', '%s', '%s');" % (screen_name, command, text, time)
db = SQLite(config.DB_FILE)
db.do_sql(sql)
def refresh_at_time():
sql = "UPDATE sw_app SET cfg_value=(SELECT time FROM sw_log ORDER BY time DESC LIMIT 1) WHERE cfg_name='at_time';"
db = SQLite(config.DB_FILE)
db.do_sql(sql)
def get_status(last_post_time):
sql = "SELECT sex,school,message,pub_time FROM sw_messages,sw_users WHERE src!='%s' AND src=screen_name AND pub_time>'%s' ORDER BY pub_time" % ('我'.decode('utf-8'),last_post_time)
db = SQLite(config.DB_FILE)
rows = db.fetch_sql(sql)
# write the time back
#now = datetime.datetime.strftime(datetime.datetime.now(),"%Y-%m-%d %H:%M:%S")
#sql = "UPDATE sw_app SET cfg_value='%s' WHERE cfg_name='post_time'" % now
if len(rows):
sql = "UPDATE sw_app SET cfg_value='%s' WHERE cfg_name='post_time'" % rows[-1][3]
db.do_sql(sql)
return rows
def is_message_exists(message):
sql = "SELECT * FROM sw_messages WHERE src='%s' AND dst='%s' AND message='%s'" % \
(message["src"], message["dst"], message["message"])
db = SQLite(config.DB_FILE)
rows = db.fetch_sql(sql)
if rows:
return True
else:
return False
def is_status_exists(status):
sql = "SELECT * FROM sw_messages WHERE message='%s';" % status
db = SQLite(sql)
rows = db.fetch_sql(sql)
if rows:
return True
else:
return False
def save_2_sqlite(messages):
db = SQLite(config.DB_FILE)
count = 0
for message in messages:
if is_message_exists(message):
count += 1
continue
sql = "INSERT INTO sw_messages(src,dst,message,pub_time) VALUES('%s', '%s', '%s', '%s')" % \
(message["src"], message["dst"], message["message"], message["time"])
db.do_sql(sql)
log("%d messages existed!" % count)
def drop_table(table_name='sw_messages'):
sql = "DELETE FROM %s" % table_name
db = SQLite(config.DB_FILE)
db.do_sql(sql)
def datetime_formater(date_string):
#log("Soving Date String: %s" % date_string)
""" datetime formater """
if re.search(r'\d{4}(-\d{2}){2} \d{2}:\d{2}:\d{2}',date_string):
return date_string
elif re.search(r'分钟前', date_string):
mins_ago = re.findall(r'(\d{1,})分钟前', date_string)[0]
d = datetime.datetime.now()-datetime.timedelta(minutes=int(mins_ago))
return datetime.datetime.strftime(d, "%Y-%m-%d %H:%M:%S")
else:
if re.match(r'今天', date_string):
date_str = time.strftime("%Y-%m-%d", time.localtime())
else:
year = time.strftime("%Y", time.localtime())
month = re.findall(r'(\d{2})月', date_string)[0]
day = re.findall(r'(\d{2})日', date_string)[0]
date_str = "%s-%s-%s" % (year, month, day)
time_str = re.findall(r'(\d{2}:\d{2})', date_string)[0]
return "%s %s:00" % (date_str, time_str)
def str2date(string):
return datetime.datetime.strptime(string, "%Y-%m-%d %H:%M:%S")
if __name__ == '__main__':
drop_table()