| Line 25... |
Line 25... |
| 25 |
parser.add_option("-M", "--mongoHost", dest="mongoHost",
|
25 |
parser.add_option("-M", "--mongoHost", dest="mongoHost",
|
| 26 |
default="localhost",
|
26 |
default="localhost",
|
| 27 |
type="str", help="The requsets a single thread handles",
|
27 |
type="str", help="The requsets a single thread handles",
|
| 28 |
metavar="MONGOHOST")
|
28 |
metavar="MONGOHOST")
|
| 29 |
parser.add_option("-m", "--mysqlHost", dest="mysqlHost",
|
29 |
parser.add_option("-m", "--mysqlHost", dest="mysqlHost",
|
| 30 |
default="127.0.0.1",
|
30 |
default="localhost",
|
| 31 |
type="str", help="The requsets a single thread handles",
|
31 |
type="str", help="The requsets a single thread handles",
|
| 32 |
metavar="MONGOHOST")
|
32 |
metavar="MONGOHOST")
|
| 33 |
|
33 |
|
| 34 |
(options, args) = parser.parse_args()
|
34 |
(options, args) = parser.parse_args()
|
| 35 |
|
35 |
|
| 36 |
GCM_URL = "https://android.googleapis.com/gcm/send"
|
36 |
GCM_URL = "https://android.googleapis.com/gcm/send"
|
| - |
|
37 |
FCM_URL = "https://fcm.googleapis.com/fcm/send"
|
| 37 |
GOOGLE_API_KEY = "AIzaSyBGL29sWCKm8OweP62ywrfv4jv4VIN8u0o"
|
38 |
GOOGLE_API_KEY = "AIzaSyBGL29sWCKm8OweP62ywrfv4jv4VIN8u0o"
|
| - |
|
39 |
FCM_API_KEY = "AAAASAjNcn4:APA91bG6fWRIgYJI0L9gCjP5ynaXz2hJHYKtD9dfH7Depdv31Nd9APJwhx-OPkAJ1WSz4BGNYG8lHThLFSjDGFxIwUZv241YcAJEGDLgt86mxq9FXJe-yBRu-S0_ZwHqmX-QaVKl5F_A"
|
| 38 |
headers = {'content-type':'application/json', "authorization":"key=" + GOOGLE_API_KEY}
|
40 |
headers_gcm = {'content-type':'application/json', "authorization":"key=" + GOOGLE_API_KEY}
|
| - |
|
41 |
headers_fcm = {'content-type':'application/json', "authorization":"key=" + FCM_API_KEY}
|
| 39 |
aff_url_headers = {
|
42 |
aff_url_headers = {
|
| 40 |
'User-agent':'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36',
|
43 |
'User-agent':'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36',
|
| 41 |
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
44 |
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
| 42 |
'Accept-Language' : 'en-US,en;q=0.8',
|
45 |
'Accept-Language' : 'en-US,en;q=0.8',
|
| 43 |
'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
|
46 |
'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
|
| 44 |
'Connection':'keep-alive'
|
47 |
'Connection':'keep-alive'
|
| 45 |
}
|
48 |
}
|
| 46 |
|
49 |
|
| 47 |
pendingNotificationSet = {'_id':1,'user_id':1,'gcm_id':1,'notification_campaign_id':1}
|
50 |
pendingNotificationSet = {'_id':1,'user_id':1,'gcm_id':1,'notification_campaign_id':1}
|
| 48 |
pendingCampaignSet = {'_id':1,'name':1,'title':1,'message':1,'type':1,'url':1,'sql':1,'expiresat':1}
|
51 |
pendingCampaignSet = {'_id':1,'name':1,'title':1,'message':1,'type':1,'url':1,'sql':1,'expiresat':1,'image':1}
|
| 49 |
|
52 |
|
| 50 |
pendingNotificationEntryMap = {}
|
53 |
pendingNotificationEntryMap = {}
|
| 51 |
campaignUsersMap = {}
|
54 |
campaignUsersMap = {}
|
| 52 |
notificationCampaignsMap = {}
|
55 |
notificationCampaignsMap = {}
|
| 53 |
userGcmRegIdMap = {}
|
56 |
userGcmRegIdMap = {}
|
| Line 58... |
Line 61... |
| 58 |
|
61 |
|
| 59 |
db = MySQLdb.connect(options.mysqlHost,"root","shop2020","dtr" )
|
62 |
db = MySQLdb.connect(options.mysqlHost,"root","shop2020","dtr" )
|
| 60 |
cursor = db.cursor()
|
63 |
cursor = db.cursor()
|
| 61 |
|
64 |
|
| 62 |
ALL_STORES_SQL = "select * from stores"
|
65 |
ALL_STORES_SQL = "select * from stores"
|
| 63 |
GCM_REG_ID_SQL1 = "select x.user_id, x.gcm_regid, x.id, x.androidid, x.created from (select gc.* from gcm_users gc join users u on gc.user_id=u.id where gc.user_id in (%s) and gc.androidid is not null and u.activated=1 order by gc.id desc) as x group by x.user_id, x.gcm_regid, x.androidid"
|
66 |
GCM_REG_ID_SQL1 = "select x.user_id, x.gcm_regid, x.id, x.androidid, x.created, x.notification_type from (select gc.* from gcm_users gc join users u on gc.user_id=u.id where gc.user_id in (%s) and gc.androidid is not null and u.activated=1 and gc.failurecount < 2 order by gc.id desc) as x group by x.user_id, x.gcm_regid, x.androidid"
|
| 64 |
GCM_REG_ID_SQL2 = "select x.user_id, x.gcm_regid, x.id, x.androidid, x.created, x.imeinumber from (select gc.* from gcm_users gc join users u on u.id=gc.user_id where gc.user_id in (%s) and gc.androidid is null and gc.imeinumber and u.activated=1 is not null order by id desc) as x group by x.user_id, x.imeinumber"
|
67 |
GCM_REG_ID_SQL2 = "select x.user_id, x.gcm_regid, x.id, x.androidid, x.created, x.imeinumber,x.notification_type from (select gc.* from gcm_users gc join users u on u.id=gc.user_id where gc.user_id in (%s) and gc.androidid is null and gc.imeinumber is not null and gc.failurecount < 2 and u.activated=1 order by id desc) as x group by x.user_id, x.imeinumber"
|
| 65 |
FETCH_GCM_REG_SQL = "select id, gcm_regid from gcm_users where id in (%s)"
|
68 |
FETCH_GCM_REG_SQL = "select id, gcm_regid, notification_type from gcm_users where id in (%s)"
|
| 66 |
|
69 |
|
| 67 |
cursor.execute(ALL_STORES_SQL)
|
70 |
cursor.execute(ALL_STORES_SQL)
|
| 68 |
result_stores = cursor.fetchall()
|
71 |
result_stores = cursor.fetchall()
|
| 69 |
domainStoresMap = {}
|
72 |
domainStoresMap = {}
|
| 70 |
for rec in result_stores:
|
73 |
for rec in result_stores:
|
| Line 97... |
Line 100... |
| 97 |
|
100 |
|
| 98 |
def populatePendingNotificationEntriesToBeSent():
|
101 |
def populatePendingNotificationEntriesToBeSent():
|
| 99 |
global pendingNotificationEntryMap
|
102 |
global pendingNotificationEntryMap
|
| 100 |
toRefetchGcmId = []
|
103 |
toRefetchGcmId = []
|
| 101 |
offset = 0
|
104 |
offset = 0
|
| - |
|
105 |
old_campaigns = []
|
| 102 |
while(True):
|
106 |
while(True):
|
| 103 |
pendingNotificationEntries = list(get_mongo_connection(host=options.mongoHost).User.pushnotifications.find({'type':'pending'}, pendingNotificationSet).skip(offset).limit(10000))
|
107 |
pendingNotificationEntries = list(get_mongo_connection(host=options.mongoHost).User.pushnotifications.find({'type':'pending'}, pendingNotificationSet).skip(offset).limit(10000))
|
| 104 |
if not pendingNotificationEntries:
|
108 |
if not pendingNotificationEntries:
|
| 105 |
break
|
109 |
break
|
| 106 |
for entry in pendingNotificationEntries:
|
110 |
for entry in pendingNotificationEntries:
|
| - |
|
111 |
try:
|
| 107 |
gcm_regid = gcmIdGcmRegIdMap.get(entry.get('gcm_id'))
|
112 |
gcm_regid = gcmIdGcmRegIdMap.get(entry.get('gcm_id'))[0]
|
| - |
|
113 |
except:
|
| - |
|
114 |
gcm_regid = None
|
| 108 |
if gcm_regid is None:
|
115 |
if gcm_regid is None:
|
| 109 |
toRefetchGcmId.append(entry.get('gcm_id'))
|
116 |
toRefetchGcmId.append(entry.get('gcm_id'))
|
| - |
|
117 |
if not notificationCampaignsMap.has_key(long(entry['notification_campaign_id'])):
|
| - |
|
118 |
old_campaigns.append(long(entry['notification_campaign_id']))
|
| 110 |
if pendingNotificationEntryMap.has_key(entry['notification_campaign_id']):
|
119 |
if pendingNotificationEntryMap.has_key(entry['notification_campaign_id']):
|
| 111 |
entries = pendingNotificationEntryMap.get(entry['notification_campaign_id'])
|
120 |
entries = pendingNotificationEntryMap.get(entry['notification_campaign_id'])
|
| 112 |
entries.append(entry)
|
121 |
entries.append(entry)
|
| 113 |
pendingNotificationEntryMap[entry['notification_campaign_id']] = entries
|
122 |
pendingNotificationEntryMap[entry['notification_campaign_id']] = entries
|
| 114 |
else:
|
123 |
else:
|
| Line 122... |
Line 131... |
| 122 |
return
|
131 |
return
|
| 123 |
cursor.execute(FETCH_GCM_REG_SQL%(",".join(map(str,toRefetchGcmId))))
|
132 |
cursor.execute(FETCH_GCM_REG_SQL%(",".join(map(str,toRefetchGcmId))))
|
| 124 |
result_data = cursor.fetchall()
|
133 |
result_data = cursor.fetchall()
|
| 125 |
if result_data:
|
134 |
if result_data:
|
| 126 |
for data in result_data:
|
135 |
for data in result_data:
|
| 127 |
gcmIdGcmRegIdMap[data[0]] = data[1]
|
136 |
gcmIdGcmRegIdMap[data[0]] = [data[1],data[2]]
|
| - |
|
137 |
|
| - |
|
138 |
if old_campaigns:
|
| - |
|
139 |
print "yes, old campaigns exist"
|
| - |
|
140 |
campaigns = list(get_mongo_connection(host=options.mongoHost).User.notificationcampaigns.find({'_id':{"$in":old_campaigns}},pendingCampaignSet))
|
| - |
|
141 |
for campaign in campaigns:
|
| - |
|
142 |
notificationCampaignsMap[long(campaign['_id'])] = campaign
|
| 128 |
|
143 |
|
| 129 |
|
144 |
|
| 130 |
|
145 |
|
| 131 |
def populateCampaignsMap(pendingCampaigns):
|
146 |
def populateCampaignsMap(pendingCampaigns):
|
| 132 |
global campaignUsersMap
|
147 |
global campaignUsersMap
|
| Line 157... |
Line 172... |
| 157 |
|
172 |
|
| 158 |
if result_data and len(result_data)>0:
|
173 |
if result_data and len(result_data)>0:
|
| 159 |
|
174 |
|
| 160 |
for dataRec in result_data:
|
175 |
for dataRec in result_data:
|
| 161 |
|
176 |
|
| 162 |
gcmIdGcmRegIdMap[dataRec[2]] = dataRec[1]
|
177 |
gcmIdGcmRegIdMap[dataRec[2]] = [dataRec[1], dataRec[5]]
|
| 163 |
|
178 |
|
| 164 |
if userGcmRegIdMap.has_key(dataRec[0]):
|
179 |
if userGcmRegIdMap.has_key(dataRec[0]):
|
| 165 |
gcmRegIdMap = userGcmRegIdMap.get(dataRec[0])
|
180 |
gcmRegIdMap = userGcmRegIdMap.get(dataRec[0])
|
| 166 |
gcmRegIdMap[dataRec[2]]= {'gcm_regid':dataRec[1],'android_id':dataRec[3]}
|
181 |
gcmRegIdMap[dataRec[2]]= {'gcm_regid':dataRec[1],'android_id':dataRec[3]}
|
| 167 |
userGcmRegIdMap[dataRec[0]] = gcmRegIdMap
|
182 |
userGcmRegIdMap[dataRec[0]] = gcmRegIdMap
|
| 168 |
else:
|
183 |
else:
|
| 169 |
userGcmRegIdMap[dataRec[0]] = {dataRec[2]:{'gcm_regid':dataRec[1],'android_id':dataRec[3]}}
|
184 |
userGcmRegIdMap[dataRec[0]] = {dataRec[2]:{'gcm_regid':dataRec[1],'android_id':dataRec[3]}}
|
| 170 |
|
185 |
|
| 171 |
logging.debug("GCM_REG_SQL_2:- "+GCM_REG_ID_SQL2%(",".join(map(str,user_list))))
|
186 |
logging.debug("GCM_REG_SQL_2:- "+GCM_REG_ID_SQL2%(",".join(map(str,user_list))))
|
| 172 |
cursor.execute(GCM_REG_ID_SQL2%(",".join(map(str,user_list))))
|
187 |
cursor.execute(GCM_REG_ID_SQL2%(",".join(map(str,user_list))))
|
| 173 |
result_data = cursor.fetchall()
|
188 |
result_data = cursor.fetchall()
|
| 174 |
for dataRec in result_data:
|
189 |
for dataRec in result_data:
|
| 175 |
|
190 |
|
| 176 |
gcmIdGcmRegIdMap[dataRec[2]] = dataRec[1]
|
191 |
gcmIdGcmRegIdMap[dataRec[2]] = [dataRec[1], dataRec[6]]
|
| 177 |
|
192 |
|
| 178 |
if userGcmRegIdMap.has_key(dataRec[0]):
|
193 |
if userGcmRegIdMap.has_key(dataRec[0]):
|
| 179 |
gcmRegIdMap = userGcmRegIdMap.get(dataRec[0])
|
194 |
gcmRegIdMap = userGcmRegIdMap.get(dataRec[0])
|
| 180 |
gcmRegIdMap[dataRec[2]]= {'gcm_regid':dataRec[1],'android_id':dataRec[3]}
|
195 |
gcmRegIdMap[dataRec[2]]= {'gcm_regid':dataRec[1],'android_id':dataRec[3]}
|
| 181 |
userGcmRegIdMap[dataRec[0]] = gcmRegIdMap
|
196 |
userGcmRegIdMap[dataRec[0]] = gcmRegIdMap
|
| Line 236... |
Line 251... |
| 236 |
for record in recordsList:
|
251 |
for record in recordsList:
|
| 237 |
try:
|
252 |
try:
|
| 238 |
#userGcmRegIdDetails = userGcmRegIdMap.get(long(record.get('user_id')))
|
253 |
#userGcmRegIdDetails = userGcmRegIdMap.get(long(record.get('user_id')))
|
| 239 |
campaign = notificationCampaignsMap.get(long(record.get('notification_campaign_id')))
|
254 |
campaign = notificationCampaignsMap.get(long(record.get('notification_campaign_id')))
|
| 240 |
gcm_id = record.get('gcm_id')
|
255 |
gcm_id = record.get('gcm_id')
|
| 241 |
gcm_regid = gcmIdGcmRegIdMap.get(gcm_id)
|
256 |
gcm_regid = gcmIdGcmRegIdMap.get(gcm_id)[0]
|
| 242 |
if gcm_regid is None:
|
257 |
if gcm_regid is None:
|
| 243 |
print "Ideally should never raise"
|
258 |
print "Ideally should never raise"
|
| 244 |
raise
|
259 |
raise
|
| 245 |
except:
|
260 |
except:
|
| 246 |
logging.debug('Error while getting GCM Details for User Id:- '+ str(record.get('user_id'))+" and Notification Id:- "+str(record.get('notification_campaign_id')))
|
261 |
logging.debug('Error while getting GCM Details for User Id:- '+ str(record.get('user_id'))+" and Notification Id:- "+str(record.get('notification_campaign_id')))
|
| Line 272... |
Line 287... |
| 272 |
result_url = campaign.get('url').strip()
|
287 |
result_url = campaign.get('url').strip()
|
| 273 |
if campaign.get('url') is None or str(campaign.get('url'))=='':
|
288 |
if campaign.get('url') is None or str(campaign.get('url'))=='':
|
| 274 |
result_url = 'http://api.profittill.com/deals?user_id='+str(record.get('user_id'))
|
289 |
result_url = 'http://api.profittill.com/deals?user_id='+str(record.get('user_id'))
|
| 275 |
data = {"message":campaign.get('message'),"cid":str(campaign.get('_id'))+"_"+str(record.get('_id')),"title":campaign.get('title'),
|
290 |
data = {"message":campaign.get('message'),"cid":str(campaign.get('_id'))+"_"+str(record.get('_id')),"title":campaign.get('title'),
|
| 276 |
"type":campaign.get('type'),"url":result_url.strip(),"vibrate":1,"sound":1,"largeIcon":"large_icon",
|
291 |
"type":campaign.get('type'),"url":result_url.strip(),"vibrate":1,"sound":1,"largeIcon":"large_icon",
|
| 277 |
"smallIcon":"small_icon","priority":"high","time_to_live":long(campaign.get('expiresat'))-long(time.mktime(datetime.now().timetuple()))}
|
292 |
"smallIcon":"small_icon","priority":"high","time_to_live":long(campaign.get('expiresat'))-long(time.mktime(datetime.now().timetuple())),"image":campaign.get("image")}
|
| 278 |
|
293 |
|
| 279 |
post_data = {}
|
294 |
post_data = {}
|
| 280 |
|
295 |
|
| 281 |
post_data['data'] = data
|
296 |
post_data['data'] = data
|
| - |
|
297 |
|
| - |
|
298 |
if gcmIdGcmRegIdMap.get(gcm_id)[1] == 'gcm':
|
| - |
|
299 |
|
| 282 |
regIds = []
|
300 |
regIds = []
|
| 283 |
regIds.append(gcm_regid)
|
301 |
regIds.append(gcm_regid)
|
| 284 |
post_data['registration_ids'] = regIds
|
302 |
post_data['registration_ids'] = regIds
|
| - |
|
303 |
|
| - |
|
304 |
post_data_json = json.dumps(post_data)
|
| - |
|
305 |
logging.debug('User Id:- '+str(record.get('user_id'))+' Post Data Json :- '+str(post_data_json))
|
| - |
|
306 |
response = requests.post(GCM_URL, data=post_data_json, headers=headers_gcm)
|
| - |
|
307 |
|
| - |
|
308 |
else:
|
| 285 |
|
309 |
|
| - |
|
310 |
post_data['to'] = gcm_regid
|
| - |
|
311 |
post_data['priority'] = 'high'
|
| - |
|
312 |
post_data['sound'] = 'default'
|
| - |
|
313 |
post_data['time_to_live'] = 2419200
|
| 286 |
post_data_json = json.dumps(post_data)
|
314 |
post_data_json = json.dumps(post_data)
|
| 287 |
logging.debug('User Id:- '+str(record.get('user_id'))+' Post Data Json :- '+str(post_data_json))
|
315 |
logging.debug('User Id:- '+str(record.get('user_id'))+' Post Data Json :- '+str(post_data_json))
|
| 288 |
response = requests.post(GCM_URL, data=post_data_json, headers=headers)
|
316 |
response = requests.post(FCM_URL, data=post_data_json, headers=headers_fcm)
|
| - |
|
317 |
|
| 289 |
logging.debug('User Id:- '+str(record.get('user_id'))+' GCM_ID:- '+str(gcm_id)+' Response :-'+str(response.text))
|
318 |
logging.debug('User Id:- '+str(record.get('user_id'))+' GCM_ID:- '+str(gcm_id)+' Response :-'+str(response.text))
|
| 290 |
result = json.loads(response.text)
|
319 |
result = json.loads(response.text)
|
| 291 |
if result["success"]:
|
320 |
if result["success"]:
|
| 292 |
get_mongo_connection(host=options.mongoHost).User.pushnotifications.update({'_id':record.get('_id')},{"$set":{'message':'success','type':'sent','sent_timestamp':to_java_date(datetime.now())}})
|
321 |
get_mongo_connection(host=options.mongoHost).User.pushnotifications.update({'_id':record.get('_id')},{"$set":{'message':'success','type':'sent','sent_timestamp':to_java_date(datetime.now())}})
|
| 293 |
else:
|
322 |
else:
|
| Line 345... |
Line 374... |
| 345 |
db.close()
|
374 |
db.close()
|
| 346 |
|
375 |
|
| 347 |
|
376 |
|
| 348 |
if __name__ == '__main__':
|
377 |
if __name__ == '__main__':
|
| 349 |
main()
|
378 |
main()
|
| 350 |
|
- |
|
| 351 |
|
379 |
|