| Line 67... |
Line 67... |
| 67 |
|
67 |
|
| 68 |
def handleCampaignRequest(threadName, recordsList ):
|
68 |
def handleCampaignRequest(threadName, recordsList ):
|
| 69 |
for record in recordsList:
|
69 |
for record in recordsList:
|
| 70 |
userGcmRegIdDetails = userGcmRegIdMap.get(long(record.get('user_id')))
|
70 |
userGcmRegIdDetails = userGcmRegIdMap.get(long(record.get('user_id')))
|
| 71 |
campaign = notificationCampaignsMap.get(record.get('notification_campaign_id'))
|
71 |
campaign = notificationCampaignsMap.get(record.get('notification_campaign_id'))
|
| - |
|
72 |
gcm_id = record.get('gcm_id')
|
| 72 |
for gcm_id, detailsMap in userGcmRegIdDetails.items():
|
73 |
detailsMap = userGcmRegIdDetails.get(gcm_id)
|
| 73 |
result_url = ""
|
74 |
result_url = ""
|
| 74 |
if campaign.get('type')=='url':
|
75 |
if campaign.get('type')=='url':
|
| 75 |
parsed_uri = urlparse(campaign.get('url'))
|
76 |
parsed_uri = urlparse(campaign.get('url'))
|
| 76 |
domain = '{uri.netloc}'.format(uri=parsed_uri)
|
77 |
domain = '{uri.netloc}'.format(uri=parsed_uri)
|
| 77 |
logging.debug('Affiliate Domain:-'+str(domain))
|
78 |
logging.debug('Affiliate Domain:-'+str(domain))
|
| 78 |
logging.debug('User Id:-'+str(record.get('user_id'))+' And GCM Reg Id:- '+ str(detailsMap.get('gcm_regid')))
|
79 |
logging.debug('User Id:-'+str(record.get('user_id'))+' And GCM Reg Id:- '+ str(detailsMap.get('gcm_regid')))
|
| 79 |
store = domainStoresMap.get(domain)
|
80 |
store = domainStoresMap.get(domain)
|
| 80 |
if store is not None:
|
81 |
if store is not None:
|
| 81 |
url_params = { 'url' : campaign.get('url'), 'userId' : record.get('user_id'), 'storeId' : store[0] }
|
82 |
url_params = { 'url' : campaign.get('url'), 'userId' : record.get('user_id'), 'storeId' : store[0] }
|
| 82 |
encoded_url_params = urllib.urlencode(url_params)
|
83 |
encoded_url_params = urllib.urlencode(url_params)
|
| 83 |
DTR_API_BASIC_AUTH = base64.encodestring('%s:%s' % ("dtr", "dtr18Feb2015")).replace('\n', '')
|
84 |
DTR_API_BASIC_AUTH = base64.encodestring('%s:%s' % ("dtr", "dtr18Feb2015")).replace('\n', '')
|
| 84 |
|
85 |
|
| 85 |
pushpostrequest = urllib2.Request('http://api.profittill.com/pushnotifications/generateAffiliateUrl', encoded_url_params, headers=aff_url_headers)
|
86 |
pushpostrequest = urllib2.Request('http://api.profittill.com/pushnotifications/generateAffiliateUrl', encoded_url_params, headers=aff_url_headers)
|
| 86 |
pushpostrequest.add_header("Authorization", "Basic %s" % DTR_API_BASIC_AUTH)
|
87 |
pushpostrequest.add_header("Authorization", "Basic %s" % DTR_API_BASIC_AUTH)
|
| 87 |
json_result = json.loads(urllib2.urlopen(pushpostrequest).read())
|
88 |
json_result = json.loads(urllib2.urlopen(pushpostrequest).read())
|
| 88 |
result_url = json_result['url']
|
89 |
result_url = json_result['url']
|
| 89 |
logging.debug('User Id:-'+str(record.get('user_id'))+' Notification Url:- '+ str(result_url))
|
90 |
logging.debug('User Id:-'+str(record.get('user_id'))+' Notification Url:- '+ str(result_url))
|
| 90 |
else:
|
- |
|
| 91 |
queryString = urlparse(campaign.get('url').strip()).query
|
- |
|
| 92 |
parsed_url = parse_qs(queryString)
|
- |
|
| 93 |
if not parsed_url.has_key('user_id'):
|
- |
|
| 94 |
if len(queryString)>0:
|
- |
|
| 95 |
result_url = campaign.get('url').strip()+'&user_id='+str(record.get('user_id'))
|
- |
|
| 96 |
logging.debug('User Id:-'+str(record.get('user_id'))+' Notification Url:- '+ str(result_url))
|
- |
|
| 97 |
else:
|
- |
|
| 98 |
result_url = campaign.get('url').strip()+'?user_id='+str(record.get('user_id'))
|
- |
|
| 99 |
logging.debug('User Id:-'+str(record.get('user_id'))+' Notification Url:- '+ str(result_url))
|
- |
|
| 100 |
else:
|
- |
|
| 101 |
logging.debug('User Id:-'+str(record.get('user_id'))+' Notification Url:- '+ str(record.get('user_id')))
|
- |
|
| 102 |
if campaign.get('url') is None or str(campaign.get('url'))=='':
|
- |
|
| 103 |
result_url = 'http://api.profittill.com/deals?user_id='+str(record.get('user_id'))
|
- |
|
| 104 |
data = {"message":campaign.get('message'),"cid":str(campaign.get('_id'))+"_"+str(record.get('_id')),"title":campaign.get('title'),
|
- |
|
| 105 |
"type":campaign.get('type'),"url":result_url.strip(),"vibrate":1,"sound":1,"largeIcon":"large_icon",
|
- |
|
| 106 |
"smallIcon":"small_icon","priority":"high","time_to_live":long(campaign.get('expiresat'))-long(time.mktime(datetime.now().timetuple()))}
|
- |
|
| 107 |
|
- |
|
| 108 |
post_data = {}
|
- |
|
| 109 |
|
- |
|
| 110 |
post_data['data'] = data
|
- |
|
| 111 |
regIds = []
|
- |
|
| 112 |
regIds.append(detailsMap.get('gcm_regid'))
|
- |
|
| 113 |
post_data['registration_ids'] = regIds
|
- |
|
| 114 |
|
- |
|
| 115 |
post_data_json = json.dumps(post_data)
|
- |
|
| 116 |
logging.debug('User Id:- '+str(record.get('user_id'))+' Post Data Json :- '+str(post_data_json))
|
- |
|
| 117 |
response = requests.post(GCM_URL, data=post_data_json, headers=headers)
|
- |
|
| 118 |
logging.debug('User Id:- '+str(record.get('user_id'))+' GCM_ID:- '+str(gcm_id)+' Response :-'+str(response.text))
|
- |
|
| 119 |
result = json.loads(response.text)
|
- |
|
| 120 |
if result["success"]:
|
- |
|
| 121 |
get_mongo_connection(host=mongoHost).User.pushnotifications.update({'_id':record.get('_id')},{"$set":{'message':'success','type':'sent','sent_timestamp':to_java_date(datetime.now())}})
|
- |
|
| 122 |
logging.debug('User Id:- '+str(record.get('user_id'))+' GCM_ID:- '+str(gcm_id)+' Update Response :- Notification Success True')
|
- |
|
| 123 |
else:
|
91 |
else:
|
| - |
|
92 |
queryString = urlparse(campaign.get('url').strip()).query
|
| - |
|
93 |
parsed_url = parse_qs(queryString)
|
| - |
|
94 |
if not parsed_url.has_key('user_id'):
|
| - |
|
95 |
if len(queryString)>0:
|
| - |
|
96 |
result_url = campaign.get('url').strip()+'&user_id='+str(record.get('user_id'))
|
| - |
|
97 |
logging.debug('User Id:-'+str(record.get('user_id'))+' Notification Url:- '+ str(result_url))
|
| - |
|
98 |
else:
|
| - |
|
99 |
result_url = campaign.get('url').strip()+'?user_id='+str(record.get('user_id'))
|
| - |
|
100 |
logging.debug('User Id:-'+str(record.get('user_id'))+' Notification Url:- '+ str(result_url))
|
| - |
|
101 |
else:
|
| - |
|
102 |
logging.debug('User Id:-'+str(record.get('user_id'))+' Notification Url:- '+ str(record.get('user_id')))
|
| - |
|
103 |
if campaign.get('url') is None or str(campaign.get('url'))=='':
|
| - |
|
104 |
result_url = 'http://api.profittill.com/deals?user_id='+str(record.get('user_id'))
|
| - |
|
105 |
data = {"message":campaign.get('message'),"cid":str(campaign.get('_id'))+"_"+str(record.get('_id')),"title":campaign.get('title'),
|
| - |
|
106 |
"type":campaign.get('type'),"url":result_url.strip(),"vibrate":1,"sound":1,"largeIcon":"large_icon",
|
| - |
|
107 |
"smallIcon":"small_icon","priority":"high","time_to_live":long(campaign.get('expiresat'))-long(time.mktime(datetime.now().timetuple()))}
|
| - |
|
108 |
|
| - |
|
109 |
post_data = {}
|
| - |
|
110 |
|
| - |
|
111 |
post_data['data'] = data
|
| - |
|
112 |
regIds = []
|
| - |
|
113 |
regIds.append(detailsMap.get('gcm_regid'))
|
| - |
|
114 |
post_data['registration_ids'] = regIds
|
| - |
|
115 |
|
| - |
|
116 |
post_data_json = json.dumps(post_data)
|
| - |
|
117 |
logging.debug('User Id:- '+str(record.get('user_id'))+' Post Data Json :- '+str(post_data_json))
|
| - |
|
118 |
response = requests.post(GCM_URL, data=post_data_json, headers=headers)
|
| - |
|
119 |
logging.debug('User Id:- '+str(record.get('user_id'))+' GCM_ID:- '+str(gcm_id)+' Response :-'+str(response.text))
|
| - |
|
120 |
result = json.loads(response.text)
|
| - |
|
121 |
if result["success"]:
|
| - |
|
122 |
get_mongo_connection(host=mongoHost).User.pushnotifications.update({'_id':record.get('_id')},{"$set":{'message':'success','type':'sent','sent_timestamp':to_java_date(datetime.now())}})
|
| - |
|
123 |
logging.debug('User Id:- '+str(record.get('user_id'))+' GCM_ID:- '+str(gcm_id)+' Update Response :- Notification Success True')
|
| - |
|
124 |
else:
|
| 124 |
get_mongo_connection(host=mongoHost).User.pushnotifications.update({'_id':record.get('_id')},{"$set":{'message':result["results"][0]["error"],'type':'failed','sent_timestamp':to_java_date(datetime.now())}})
|
125 |
get_mongo_connection(host=mongoHost).User.pushnotifications.update({'_id':record.get('_id')},{"$set":{'message':result["results"][0]["error"],'type':'failed','sent_timestamp':to_java_date(datetime.now())}})
|
| 125 |
logging.debug('User Id:- '+str(record.get('user_id'))+' GCM_ID:- '+str(gcm_id)+' Update Response :- Notification Fail True')
|
126 |
logging.debug('User Id:- '+str(record.get('user_id'))+' GCM_ID:- '+str(gcm_id)+' Update Response :- Notification Fail True')
|
| 126 |
updateGcmUserSql = "update gcm_users set failurecount=failurecount+1 where gcm_regid='%s'"%(detailsMap.get('gcm_regid'))
|
127 |
updateGcmUserSql = "update gcm_users set failurecount=failurecount+1 where gcm_regid='%s'"%(detailsMap.get('gcm_regid'))
|
| 127 |
logging.debug('Update GCM User Query :-'+str(updateGcmUserSql))
|
128 |
logging.debug('Update GCM User Query :-'+str(updateGcmUserSql))
|
| 128 |
try:
|
129 |
try:
|
| 129 |
dtrdb = MySQLdb.connect('localhost',"root","shop2020","dtr" )
|
130 |
dtrdb = MySQLdb.connect('localhost',"root","shop2020","dtr" )
|
| 130 |
cursor = dtrdb.cursor()
|
131 |
cursor = dtrdb.cursor()
|
| 131 |
cursor.execute(updateGcmUserSql)
|
132 |
cursor.execute(updateGcmUserSql)
|
| 132 |
dtrdb.commit()
|
133 |
dtrdb.commit()
|
| 133 |
session.commit()
|
134 |
session.commit()
|
| 134 |
dtrdb.close()
|
135 |
dtrdb.close()
|
| 135 |
except:
|
136 |
except:
|
| 136 |
dtrdb.rollback()
|
137 |
dtrdb.rollback()
|
| 137 |
dtrdb.close()
|
138 |
dtrdb.close()
|
| 138 |
|
139 |
|
| 139 |
def chunks(l, n):
|
140 |
def chunks(l, n):
|
| 140 |
"""Yield successive n-sized chunks from l."""
|
141 |
"""Yield successive n-sized chunks from l."""
|
| 141 |
for i in xrange(0, len(l), n):
|
142 |
for i in xrange(0, len(l), n):
|
| 142 |
yield l[i:i+n]
|
143 |
yield l[i:i+n]
|