-
Notifications
You must be signed in to change notification settings - Fork 4
/
QuerySqlTask.py
270 lines (243 loc) · 8.04 KB
/
QuerySqlTask.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/python
# -* coding: UTF-8 -*-
from MysqlUtils import mysql_client_connect
from celery import Celery
import json
app = Celery(backend='amqp', broker='amqp://')
def encode_contacts_json(diff):
if len(diff) == 0:
return str(diff)
str_json = '[{\"fphone\":\"%s\", \"fname\":\"%s\"}' % (diff[0][0], diff[0][1])
for idx in range(1, len(diff)):
item = ',{\"fphone\":\"%s\", \"fname\":\"%s\"}' % (diff[idx][0], diff[idx][1])
str_json = str_json + item
str_json = str_json + ']'
return str_json
def encode_calllogs_json(diff):
if len(diff) == 0:
return str(diff)
str_json = '[{\"fphone\":\"%s\", \"fname\":\"%s\", \"type\":%d, \
\"long_date\":%d}' % (diff[0][0], diff[0][1], diff[0][2], diff[0][3])
for idx in range(1, len(diff)):
item = ',{\"fphone\":\"%s\", \"fname\":\"%s\", \"type\":%d, \
\"long_date\":%d}' % (diff[idx][0], diff[idx][1], diff[idx][2], diff[idx][3])
str_json = str_json + item
str_json = str_json + ']'
return str_json
def encode_msg_json(diff):
if len(diff) == 0:
return str(diff)
str_json = '[{\"long_date\":%d, \"from_user\":\"%s\", \
\"msg\":\"%s\"}' % (diff[0][0], diff[0][1], diff[0][2])
for idx in range(1, len(diff)):
item = ',{\"long_date\":%d, \"from_user\":\"%s\", \
\"msg\":\"%s\"}' % (diff[idx][0], diff[idx][1], diff[idx][2])
str_json = str_json + item
str_json = str_json + ']'
return str_json
@app.task
def celery_query_contacts(name, passwd):
uid = celery_user_login(name, passwd)
if uid == 0:
return []
conn = mysql_client_connect()
cursor = conn.cursor()
sql = 'select fname, fphone from tbl_contacts \
where uid={uid}'.format(uid=int(uid))
cursor.execute(sql)
row = cursor.fetchall()
cursor.close()
conn.close()
return row
@app.task
def celery_query_calllogs(name, passwd, stamp):
uid = celery_user_login(name, passwd)
if uid == 0:
return []
conn = mysql_client_connect()
cursor = conn.cursor()
sql = 'select fname, fphone, type from tbl_calllogs \
where long_date>{stamp} order by long_date desc'.format(stamp=stamp)
cursor.execute(sql)
row = cursor.fetchall()
cursor.close()
conn.close()
return row
@app.task
def celery_query_msgs(name, passwd):
uid = celery_user_login(name, passwd)
if uid == 0:
return []
conn = mysql_client_connect()
cursor = conn.cursor()
sql = 'select from_user, msg from tbl_msgs \
where uid={uid} order by long_date desc'.format(uid=int(uid))
cursor.execute(sql)
row = cursor.fetchall()
cursor.close()
conn.close()
return row
@app.task
def celery_sync_contacts(uid, contacts):
json_contacts = json.loads(contacts)
size = len(json_contacts)
conn = mysql_client_connect()
cursor = conn.cursor()
hashtbl = {}
sql = 'select fphone, fname from tbl_contacts \
where uid={uid}'.format(uid=int(uid))
cursor.execute(sql)
row = cursor.fetchall()
contacts_db = []
for i in range(len(row)):
fphone = row[i][0]
fname = row[i][1]
contacts_db.append((fphone, fname))
hashtbl[fphone] = fname
contacts_user = []
for i in range(size):
item = json_contacts[i]
fphone = item['fphone']
fname = item['fname']
contacts_user.append((fphone, fname))
if fphone in hashtbl:
continue
else:
hashtbl[fphone] = fname
sql = 'insert into tbl_contacts(uid, fname, fphone) \
values({uid}, \'{fname}\', \'{fphone}\')'.format(uid=int(uid), fname=fname, fphone=fphone)
cursor.execute(sql)
conn.commit()
cursor.close()
conn.close()
diff = []
if not contacts_db:
return str(diff)
diff.extend(list(set(contacts_db).difference(set(contacts_user))))
return encode_contacts_json(diff)
@app.task
def celery_sync_calllogs(uid, calllogs):
json_calllogs = json.loads(calllogs)
size = len(json_calllogs)
conn = mysql_client_connect()
cursor = conn.cursor()
hashtbl = {}
sql = 'select fphone, fname, long_date, type from tbl_calllogs \
where uid={uid}'.format(uid=int(uid))
cursor.execute(sql)
row = cursor.fetchall()
calllogs_db = []
for i in range(len(row)):
fphone = row[i][0]
fname = row[i][1]
long_date = row[i][2]
ftype = row[i][3]
calllogs_db.append((fphone, fname, ftype, long_date))
k = ''.join([str(fphone), str(long_date)])
hashtbl[k] = 1
calllogs_user = []
for i in range(size):
item = json_calllogs[i]
fphone = item['fphone']
fname = item['fname']
long_date = item['long_date']
ftype = item['type']
calllogs_user.append((fphone, fname, ftype, long_date))
if fphone in hashtbl:
continue
else:
k = ''.join([str(fphone), str(long_date)])
hashtbl[k] = 1
sql = 'insert into tbl_calllogs(uid, fname, fphone, type, long_date) \
values({uid}, \'{fname}\', \'{fphone}\', \
{ftype}, {long_date})'.format(uid=int(uid), fname=fname, fphone=fphone, ftype=ftype, long_date=long_date)
cursor.execute(sql)
conn.commit()
cursor.close()
conn.close()
diff = []
if not calllogs_db:
return str(diff)
diff.extend(list(set(calllogs_db).difference(set(calllogs_user))))
return encode_calllogs_json(diff)
@app.task
def celery_sync_msgs(uid, msgs):
json_msgs = json.loads(msgs)
size = len(json_msgs)
conn = mysql_client_connect()
cursor = conn.cursor()
sql = 'select long_date, from_user, msg from \
tbl_msgs where uid={uid}'.format(uid=int(uid))
cursor.execute(sql)
row = cursor.fetchall()
hashtbl = {}
msgs_db = []
for i in range(len(row)):
long_date = row[i][0]
from_user = row[i][1]
msgs_db.append((long_date, from_user))
hashtbl[''.join([str(long_date), from_user])] = row[i][2]
msgs_user = []
for i in range(size):
item = json_msgs[i]
from_user = item['from_user']
long_date = item['long_date']
msgs_user.append((long_date, from_user))
k = ''.join([str(long_date), from_user])
if k in hashtbl:
continue
else:
hashtbl[k] = item['msg']
msg = item['msg']
sql = 'insert into tbl_msgs(uid, from_user, msg, long_date) \
values( \
{uid}, \'{from_user}\', \'{msg}\', {long_date} \
)'.format(uid=int(uid), from_user=from_user, msg=msg, long_date=long_date)
cursor.execute(sql)
conn.commit()
cursor.close()
conn.close()
diffk = []
if not msgs_db:
return str(diffk)
diffk.extend(list(set(msgs_db).difference(set(msgs_user))))
diff = []
for item in diffk:
k = ''.join([str(item[0]), item[1]])
diff.append((item[0], item[1], hashtbl[k]))
return encode_msg_json(diff)
@app.task
def celery_user_register(name, passwd):
sql = 'select uid from tbl_user_info \
where username=\'{name}\' limit 1;'.format(name=name)
conn = mysql_client_connect()
cursor = conn.cursor()
cursor.execute(sql)
row = cursor.fetchone()
if row and row[0]:
return 0
sql = 'insert into tbl_user_info(username, passwd) \
values(\'{name}\',\'{passwd}\');'.format(name=name, passwd=passwd)
cursor.execute(sql)
conn.commit()
sql = 'select uid from tbl_user_info where \
username=\'{name}\' limit 1;'.format(name=name)
cursor.execute(sql)
row = cursor.fetchone()
cursor.close()
conn.close()
return row[0]
@app.task
def celery_user_login(name, passwd):
sql = 'select uid from tbl_user_info \
where username=\'{name}\' and passwd=\'{passwd}\''.format(name=name, passwd=passwd)
conn = mysql_client_connect()
cursor = conn.cursor()
cursor.execute(sql)
row = cursor.fetchone()
uid = 0
if row and row[0]:
uid = row[0]
cursor.close()
conn.close()
return uid