| Line 116... |
Line 116... |
| 116 |
notificationRecord.url = json_result['url']
|
116 |
notificationRecord.url = json_result['url']
|
| 117 |
logging.debug('User Id:-'+str(notificationRecord.userId)+' Notification Url:- '+ str(notificationRecord.url))
|
117 |
logging.debug('User Id:-'+str(notificationRecord.userId)+' Notification Url:- '+ str(notificationRecord.url))
|
| 118 |
else:
|
118 |
else:
|
| 119 |
notificationRecord.url = notificationRecord.url+'?user_id='+str(notificationRecord.userId)
|
119 |
notificationRecord.url = notificationRecord.url+'?user_id='+str(notificationRecord.userId)
|
| 120 |
logging.debug('User Id:-'+str(notificationRecord.userId)+' Notification Url:- '+ str(notificationRecord.url))
|
120 |
logging.debug('User Id:-'+str(notificationRecord.userId)+' Notification Url:- '+ str(notificationRecord.url))
|
| 121 |
|
121 |
if notificationRecord.url is None or str(notificationRecord.url)=='':
|
| - |
|
122 |
notificationRecord.url = 'http://api.profittill.com/deals?user_id='+str(notificationRecord.userId)
|
| 122 |
data = {"message":notificationRecord.message,"cid":notificationRecord.campaignId,"title":notificationRecord.title,
|
123 |
data = {"message":notificationRecord.message,"cid":notificationRecord.campaignId,"title":notificationRecord.title,
|
| 123 |
"type":notificationRecord.type,"url":notificationRecord.url,"vibrate":1,"sound":1,"largeIcon":"large_icon",
|
124 |
"type":notificationRecord.type,"url":notificationRecord.url,"vibrate":1,"sound":1,"largeIcon":"large_icon",
|
| 124 |
"smallIcon":"small_icon","priority":"high","time_to_live":long(time.mktime(notificationRecord.expiresAt.timetuple()))-long(time.mktime(datetime.datetime.now().timetuple()))}
|
125 |
"smallIcon":"small_icon","priority":"high","time_to_live":long(time.mktime(notificationRecord.expiresAt.timetuple()))-long(time.mktime(datetime.datetime.now().timetuple()))}
|
| 125 |
|
126 |
|
| 126 |
post_data = {}
|
127 |
post_data = {}
|
| 127 |
|
128 |
|
| 128 |
post_data['data'] = data
|
129 |
post_data['data'] = data
|
| 129 |
regIds = []
|
130 |
regIds = []
|
| 130 |
regIds.append(notificationRecord.gcmRegId)
|
131 |
regIds.append(notificationRecord.gcmRegId)
|
| 131 |
post_data['registration_ids'] = regIds
|
132 |
post_data['registration_ids'] = regIds
|
| 132 |
|
133 |
|
| 133 |
post_data_json = json.dumps(post_data)
|
134 |
post_data_json = json.dumps(post_data)
|
| 134 |
logging.debug('User Id:- '+str(notificationRecord.userId)+' Post Data Json :- '+str(post_data_json))
|
135 |
logging.debug('User Id:- '+str(notificationRecord.userId)+' Post Data Json :- '+str(post_data_json))
|
| 135 |
|
136 |
|
| 136 |
response = requests.post(GCM_URL, data=post_data_json, headers=headers)
|
137 |
response = requests.post(GCM_URL, data=post_data_json, headers=headers)
|
| 137 |
logging.debug('User Id:- '+str(notificationRecord.userId)+' Response :-'+str(response.text))
|
138 |
logging.debug('User Id:- '+str(notificationRecord.userId)+' Response :-'+str(response.text))
|
| 138 |
result = json.loads(response.text)
|
139 |
result = json.loads(response.text)
|
| 139 |
|
140 |
|
| 140 |
if result["success"]:
|
141 |
if result["success"]:
|
| 141 |
update_params = { 'user_id' : notificationRecord.userId, 'notification_campaign_id' : notificationRecord.campaignId, 'type' : 'sent', 'status':1, 'message':'success' }
|
142 |
update_params = { 'user_id' : notificationRecord.userId, 'notification_campaign_id' : notificationRecord.campaignId, 'type' : 'sent', 'status':1, 'message':'success' }
|
| 142 |
encoded_update_params = urllib.urlencode(update_params)
|
143 |
encoded_update_params = urllib.urlencode(update_params)
|
| 143 |
updateReq = urllib2.Request(PUSH_NOTIFICATIONS_UPDATE_URL+encoded_update_params)
|
144 |
updateReq = urllib2.Request(PUSH_NOTIFICATIONS_UPDATE_URL+encoded_update_params)
|
| 144 |
updateResponse = urllib2.urlopen(updateReq)
|
145 |
updateResponse = urllib2.urlopen(updateReq)
|
| 145 |
response_str = updateResponse.read()
|
146 |
response_str = updateResponse.read()
|
| 146 |
logging.debug('User Id:- '+str(notificationRecord.userId)+' Update Response :-'+str(response_str))
|
147 |
logging.debug('User Id:- '+str(notificationRecord.userId)+' Update Response :-'+str(response_str))
|
| 147 |
else:
|
148 |
else:
|
| 148 |
update_params = { 'user_id' : notificationRecord.userId, 'notification_campaign_id' : notificationRecord.campaignId, 'type' : 'sent', 'status':0, 'message':result["results"][0]["error"] }
|
149 |
update_params = { 'user_id' : notificationRecord.userId, 'notification_campaign_id' : notificationRecord.campaignId, 'type' : 'sent', 'status':0, 'message':result["results"][0]["error"] }
|
| 149 |
encoded_update_params = urllib.urlencode(update_params)
|
150 |
encoded_update_params = urllib.urlencode(update_params)
|
| 150 |
updateReq = urllib2.Request(PUSH_NOTIFICATIONS_UPDATE_URL+encoded_update_params)
|
151 |
updateReq = urllib2.Request(PUSH_NOTIFICATIONS_UPDATE_URL+encoded_update_params)
|
| 151 |
updateResponse = urllib2.urlopen(updateReq)
|
152 |
updateResponse = urllib2.urlopen(updateReq)
|
| 152 |
response_str = updateResponse.read()
|
153 |
response_str = updateResponse.read()
|
| 153 |
logging.debug('User Id:- '+str(notificationRecord.userId)+' Update Response :-'+str(response_str))
|
154 |
logging.debug('User Id:- '+str(notificationRecord.userId)+' Update Response :-'+str(response_str))
|
| 154 |
|
155 |
|
| 155 |
updateGcmUserSql = "update gcm_users set failurecount=failurecount+1 where gcm_regid='%s'"%(notificationRecord.gcmRegId)
|
156 |
updateGcmUserSql = "update gcm_users set failurecount=failurecount+1 where gcm_regid='%s'"%(notificationRecord.gcmRegId)
|
| 156 |
logging.debug('Update GCM User Query :-'+str(updateGcmUserSql))
|
157 |
logging.debug('Update GCM User Query :-'+str(updateGcmUserSql))
|
| 157 |
try:
|
158 |
try:
|
| 158 |
dtrdb = MySQLdb.connect('localhost',"root","shop2020","dtr" )
|
159 |
dtrdb = MySQLdb.connect('localhost',"root","shop2020","dtr" )
|
| 159 |
cursor = dtrdb.cursor()
|
160 |
cursor = dtrdb.cursor()
|
| 160 |
cursor.execute(updateGcmUserSql)
|
161 |
cursor.execute(updateGcmUserSql)
|
| 161 |
dtrdb.commit()
|
162 |
dtrdb.commit()
|
| 162 |
session.commit()
|
163 |
session.commit()
|
| 163 |
dtrdb.close()
|
164 |
dtrdb.close()
|
| 164 |
except:
|
165 |
except:
|
| 165 |
dtrdb.rollback()
|
166 |
dtrdb.rollback()
|
| 166 |
dtrdb.close()
|
167 |
dtrdb.close()
|
| 167 |
#time.sleep(2)
|
168 |
#time.sleep(2)
|
| 168 |
|
169 |
|
| 169 |
def chunks(l, n):
|
170 |
def chunks(l, n):
|
| 170 |
"""Yield successive n-sized chunks from l."""
|
171 |
"""Yield successive n-sized chunks from l."""
|
| 171 |
for i in xrange(0, len(l), n):
|
172 |
for i in xrange(0, len(l), n):
|
| Line 195... |
Line 196... |
| 195 |
if notificationRec.get('notification_campaign_id') not in campaignIdList:
|
196 |
if notificationRec.get('notification_campaign_id') not in campaignIdList:
|
| 196 |
campaignIdList.append(notificationRec.get('notification_campaign_id'))
|
197 |
campaignIdList.append(notificationRec.get('notification_campaign_id'))
|
| 197 |
|
198 |
|
| 198 |
usersList.append(0)
|
199 |
usersList.append(0)
|
| 199 |
campaignIdList.append(0)
|
200 |
campaignIdList.append(0)
|
| - |
|
201 |
logging.debug('Starting Push Notification Job....'+str(datetime.datetime.now()))
|
| 200 |
|
202 |
|
| 201 |
if len(usersList) >1 and len(campaignIdList)>1:
|
203 |
if len(usersList) >1 and len(campaignIdList)>1:
|
| 202 |
cursor.execute(GCM_REG_ID_SQL%(str(tuple(usersList))))
|
204 |
cursor.execute(GCM_REG_ID_SQL%(str(tuple(usersList))))
|
| 203 |
result_data = cursor.fetchall()
|
205 |
result_data = cursor.fetchall()
|
| 204 |
|
206 |
|
| Line 217... |
Line 219... |
| 217 |
print len(campaign_receivers_list)
|
219 |
print len(campaign_receivers_list)
|
| 218 |
for sublist in campaign_receivers_list:
|
220 |
for sublist in campaign_receivers_list:
|
| 219 |
thread = NotificationThread(count, "Thread-"+str(count), sublist)
|
221 |
thread = NotificationThread(count, "Thread-"+str(count), sublist)
|
| 220 |
thread.start()
|
222 |
thread.start()
|
| 221 |
count = count +1
|
223 |
count = count +1
|
| - |
|
224 |
|
| - |
|
225 |
logging.debug('Stopping Push Notification Job....'+str(datetime.datetime.now()))
|
| 222 |
|
226 |
|
| 223 |
db.close()
|
227 |
db.close()
|
| 224 |
|
228 |
|
| 225 |
if __name__=='__main__':
|
229 |
if __name__=='__main__':
|
| 226 |
main()
|
230 |
main()
|