| Line 1... |
Line 1... |
| 1 |
#!/usr/bin/python
|
1 |
#!/usr/bin/python
|
| 2 |
|
2 |
|
| 3 |
import threading
|
3 |
import threading
|
| 4 |
import time
|
4 |
import time
|
| 5 |
import datetime
|
- |
|
| 6 |
|
5 |
|
| 7 |
import MySQLdb
|
6 |
import MySQLdb
|
| 8 |
from elixir import *
|
7 |
from elixir import *
|
| 9 |
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
|
8 |
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
|
| 10 |
from sqlalchemy.sql import func
|
9 |
from sqlalchemy.sql import func
|
| Line 40... |
Line 39... |
| 40 |
userGcmRegIdMap = {}
|
39 |
userGcmRegIdMap = {}
|
| 41 |
db = MySQLdb.connect('localhost',"root","shop2020","dtr" )
|
40 |
db = MySQLdb.connect('localhost',"root","shop2020","dtr" )
|
| 42 |
cursor = db.cursor()
|
41 |
cursor = db.cursor()
|
| 43 |
|
42 |
|
| 44 |
ALL_STORES_SQL = "select * from stores"
|
43 |
ALL_STORES_SQL = "select * from stores"
|
| 45 |
GCM_REG_ID_SQL = "select x.user_id, x.gcm_regid, x.id, x.androidid, x.created from (select * from gcm_users where user_id in %s order by id desc) as x group by x.user_id, x.gcm_regid, x.androidid"
|
44 |
GCM_REG_ID_SQL1 = "select x.user_id, x.gcm_regid, x.id, x.androidid, x.created from (select * from gcm_users where user_id in %s and androidid is not null order by id desc) as x group by x.user_id, x.gcm_regid, x.androidid"
|
| - |
|
45 |
GCM_REG_ID_SQL2 = "select x.user_id, x.gcm_regid, x.id, x.androidid, x.created from (select * from gcm_users where user_id in %s and androidid is null order by id desc) as x group by x.user_id, x.gcm_regid"
|
| - |
|
46 |
|
| 46 |
cursor.execute(ALL_STORES_SQL)
|
47 |
cursor.execute(ALL_STORES_SQL)
|
| 47 |
result_stores = cursor.fetchall()
|
48 |
result_stores = cursor.fetchall()
|
| 48 |
domainStoresMap = {}
|
49 |
domainStoresMap = {}
|
| 49 |
for rec in result_stores:
|
50 |
for rec in result_stores:
|
| 50 |
domainStoresMap[rec[2]] = rec
|
51 |
domainStoresMap[rec[2]] = rec
|
| Line 100... |
Line 101... |
| 100 |
logging.debug('User Id:-'+str(record.get('user_id'))+' Notification Url:- '+ str(record.get('user_id')))
|
101 |
logging.debug('User Id:-'+str(record.get('user_id'))+' Notification Url:- '+ str(record.get('user_id')))
|
| 101 |
if campaign.get('url') is None or str(campaign.get('url'))=='':
|
102 |
if campaign.get('url') is None or str(campaign.get('url'))=='':
|
| 102 |
result_url = 'http://api.profittill.com/deals?user_id='+str(record.get('user_id'))
|
103 |
result_url = 'http://api.profittill.com/deals?user_id='+str(record.get('user_id'))
|
| 103 |
data = {"message":campaign.get('message'),"cid":str(campaign.get('_id'))+"_"+str(record.get('_id')),"title":campaign.get('title'),
|
104 |
data = {"message":campaign.get('message'),"cid":str(campaign.get('_id'))+"_"+str(record.get('_id')),"title":campaign.get('title'),
|
| 104 |
"type":campaign.get('type'),"url":result_url.strip(),"vibrate":1,"sound":1,"largeIcon":"large_icon",
|
105 |
"type":campaign.get('type'),"url":result_url.strip(),"vibrate":1,"sound":1,"largeIcon":"large_icon",
|
| 105 |
"smallIcon":"small_icon","priority":"high","time_to_live":long(campaign.get('expiresat'))-long(time.mktime(datetime.datetime.now().timetuple()))}
|
106 |
"smallIcon":"small_icon","priority":"high","time_to_live":long(campaign.get('expiresat'))-long(time.mktime(datetime.now().timetuple()))}
|
| 106 |
|
107 |
|
| 107 |
post_data = {}
|
108 |
post_data = {}
|
| 108 |
|
109 |
|
| 109 |
post_data['data'] = data
|
110 |
post_data['data'] = data
|
| 110 |
regIds = []
|
111 |
regIds = []
|
| Line 164... |
Line 165... |
| 164 |
|
165 |
|
| 165 |
def populateCampaignsMap(pendingCampaigns):
|
166 |
def populateCampaignsMap(pendingCampaigns):
|
| 166 |
global campaignUsersMap
|
167 |
global campaignUsersMap
|
| 167 |
global notificationCampaignsMap
|
168 |
global notificationCampaignsMap
|
| 168 |
for campaign in pendingCampaigns:
|
169 |
for campaign in pendingCampaigns:
|
| 169 |
cursor.execute(campaign['sql'])
|
170 |
cursor.execute(str(campaign['sql']))
|
| 170 |
user_records = cursor.fetchall()
|
171 |
user_records = cursor.fetchall()
|
| 171 |
userids = []
|
172 |
userids = []
|
| 172 |
for record in user_records:
|
173 |
for record in user_records:
|
| 173 |
if long(record[0]) not in userids:
|
174 |
if long(record[0]) not in userids:
|
| 174 |
userids.append(long(record[0]))
|
175 |
userids.append(long(record[0]))
|
| Line 176... |
Line 177... |
| 176 |
notificationCampaignsMap[campaign['_id']] = campaign
|
177 |
notificationCampaignsMap[campaign['_id']] = campaign
|
| 177 |
|
178 |
|
| 178 |
def insertPushNotificationEntriesToSent():
|
179 |
def insertPushNotificationEntriesToSent():
|
| 179 |
global userGcmRegIdMap
|
180 |
global userGcmRegIdMap
|
| 180 |
for userList in campaignUsersMap.values():
|
181 |
for userList in campaignUsersMap.values():
|
| 181 |
cursor.execute(GCM_REG_ID_SQL%(str(tuple(userList))))
|
182 |
cursor.execute(GCM_REG_ID_SQL1%(str(tuple(userList))))
|
| 182 |
result_data = cursor.fetchall()
|
183 |
result_data = cursor.fetchall()
|
| 183 |
|
184 |
|
| - |
|
185 |
if result_data and len(result_data)>0:
|
| 184 |
for dataRec in result_data:
|
186 |
for dataRec in result_data:
|
| - |
|
187 |
if dataRec[0] in userList:
|
| - |
|
188 |
userList.remove(long(dataRec[0]))
|
| 185 |
if userGcmRegIdMap.has_key(dataRec[0]):
|
189 |
if userGcmRegIdMap.has_key(dataRec[0]):
|
| 186 |
detailMap = {}
|
190 |
detailMap = {}
|
| 187 |
gcmRegIdMap = userGcmRegIdMap.get(dataRec[0])
|
191 |
gcmRegIdMap = userGcmRegIdMap.get(dataRec[0])
|
| 188 |
detailMap['gcm_regid'] = dataRec[1]
|
192 |
detailMap['gcm_regid'] = dataRec[1]
|
| 189 |
if dataRec[3] is not None:
|
193 |
if dataRec[3] is not None:
|
| 190 |
detailMap['android_id'] = dataRec[3]
|
194 |
detailMap['android_id'] = dataRec[3]
|
| 191 |
gcmRegIdMap[dataRec[2]]= detailMap
|
195 |
gcmRegIdMap[dataRec[2]]= detailMap
|
| 192 |
userGcmRegIdMap[dataRec[0]] = gcmRegIdMap
|
196 |
userGcmRegIdMap[dataRec[0]] = gcmRegIdMap
|
| 193 |
else:
|
197 |
else:
|
| 194 |
gcmRegIdMap = {}
|
198 |
gcmRegIdMap = {}
|
| 195 |
detailMap = {}
|
199 |
detailMap = {}
|
| 196 |
detailMap['gcm_regid'] = dataRec[1]
|
200 |
detailMap['gcm_regid'] = dataRec[1]
|
| 197 |
if dataRec[3] is not None:
|
201 |
if dataRec[3] is not None:
|
| 198 |
detailMap['android_id'] = dataRec[3]
|
202 |
detailMap['android_id'] = dataRec[3]
|
| 199 |
gcmRegIdMap[dataRec[2]]= detailMap
|
203 |
gcmRegIdMap[dataRec[2]]= detailMap
|
| 200 |
userGcmRegIdMap[dataRec[0]] = gcmRegIdMap
|
204 |
userGcmRegIdMap[dataRec[0]] = gcmRegIdMap
|
| 201 |
|
205 |
|
| - |
|
206 |
cursor.execute(GCM_REG_ID_SQL2%(str(tuple(userList))))
|
| - |
|
207 |
result_data = cursor.fetchall()
|
| - |
|
208 |
for dataRec in result_data:
|
| - |
|
209 |
if userGcmRegIdMap.has_key(dataRec[0]):
|
| - |
|
210 |
detailMap = {}
|
| - |
|
211 |
gcmRegIdMap = userGcmRegIdMap.get(dataRec[0])
|
| - |
|
212 |
detailMap['gcm_regid'] = dataRec[1]
|
| - |
|
213 |
if dataRec[3] is not None:
|
| - |
|
214 |
detailMap['android_id'] = dataRec[3]
|
| - |
|
215 |
gcmRegIdMap[dataRec[2]]= detailMap
|
| - |
|
216 |
userGcmRegIdMap[dataRec[0]] = gcmRegIdMap
|
| - |
|
217 |
else:
|
| - |
|
218 |
gcmRegIdMap = {}
|
| - |
|
219 |
detailMap = {}
|
| - |
|
220 |
detailMap['gcm_regid'] = dataRec[1]
|
| - |
|
221 |
if dataRec[3] is not None:
|
| - |
|
222 |
detailMap['android_id'] = dataRec[3]
|
| - |
|
223 |
gcmRegIdMap[dataRec[2]]= detailMap
|
| - |
|
224 |
userGcmRegIdMap[dataRec[0]] = gcmRegIdMap
|
| - |
|
225 |
else:
|
| - |
|
226 |
cursor.execute(GCM_REG_ID_SQL2%(str(tuple(userList))))
|
| - |
|
227 |
result_data = cursor.fetchall()
|
| - |
|
228 |
for dataRec in result_data:
|
| - |
|
229 |
if userGcmRegIdMap.has_key(dataRec[0]):
|
| - |
|
230 |
detailMap = {}
|
| - |
|
231 |
gcmRegIdMap = userGcmRegIdMap.get(dataRec[0])
|
| - |
|
232 |
detailMap['gcm_regid'] = dataRec[1]
|
| - |
|
233 |
if dataRec[3] is not None:
|
| - |
|
234 |
detailMap['android_id'] = dataRec[3]
|
| - |
|
235 |
gcmRegIdMap[dataRec[2]]= detailMap
|
| - |
|
236 |
userGcmRegIdMap[dataRec[0]] = gcmRegIdMap
|
| - |
|
237 |
else:
|
| - |
|
238 |
gcmRegIdMap = {}
|
| - |
|
239 |
detailMap = {}
|
| - |
|
240 |
detailMap['gcm_regid'] = dataRec[1]
|
| - |
|
241 |
if dataRec[3] is not None:
|
| - |
|
242 |
detailMap['android_id'] = dataRec[3]
|
| - |
|
243 |
gcmRegIdMap[dataRec[2]]= detailMap
|
| - |
|
244 |
userGcmRegIdMap[dataRec[0]] = gcmRegIdMap
|
| 202 |
|
245 |
|
| 203 |
for campaignId, userList in campaignUsersMap.items():
|
246 |
for campaignId, userList in campaignUsersMap.items():
|
| 204 |
for userId in userList:
|
247 |
for userId in userList:
|
| 205 |
gcmRegIdMap = userGcmRegIdMap.get(userId)
|
248 |
gcmRegIdMap = userGcmRegIdMap.get(userId)
|
| 206 |
if gcmRegIdMap is None:
|
249 |
if gcmRegIdMap is None:
|
| Line 226... |
Line 269... |
| 226 |
entries.append(entry)
|
269 |
entries.append(entry)
|
| 227 |
pendingNotificationEntryMap[entry['notification_campaign_id']] = entries
|
270 |
pendingNotificationEntryMap[entry['notification_campaign_id']] = entries
|
| 228 |
|
271 |
|
| 229 |
def initiateNotificationThreadProcess(chunkSize):
|
272 |
def initiateNotificationThreadProcess(chunkSize):
|
| 230 |
count = 1
|
273 |
count = 1
|
| 231 |
logging.debug('Starting Push Notification Job....'+str(datetime.datetime.now()))
|
274 |
logging.debug('Starting Push Notification Job....'+str(datetime.now()))
|
| 232 |
for entries in pendingNotificationEntryMap.values():
|
275 |
for entries in pendingNotificationEntryMap.values():
|
| 233 |
campaign_receivers_list = list(chunks(entries, chunkSize))
|
276 |
campaign_receivers_list = list(chunks(entries, chunkSize))
|
| 234 |
print len(campaign_receivers_list)
|
277 |
print len(campaign_receivers_list)
|
| 235 |
for sublist in campaign_receivers_list:
|
278 |
for sublist in campaign_receivers_list:
|
| 236 |
thread = NotificationThread(count, "Thread-"+str(count), sublist)
|
279 |
thread = NotificationThread(count, "Thread-"+str(count), sublist)
|
| 237 |
thread.start()
|
280 |
thread.start()
|
| 238 |
count = count +1
|
281 |
count = count +1
|
| 239 |
logging.debug('Stopping Push Notification Job....'+str(datetime.datetime.now()))
|
282 |
logging.debug('Stopping Push Notification Job....'+str(datetime.now()))
|
| 240 |
|
283 |
|
| - |
|
284 |
def markNotificationCampaignsProcessed():
|
| - |
|
285 |
for campaign in notificationCampaignsMap.values():
|
| - |
|
286 |
logging.debug('Notification Campaign....'+str(campaign.get('notification_id'))+"...Marked Processed. "+str(datetime.now()))
|
| - |
|
287 |
get_mongo_connection(host=mongoHost).User.notificationcampaigns.update({'_id':campaign.get('_id')},{"$set":{'notification_processed':1}})
|
| 241 |
|
288 |
|
| 242 |
def main():
|
289 |
def main():
|
| 243 |
global mongoHost
|
290 |
global mongoHost
|
| 244 |
parser = optparse.OptionParser()
|
291 |
parser = optparse.OptionParser()
|
| 245 |
parser.add_option("-C", "--chunksize", dest="chunksize",
|
292 |
parser.add_option("-C", "--chunksize", dest="chunksize",
|
| Line 255... |
Line 302... |
| 255 |
pendingCampaigns = getPendingCampaigns()
|
302 |
pendingCampaigns = getPendingCampaigns()
|
| 256 |
populateCampaignsMap(pendingCampaigns)
|
303 |
populateCampaignsMap(pendingCampaigns)
|
| 257 |
insertPushNotificationEntriesToSent()
|
304 |
insertPushNotificationEntriesToSent()
|
| 258 |
populatePendingNotificationEntriesToBeSent()
|
305 |
populatePendingNotificationEntriesToBeSent()
|
| 259 |
initiateNotificationThreadProcess(options.chunksize)
|
306 |
initiateNotificationThreadProcess(options.chunksize)
|
| - |
|
307 |
markNotificationCampaignsProcessed()
|
| - |
|
308 |
|
| - |
|
309 |
db.close()
|
| 260 |
|
310 |
|
| 261 |
if __name__=='__main__':
|
311 |
if __name__=='__main__':
|
| 262 |
main()
|
312 |
main()
|
| 263 |
|
313 |
|
| 264 |
|
314 |
|