Rev 16294 | Rev 16353 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
#!/usr/bin/pythonimport threadingimport timeimport MySQLdbfrom dtr.storage import DataServicefrom dtr.storage.DataService import Pushnotificationsfrom elixir import *from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFoundfrom sqlalchemy.sql import funcfrom sqlalchemy.sql.expression import and_, or_, desc, not_, distinct, cast, \betweenfrom urlparse import urlparseimport requestsimport jsonimport optparseimport urllib2import base64import urllibimport loggingGCM_URL = "https://android.googleapis.com/gcm/send"GOOGLE_API_KEY = "AIzaSyDw1qBnmxtnfR9NqBewryQ-yo3cG2ravGM"headers = {'content-type':'application/json', "authorization":"key=" + GOOGLE_API_KEY}aff_url_headers = {'User-agent':'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36','Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8','Accept-Language' : 'en-US,en;q=0.8','Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3','Connection':'keep-alive'}db = MySQLdb.connect('localhost',"root","shop2020","dtr" )cursor = db.cursor()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'"ALL_STORES_SQL = "select * from stores"cursor.execute(ALL_STORES_SQL)result_stores = cursor.fetchall()domainStoresMap = {}for rec in result_stores:domainStoresMap[rec[2]] = reclogging.basicConfig(level=logging.DEBUG,format='[%(levelname)s] (%(threadName)-10s) %(message)s',)class NotificationRecord():pushNotificationId = NoneuserId = NonecampaignId = NonecampaignName = Nonetitle = Nonemessage= Nonetype = Noneurl = NoneexpiresAt = NonenotificationStatus = NonenotificationCreated = NonegcmRegId = Nonedef __init__(self,pushNotificationId, userId, campaignId, campaignName, title, message, type, url, expiresAt, notificationStatus, notificationCreated, gcmRegId):self.pushNotificationId = pushNotificationIdself.userId = userIdself.campaignId = campaignIdself.campaignName = campaignNameself.title = titleself.message= messageself.type = typeself.url = urlself.expiresAt = expiresAtself.notificationStatus = notificationStatusself.notificationCreated = notificationCreatedself.gcmRegId = gcmRegIdclass NotificationThread (threading.Thread):def __init__(self, threadID, name, recordsList):threading.Thread.__init__(self)self.threadID = threadIDself.name = nameself.recordsList = recordsListdef run(self):logging.debug('Starting')handleCampaignRequest(self.name, self.recordsList)logging.debug('Completed')def handleCampaignRequest(threadName, recordsList ):for record in recordsList: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])if notificationRecord.type=='url':parsed_uri = urlparse(notificationRecord.url)domain = '{uri.netloc}'.format(uri=parsed_uri)logging.debug('Affiliate Domain:-'+str(domain))logging.debug('User Id:-'+str(notificationRecord.userId)+' And GCM Reg Id:- '+ str(notificationRecord.gcmRegId))store = domainStoresMap.get(domain)if store is not None:url_params = { 'url' : notificationRecord.url, 'userId' : notificationRecord.userId, 'storeId' : store[0] }encoded_url_params = urllib.urlencode(url_params)DTR_API_BASIC_AUTH = base64.encodestring('%s:%s' % ("dtr", "dtr18Feb2015")).replace('\n', '')pushpostrequest = urllib2.Request('http://api.profittill.com/pushnotifications/generateAffiliateUrl', encoded_url_params, headers=aff_url_headers)pushpostrequest.add_header("Authorization", "Basic %s" % DTR_API_BASIC_AUTH)json_result = json.loads(urllib2.urlopen(pushpostrequest).read())notificationRecord.url = json_result['url']logging.debug('User Id:-'+str(notificationRecord.userId)+' Notification Url:- '+ str(notificationRecord.url))else:notificationRecord.url = notificationRecord.url+'?user_id='+str(notificationRecord.userId)data = {"message":notificationRecord.message,"cid":notificationRecord.campaignId,"title":notificationRecord.title,"type":notificationRecord.type,"url":notificationRecord.url,"vibrate":1,"sound":1,"largeIcon":"large_icon","smallIcon":"small_icon"}post_data = {}post_data['data'] = dataregIds = []regIds.append(notificationRecord.gcmRegId)post_data['registration_ids'] = regIdspost_data_json = json.dumps(post_data)logging.debug('User Id:- '+str(notificationRecord.userId)+' Post Data Json :- '+str(post_data_json))response = requests.post(GCM_URL, data=post_data_json, headers=headers)logging.debug('User Id:- '+str(notificationRecord.userId)+' Response :-'+str(response.text))result = json.loads(response.text)pushnotification = Pushnotifications.get_by(id=notificationRecord.pushNotificationId)if result["success"]:pushnotification.type = "sent"pushnotification.status = Truepushnotification.message = "success"session.commit()else:pushnotification.type = "sent"pushnotification.status = Falsepushnotification.message = result["results"][0]["error"]updateGcmUserSql = "update gcm_users set failurecount=failurecount+1 where gcm_regid='%s'"%(notificationRecord.gcmRegId)logging.debug('Update GCM User Query :-'+str(updateGcmUserSql))try:dtrdb = MySQLdb.connect('localhost',"root","shop2020","dtr" )cursor = dtrdb.cursor()cursor.execute(updateGcmUserSql)dtrdb.commit()session.commit()dtrdb.close()except:dtrdb.rollback()dtrdb.close()#time.sleep(2)def chunks(l, n):"""Yield successive n-sized chunks from l."""for i in xrange(0, len(l), n):yield l[i:i+n]def main():parser = optparse.OptionParser()parser.add_option("-C", "--chunksize", dest="chunksize",default="100",type="int", help="The requsets a single thread handles",metavar="CHUNKSIZE")(options, args) = parser.parse_args()cursor.execute(PUSH_NOTIFICATIONS_SQL)result_data = cursor.fetchall()count = 1DataService.initialize(db_hostname="localhost")if result_data:campaign_receivers_list = list(chunks(result_data, options.chunksize))print len(campaign_receivers_list)for sublist in campaign_receivers_list:thread = NotificationThread(count, "Thread-"+str(count), sublist)thread.start()count = count +1db.close()if session.is_active:print "session is active. closing it."session.close()if __name__=='__main__':main()