Subversion Repositories SmartDukaan

Rev

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

import json
from dtr.storage import DataService
from dtr.storage.DataService import notification_campaigns, Users
import MySQLdb
import pymongo
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 datetime import datetime
from datetime import timedelta
import urllib
import urllib2
import json
import time
import traceback
import logging

PUSH_NOTIFICATIONS_DETAILS_URL="http://localhost:3001/getPushNotificationDetailsByType/?type=%s&notification_campaign_id=%d&status=%s"
PROMOTIONAL_SMS_SEND_URL = "http://103.15.179.45:8085/MessagingGateway/SendTransSMS?"
PUSH_NOTIFICATIONS_UPDATE_URL = "http://localhost:3001/updatePushNotification/?"
#ID is: PRO02135496 Mobile Number is: 918512809859 PRO02135496#SMS Sent successfully to : 918512809859
#http://103.15.179.45:8085/MessagingGateway/http/querymsg?user=srlsaholicP&password=srp8oct&apimsgid=PRO02257518
#MessageID: PRO02135496 STATUS: DELIVRD ErrorCode: 0 DR Time: 2015-10-20 18:05:11 

DataService.initialize(db_hostname="192.168.158.89")
logging.basicConfig(level=logging.DEBUG)

def main():
    timeCondition = datetime.now()-timedelta(hours=2)
    eligibleSmsNotficationCampaigns = notification_campaigns.query.filter(notification_campaigns.smsprocessed==True).filter(notification_campaigns.created<timeCondition).all()
    for notificationCampaign in eligibleSmsNotficationCampaigns:
        logging.debug('Notification_Campign_Id:- '+str(notificationCampaign.id))
        
        allSentResponse = urllib2.urlopen(PUSH_NOTIFICATIONS_DETAILS_URL%('SMS_SENT_OPERATOR',notificationCampaign.id,1)).read()
        jsonSentList = json.loads(allSentResponse)
        
        for smsResponse in jsonSentList:
            del_det_params = {'user' : 'srlsaholicP', 'password' : 'srp8oct' , 'apimsgid' : smsResponse.get("sms_id")}
            encoded_del_det_params = urllib.urlencode(del_det_params)
            del_det_url = 'http://103.15.179.45:8085/MessagingGateway/http/querymsg?' + encoded_del_det_params
            logging.debug('User Id:- '+str(smsResponse.get('user_id'))+ ' Delivery Report Url:- '+del_det_url)
            
            try:
                del_req = urllib2.Request(del_det_url)
                del_response = urllib2.urlopen(del_req)
                del_response_str = del_response.read()
                logging.debug('Delivery Report Response:- '+str(del_response_str))
                if 'STATUS' in del_response_str:
                    status = del_response_str.split(' ')[3]
                    if status=='DELIVRD':
                        status = 'SMS_DELIVRD'
                    elif status=='SENT':
                        status = 'SMS_SENT_OPERATOR'
                    elif status=='REJECTD' or status== 'FAILED':
                        status = 'SMS_FAILED'
                    elif 'EXP_' in status:
                        status = 'SMS_EXPIRED'
                    else:
                        status = 'SMS_'+status
                    
                    update_params = { 'user_id' : smsResponse.get('user_id'),  'notification_campaign_id' : smsResponse.get('notification_campaign_id'), 'type' : status, 'status':1, 'message':'success' }
                    encoded_update_params = urllib.urlencode(update_params)
                    updateReq = urllib2.Request(PUSH_NOTIFICATIONS_UPDATE_URL+encoded_update_params)
                    updateResponse = urllib2.urlopen(updateReq)
                    response_str = updateResponse.read()
                       
                    logging.debug('Push Notification Update Response:- '+str(response_str))
                
            except Exception as e:
                logging.debug('Error while getting response for message id:- '+ str(smsResponse.get("sms_id")))
                traceback.print_exc()
                continue
            
        allSentResponse = urllib2.urlopen(PUSH_NOTIFICATIONS_DETAILS_URL%('SMS_INPROCESS',notificationCampaign.id,1)).read()
        jsonSentList = json.loads(allSentResponse)
        
        for smsResponse in jsonSentList:
            del_det_params = {'uname' : 'srlsaholic', 'passwd' : 'sr18mar' , 'messageid' : str(smsResponse.get("sms_id"))}
            encoded_del_det_params = urllib.urlencode(del_det_params)
            del_det_url = 'http://103.15.179.45:8085/SMSGateway/getApiReport?' + encoded_del_det_params
            logging.debug('User Id:- '+str(smsResponse.get('user_id'))+ ' Delivery Report Url:- '+del_det_url)
            
            try:
                del_req = urllib2.Request(del_det_url)
                del_response = urllib2.urlopen(del_req)
                del_response_str = del_response.read()
                logging.debug('Delivery Report Response:- '+str(del_response_str))
                
                del_response_vals = del_response_str.split(' ')
                
                status = ''
                if len(del_response_vals) == 8 and del_response_vals[7][:-2] == '0':
                    status = 'SMS_DELIVRD'
                elif "Submitted to SMSC" in del_response_str:
                    status = 'SMS_INPROCESS'
                else:
                    status = 'SMS_FAILED'
                    
                update_params = { 'user_id' : smsResponse.get('user_id'),  'notification_campaign_id' : smsResponse.get('notification_campaign_id'), 'type' : status, 'status':1, 'message':'success' }
                encoded_update_params = urllib.urlencode(update_params)
                updateReq = urllib2.Request(PUSH_NOTIFICATIONS_UPDATE_URL+encoded_update_params)
                updateResponse = urllib2.urlopen(updateReq)
                response_str = updateResponse.read()
                   
                logging.debug('Push Notification Update Response:- '+str(response_str))
                
            except Exception as e:
                logging.debug('Error while getting response for message id:- '+ str(smsResponse.get("sms_id")))
                traceback.print_exc()
                continue
            
            
    
if __name__=='__main__':
    main()
    try:
        session.close()
    except:
        print 'Error while closing session'