Subversion Repositories SmartDukaan

Rev

Rev 16386 | Rev 17229 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 16386 Rev 17228
Line 3... Line 3...
3
import threading
3
import threading
4
import time
4
import time
5
import datetime
5
import datetime
6
 
6
 
7
import MySQLdb
7
import MySQLdb
8
from dtr.storage import DataService
-
 
9
from dtr.storage.DataService import Pushnotifications
-
 
10
from elixir import *
8
from elixir import *
11
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
9
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
12
from sqlalchemy.sql import func
10
from sqlalchemy.sql import func
13
from sqlalchemy.sql.expression import and_, or_, desc, not_, distinct, cast, \
11
from sqlalchemy.sql.expression import and_, or_, desc, not_, distinct, cast, \
14
    between
12
    between
Line 18... Line 16...
18
import optparse
16
import optparse
19
import urllib2
17
import urllib2
20
import base64
18
import base64
21
import urllib
19
import urllib
22
import logging
20
import logging
-
 
21
from dtr.utils.utils import get_mongo_connection
23
 
22
 
24
GCM_URL = "https://android.googleapis.com/gcm/send"
23
GCM_URL = "https://android.googleapis.com/gcm/send"
25
GOOGLE_API_KEY = "AIzaSyDw1qBnmxtnfR9NqBewryQ-yo3cG2ravGM"
24
GOOGLE_API_KEY = "AIzaSyDw1qBnmxtnfR9NqBewryQ-yo3cG2ravGM"
-
 
25
PENDING_PUSH_NOTIFICATION_URL = "http://45.33.50.227:3001/getPendingNotifications"
-
 
26
PUSH_NOTIFICATIONS_UPDATE_URL = "http://45.33.50.227:3001/updatePushNotification/?"
26
headers = {'content-type':'application/json', "authorization":"key=" + GOOGLE_API_KEY}
27
headers = {'content-type':'application/json', "authorization":"key=" + GOOGLE_API_KEY}
27
aff_url_headers = { 
28
aff_url_headers = { 
28
            'User-agent':'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36',
29
            'User-agent':'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36',
29
            'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',      
30
            'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',      
30
            'Accept-Language' : 'en-US,en;q=0.8',                     
31
            'Accept-Language' : 'en-US,en;q=0.8',                     
Line 32... Line 33...
32
            'Connection':'keep-alive'
33
            'Connection':'keep-alive'
33
        }
34
        }
34
 
35
 
35
db = MySQLdb.connect('localhost',"root","shop2020","dtr" )
36
db = MySQLdb.connect('localhost',"root","shop2020","dtr" )
36
cursor = db.cursor()
37
cursor = db.cursor()
-
 
38
userGcmRegIdMap = {}
-
 
39
campaignsMap = {}
37
 
40
 
38
PUSH_NOTIFICATIONS_SQL = "select p.id, p.user_id, n.*, g.gcm_regid from pushnotifications p join notification_campaigns n on p.notification_campaign_id = n.id left outer join (select * from (select * from gcm_users order by id desc) as X group by user_id) as g on p.user_id = g.user_id where p.type ='pending'"
-
 
39
ALL_STORES_SQL = "select * from stores"
41
ALL_STORES_SQL = "select * from stores"
-
 
42
GCM_REG_ID_SQL = "select x.user_id, x.gcm_regid from (select * from gcm_users where user_id in %s order by id desc) as x group by x.user_id"
-
 
43
CAMPAIGNS_SQL = "select * from notification_campaigns where id in %s order by id"
-
 
44
 
40
 
45
 
41
cursor.execute(ALL_STORES_SQL)
46
cursor.execute(ALL_STORES_SQL)
42
result_stores = cursor.fetchall()
47
result_stores = cursor.fetchall()
43
domainStoresMap = {}
48
domainStoresMap = {}
44
for rec in result_stores:
49
for rec in result_stores:
Line 89... Line 94...
89
        handleCampaignRequest(self.name, self.recordsList)
94
        handleCampaignRequest(self.name, self.recordsList)
90
        logging.debug('Completed')
