Subversion Repositories SmartDukaan

Rev

Rev 15727 | Rev 16295 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/usr/bin/python

import threading
import time

import MySQLdb
from dtr.storage import DataService
from dtr.storage.DataService import Pushnotifications
from elixir import *
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
from sqlalchemy.sql import func
from sqlalchemy.sql.expression import and_, or_, desc, not_, distinct, cast, \
    between
from urlparse import urlparse
import requests
import json
import optparse
import urllib2
import base64
import urllib
import logging

GCM_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]] = rec
    
logging.basicConfig(level=logging.DEBUG,
                    format='[%(levelname)s] (%(threadName)-10s) %(message)s',
                    )


class NotificationRecord():
    pushNotificationId = None
    userId = None
    campaignId = None
    campaignName = None
    title = None
    message= None
    type = None
    url = None
    expiresAt = None
    notificationStatus = None
    notificationCreated = None
    gcmRegId = None
    
    def __init__(self,pushNotificationId, userId, campaignId, campaignName, title, message, type, url, expiresAt, notificationStatus, notificationCreated, gcmRegId):
        self.pushNotificationId = pushNotificationId
        self.userId = userId
        self.campaignId = campaignId
        self.campaignName = campaignName
        self.title = title
        self.message= message
        self.type = type
        self.url = url
        self.expiresAt = expiresAt
        self.notificationStatus = notificationStatus
        self.notificationCreated = notificationCreated
        self.gcmRegId = gcmRegId
        
        
class NotificationThread (threading.Thread):
    def __init__(self, threadID, name, recordsList):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.recordsList = recordsList
    def 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']
            
            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'] = data
            regIds = []
            regIds.append(notificationRecord.gcmRegId)
            post_data['registration_ids'] = regIds
             
            post_data_json = json.dumps(post_data)
            logging.debug('Post Data Json :-'+str(post_data_json))
            
            response = requests.post(GCM_URL, data=post_data_json, headers=headers)
            logging.debug('Response :-'+str(response.text))
            result = json.loads(response.text)
            pushnotification = Pushnotifications.get_by(id=notificationRecord.pushNotificationId)
            
            if result["success"]:
                pushnotification.type = "sent"
                pushnotification.status = True
                pushnotification.message = "success"
                session.commit()
            else:
                pushnotification.type = "sent"
                pushnotification.status = False
                pushnotification.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 = 1
    DataService.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 +1
            
    db.close()
    if session.is_active:
        print "session is active. closing it."
        session.close()    

if __name__=='__main__':
    main()