Subversion Repositories SmartDukaan

Rev

Rev 18218 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17399 manish.sha 1
import json
2
from dtr.storage import DataService
3
from dtr.storage.DataService import notification_campaigns, Users
4
import MySQLdb
5
import pymongo
6
from elixir import *
7
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
8
from sqlalchemy.sql import func
9
from sqlalchemy.sql.expression import and_, or_, desc, not_, distinct, cast, \
10
    between
11
from datetime import datetime
12
from datetime import timedelta
13
import urllib
14
import urllib2
15
import json
16
import time
17
import traceback
18
import logging
19
 
17401 manish.sha 20
PUSH_NOTIFICATIONS_DETAILS_URL="http://localhost:3001/getPushNotificationDetailsByType/?type=%s&notification_campaign_id=%d&status=%s"
17399 manish.sha 21
PROMOTIONAL_SMS_SEND_URL = "http://103.15.179.45:8085/MessagingGateway/SendTransSMS?"
17401 manish.sha 22
PUSH_NOTIFICATIONS_UPDATE_URL = "http://localhost:3001/updatePushNotification/?"
17399 manish.sha 23
#ID is: PRO02135496 Mobile Number is: 918512809859 PRO02135496#SMS Sent successfully to : 918512809859
24
#http://103.15.179.45:8085/MessagingGateway/http/querymsg?user=srlsaholicP&password=srp8oct&apimsgid=PRO02257518
25
#MessageID: PRO02135496 STATUS: DELIVRD ErrorCode: 0 DR Time: 2015-10-20 18:05:11 
26
 
27
DataService.initialize(db_hostname="192.168.158.89")
28
logging.basicConfig(level=logging.DEBUG)
29
 
30
def main():
31
    timeCondition = datetime.now()-timedelta(hours=2)
32
    eligibleSmsNotficationCampaigns = notification_campaigns.query.filter(notification_campaigns.smsprocessed==True).filter(notification_campaigns.created<timeCondition).all()
33
    for notificationCampaign in eligibleSmsNotficationCampaigns:
34
        logging.debug('Notification_Campign_Id:- '+str(notificationCampaign.id))
35
 
36
        allSentResponse = urllib2.urlopen(PUSH_NOTIFICATIONS_DETAILS_URL%('SMS_SENT_OPERATOR',notificationCampaign.id,1)).read()
37
        jsonSentList = json.loads(allSentResponse)
38
 
39
        for smsResponse in jsonSentList:
40
            del_det_params = {'user' : 'srlsaholicP', 'password' : 'srp8oct' , 'apimsgid' : smsResponse.get("sms_id")}
41
            encoded_del_det_params = urllib.urlencode(del_det_params)
42
            del_det_url = 'http://103.15.179.45:8085/MessagingGateway/http/querymsg?' + encoded_del_det_params
43
            logging.debug('User Id:- '+str(smsResponse.get('user_id'))+ ' Delivery Report Url:- '+del_det_url)
44
 
45
            try:
46
                del_req = urllib2.Request(del_det_url)
47
                del_response = urllib2.urlopen(del_req)
48
                del_response_str = del_response.read()
49
                logging.debug('Delivery Report Response:- '+str(del_response_str))
50
                if 'STATUS' in del_response_str:
51
                    status = del_response_str.split(' ')[3]
52
                    if status=='DELIVRD':
53
                        status = 'SMS_DELIVRD'
54
                    elif status=='SENT':
55
                        status = 'SMS_SENT_OPERATOR'
56
                    elif status=='REJECTD' or status== 'FAILED':
57
                        status = 'SMS_FAILED'
58
                    elif 'EXP_' in status:
59
                        status = 'SMS_EXPIRED'
60
                    else:
61
                        status = 'SMS_'+status
62
 
18248 manas 63
                    update_params = { 'user_id' : smsResponse.get('user_id'),  'notification_campaign_id' : smsResponse.get('notification_campaign_id'), 'oldType':'SMS_SENT_OPERATOR', 'type' : status, 'status':1, 'message':'success' }
17399 manish.sha 64
                    encoded_update_params = urllib.urlencode(update_params)
65
                    updateReq = urllib2.Request(PUSH_NOTIFICATIONS_UPDATE_URL+encoded_update_params)
66
                    updateResponse = urllib2.urlopen(updateReq)
67
                    response_str = updateResponse.read()
68
 
69
                    logging.debug('Push Notification Update Response:- '+str(response_str))
70
 
71
            except Exception as e:
72
                logging.debug('Error while getting response for message id:- '+ str(smsResponse.get("sms_id")))
73
                traceback.print_exc()
74
                continue
75
 
18218 manish.sha 76
        allSentResponse = urllib2.urlopen(PUSH_NOTIFICATIONS_DETAILS_URL%('SMS_INPROCESS',notificationCampaign.id,1)).read()
77
        jsonSentList = json.loads(allSentResponse)
78
 
79
        for smsResponse in jsonSentList:
80
            del_det_params = {'uname' : 'srlsaholic', 'passwd' : 'sr18mar' , 'messageid' : str(smsResponse.get("sms_id"))}
81
            encoded_del_det_params = urllib.urlencode(del_det_params)
82
            del_det_url = 'http://103.15.179.45:8085/SMSGateway/getApiReport?' + encoded_del_det_params
83
            logging.debug('User Id:- '+str(smsResponse.get('user_id'))+ ' Delivery Report Url:- '+del_det_url)
84
 
85
            try:
86
                del_req = urllib2.Request(del_det_url)
87
                del_response = urllib2.urlopen(del_req)
88
                del_response_str = del_response.read()
89
                logging.debug('Delivery Report Response:- '+str(del_response_str))
90
 
91
                del_response_vals = del_response_str.split(' ')
92
 
93
                status = ''
94
                if len(del_response_vals) == 8 and del_response_vals[7][:-2] == '0':
95
                    status = 'SMS_DELIVRD'
96
                elif "Submitted to SMSC" in del_response_str:
97
                    status = 'SMS_INPROCESS'
98
                else:
99
                    status = 'SMS_FAILED'
100
 
18248 manas 101
                update_params = { 'user_id' : smsResponse.get('user_id'),  'notification_campaign_id' : smsResponse.get('notification_campaign_id'), 'oldType':'SMS_INPROCESS', 'type' : status, 'status':1, 'message':'success' }
18218 manish.sha 102
                encoded_update_params = urllib.urlencode(update_params)
103
                updateReq = urllib2.Request(PUSH_NOTIFICATIONS_UPDATE_URL+encoded_update_params)
104
                updateResponse = urllib2.urlopen(updateReq)
105
                response_str = updateResponse.read()
106
 
107
                logging.debug('Push Notification Update Response:- '+str(response_str))
108
 
109
            except Exception as e:
110
                logging.debug('Error while getting response for message id:- '+ str(smsResponse.get("sms_id")))
111
                traceback.print_exc()
112
                continue
113
 
114
 
17399 manish.sha 115
 
116
if __name__=='__main__':
117
    main()
118
    try:
119
        session.close()
120
    except:
121
        print 'Error while closing session'