95
        logging.debug('Completed')
91
 
96
 
92
def handleCampaignRequest(threadName, recordsList ):
97
def handleCampaignRequest(threadName, recordsList ):
93
    for record in recordsList:
98
    for record in recordsList:
-
 
99
        campaignRecord = campaignsMap.get(record.get('notification_campaign_id'))
94
        notificationRecord = NotificationRecord(record[0], record[1], record[2], record[3], record[4], record[5], record[6], record[7], record[9], record[10], record[11], record[12])
100
        notificationRecord = NotificationRecord(record.get('_id'), record.get('user_id'), record.get('notification_campaign_id'), campaignRecord[1], campaignRecord[2], campaignRecord[3], campaignRecord[4], campaignRecord[5], campaignRecord[7], campaignRecord[8], campaignRecord[9], userGcmRegIdMap.get(record.get('user_id')))
95
        if notificationRecord.type=='url':
101
        if notificationRecord.type=='url':
96
            parsed_uri = urlparse(notificationRecord.url)
102
            parsed_uri = urlparse(notificationRecord.url)
97
            domain = '{uri.netloc}'.format(uri=parsed_uri)
103
            domain = '{uri.netloc}'.format(uri=parsed_uri)
98
            logging.debug('Affiliate Domain:-'+str(domain))
104
            logging.debug('Affiliate Domain:-'+str(domain))
99
            logging.debug('User Id:-'+str(notificationRecord.userId)+' And GCM Reg Id:- '+ str(notificationRecord.gcmRegId))
105
            logging.debug('User Id:-'+str(notificationRecord.userId)+' And GCM Reg Id:- '+ str(notificationRecord.gcmRegId))
Line 128... Line 134...
128
            logging.debug('User Id:- '+str(notificationRecord.userId)+' Post Data Json :- '+str(post_data_json))
134
            logging.debug('User Id:- '+str(notificationRecord.userId)+' Post Data Json :- '+str(post_data_json))
129
            
135
            
130
            response = requests.post(GCM_URL, data=post_data_json, headers=headers)
136
            response = requests.post(GCM_URL, data=post_data_json, headers=headers)
131
            logging.debug('User Id:- '+str(notificationRecord.userId)+' Response :-'+str(response.text))
137
            logging.debug('User Id:- '+str(notificationRecord.userId)+' Response :-'+str(response.text))
132
            result = json.loads(response.text)
138
            result = json.loads(response.text)
133
            pushnotification = Pushnotifications.get_by(id=notificationRecord.pushNotificationId)
-
 
134
            
139
            
135
            if result["success"]:
140
            if result["success"]:
136
                if pushnotification.message is None or pushnotification.message!="success":
141
                update_params = { 'user_id' : notificationRecord.userId,  'notification_campaign_id' : notificationRecord.campaignId, 'type' : 'sent', 'status':1, 'message':'success' }
137
                    pushnotification.type = "sent"
142
                encoded_update_params = urllib.urlencode(update_params)
138
                    pushnotification.status = True
143
                updateReq = urllib2.Request(PUSH_NOTIFICATIONS_UPDATE_URL+encoded_update_params)
139
                    pushnotification.message = "success"
144
                updateResponse = urllib2.urlopen(updateReq)
140
                    session.commit()
145
                response_str = updateResponse.read()
-
 
146
                logging.debug('User Id:- '+str(notificationRecord.userId)+' Update Response :-'+str(response_str))
141
            else:
147
            else:
-
 
148
                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
                updateReq = urllib2.Request(PUSH_NOTIFICATIONS_UPDATE_URL+encoded_update_params)
142
                pushnotification.type = "sent"
151
                updateResponse = urllib2.urlopen(updateReq)
143
                pushnotification.status = False
152
                response_str = updateResponse.read()
144
                pushnotification.message = result["results"][0]["error"]
153
                logging.debug('User Id:- '+str(notificationRecord.userId)+' Update Response :-'+str(response_str))
145
                
154
                
146
                updateGcmUserSql = "update gcm_users set failurecount=failurecount+1 where gcm_regid='%s'"%(notificationRecord.gcmRegId)
155
                updateGcmUserSql = "update gcm_users set failurecount=failurecount+1 where gcm_regid='%s'"%(notificationRecord.gcmRegId)
147
                logging.debug('Update GCM User Query :-'+str(updateGcmUserSql))
156
                logging.debug('Update GCM User Query :-'+str(updateGcmUserSql))
148
                try:
157
                try:
149
                    dtrdb = MySQLdb.connect('localhost',"root","shop2020","dtr" )
158
                    dtrdb = MySQLdb.connect('localhost',"root","shop2020","dtr" )
Line 161... Line 170...
161
    """Yield successive n-sized chunks from l."""
170
    """Yield successive n-sized chunks from l."""
162
    for i in xrange(0, len(l), n):
171
    for i in xrange(0, len(l), n):
163
        yield l[i:i+n]
172
        yield l[i:i+n]
164
 
173
 
165
def main():
174
def main():
-
 
175
    global userGcmRegIdMap
-
 
176
    global campaignsMap
166
    parser = optparse.OptionParser()
177
    parser = optparse.OptionParser()
167
    parser.add_option("-C", "--chunksize", dest="chunksize",
178
    parser.add_option("-C", "--chunksize", dest="chunksize",
168
                      default="100",
179
                      default="100",
169
                      type="int", help="The requsets a single thread handles",
180
                      type="int", help="The requsets a single thread handles",
170
                      metavar="CHUNKSIZE")
181
                      metavar="CHUNKSIZE")
171
    (options, args) = parser.parse_args()
182
    (options, args) = parser.parse_args()
172
    
183
    
-
 
184
 
-
 
185
    notificationListReq = urllib2.Request(PENDING_PUSH_NOTIFICATION_URL)
-
 
186
    notificationListResponse = urllib2.urlopen(notificationListReq)
-
 
187
    notificationListJson = json.loads(notificationListResponse.read())
-
 
188
    
-
 
189
    usersList =[]
-
 
190
    campaignIdList = []
-
 
191
    
-
 
192
    for notificationRec in notificationListJson:
-
 
193
        if notificationRec.get('user_id') not in usersList:
-
 
194
            usersList.append(notificationRec.get('user_id'))
-
 
195
        if notificationRec.get('notification_campaign_id') not in campaignIdList:
-
 
196
            campaignIdList.append(notificationRec.get('notification_campaign_id'))
-
 
197
    
-
 
198
    usersList.append(0)
-
 
199
    campaignIdList.append(0)
-
 
200
        
173
    cursor.execute(PUSH_NOTIFICATIONS_SQL)
201
    cursor.execute(GCM_REG_ID_SQL%(str(tuple(usersList))))
174
    result_data = cursor.fetchall()
202
    result_data = cursor.fetchall()
175
    
203
    
-
 
204
    for dataRec in result_data:
-
 
205
        userGcmRegIdMap[dataRec[0]] = dataRec[1]
-
 
206
        
-
 
207
    cursor.execute(CAMPAIGNS_SQL%(str(tuple(campaignIdList))))
-
 
208
    campaign_data = cursor.fetchall()
-
 
209
    
-
 
210
    for campaignRec in campaign_data:
-
 
211
        campaignsMap[campaignRec[0]] = campaignRec    
-
 
212
    
176
    count = 1
213
    count = 1
177
    DataService.initialize(db_hostname="localhost") 
-
 
178
    if result_data:
214
    if result_data:
179
        campaign_receivers_list = list(chunks(result_data, options.chunksize))
215
        campaign_receivers_list = list(chunks(notificationListJson, options.chunksize))
180
        print len(campaign_receivers_list)
216
        print len(campaign_receivers_list)
181
        for sublist in campaign_receivers_list:
217
        for sublist in campaign_receivers_list:
182
            thread = NotificationThread(count, "Thread-"+str(count), sublist)
218
            thread = NotificationThread(count, "Thread-"+str(count), sublist)
183
            thread.start()
219
            thread.start()
184
            count = count +1
220
            count = count +1