| 19107 |
manish.sha |
1 |
import MySQLdb
|
|
|
2 |
from elixir import *
|
|
|
3 |
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
|
|
|
4 |
from sqlalchemy.sql import func
|
|
|
5 |
from sqlalchemy.sql.expression import and_, or_, desc, not_, distinct, cast, \
|
|
|
6 |
between
|
|
|
7 |
import optparse
|
|
|
8 |
import urllib2
|
|
|
9 |
import base64
|
|
|
10 |
import urllib
|
|
|
11 |
import logging
|
|
|
12 |
from dtr.utils.utils import get_mongo_connection, to_java_date, num_encode
|
|
|
13 |
from datetime import datetime, timedelta
|
|
|
14 |
from dtr.storage import DataService
|
|
|
15 |
from dtr.storage.DataService import Users
|
|
|
16 |
import traceback
|
|
|
17 |
import time
|
|
|
18 |
|
|
|
19 |
DataService.initialize(db_hostname="192.168.158.89")
|
|
|
20 |
logging.basicConfig(level=logging.DEBUG)
|
|
|
21 |
mongoHost = 'localhost'
|
|
|
22 |
|
|
|
23 |
def main():
|
|
|
24 |
global mongoHost
|
|
|
25 |
parser = optparse.OptionParser()
|
|
|
26 |
parser.add_option("-M", "--mongo_host", dest="mongo_host",
|
|
|
27 |
default="localhost",
|
|
|
28 |
type="str", help="The requsets a single thread handles",
|
|
|
29 |
metavar="MONGOHOST")
|
|
|
30 |
(options, args) = parser.parse_args()
|
|
|
31 |
mongoHost = options.mongo_host
|
|
|
32 |
timeCondition = datetime.now()-timedelta(hours=2)
|
|
|
33 |
eligibleSmsNotficationCampaigns = list(get_mongo_connection(host=mongoHost).User.notificationcampaigns.find({'smsprocessed':1,'created':{'$lte':to_java_date(timeCondition)}}))
|
|
|
34 |
for notificationCampaign in eligibleSmsNotficationCampaigns:
|
|
|
35 |
logging.debug('Notification_Campign_Id:- '+str(notificationCampaign.get('_id')))
|
|
|
36 |
|
| 19259 |
manish.sha |
37 |
jsonSentList = list(get_mongo_connection(host=mongoHost).User.pushnotificationsnew.find({'notification_campaign_id':notificationCampaign.get('_id'),'smstype':'SMS_SENT_OPERATOR'}))
|
| 19107 |
manish.sha |
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 |
status = ""
|
|
|
47 |
del_req = urllib2.Request(del_det_url)
|
|
|
48 |
del_response = urllib2.urlopen(del_req)
|
|
|
49 |
del_response_str = del_response.read()
|
|
|
50 |
logging.debug('Delivery Report Response:- '+str(del_response_str))
|
|
|
51 |
if 'STATUS' in del_response_str:
|
|
|
52 |
status = del_response_str.split(' ')[3]
|
|
|
53 |
if status=='DELIVRD':
|
|
|
54 |
status = 'SMS_DELIVRD'
|
|
|
55 |
elif status=='SENT':
|
|
|
56 |
status = 'SMS_SENT_OPERATOR'
|
|
|
57 |
elif status=='REJECTD' or status== 'FAILED':
|
|
|
58 |
status = 'SMS_FAILED'
|
|
|
59 |
elif 'EXP_' in status:
|
|
|
60 |
status = 'SMS_EXPIRED'
|
|
|
61 |
else:
|
|
|
62 |
status = 'SMS_'+status
|
|
|
63 |
|
| 19259 |
manish.sha |
64 |
get_mongo_connection(host=mongoHost).User.pushnotificationsnew.update({'_id':smsResponse.get('_id')},{"$set":{'sms_type':status,'sms_timestamp':to_java_date(datetime.now())}})
|
| 19107 |
manish.sha |
65 |
|
|
|
66 |
except Exception as e:
|
|
|
67 |
logging.debug('Error while getting response for message id:- '+ str(smsResponse.get("sms_id")))
|
|
|
68 |
traceback.print_exc()
|
|
|
69 |
continue
|
|
|
70 |
|
| 19259 |
manish.sha |
71 |
jsonSentList = list(get_mongo_connection(host=mongoHost).User.pushnotificationsnew.find({'notification_campaign_id':notificationCampaign.get('_id'),'smstype':'SMS_INPROCESS'}))
|
| 19107 |
manish.sha |
72 |
|
|
|
73 |
for smsResponse in jsonSentList:
|
|
|
74 |
del_det_params = {'uname' : 'srlsaholic', 'passwd' : 'sr18mar' , 'messageid' : str(smsResponse.get('sms_id'))}
|
|
|
75 |
encoded_del_det_params = urllib.urlencode(del_det_params)
|
|
|
76 |
del_det_url = 'http://103.15.179.45:8085/SMSGateway/getApiReport?' + encoded_del_det_params
|
|
|
77 |
logging.debug('User Id:- '+str(smsResponse.get('user_id'))+ ' Delivery Report Url:- '+del_det_url)
|
|
|
78 |
|
|
|
79 |
try:
|
|
|
80 |
del_req = urllib2.Request(del_det_url)
|
|
|
81 |
del_response = urllib2.urlopen(del_req)
|
|
|
82 |
del_response_str = del_response.read()
|
|
|
83 |
logging.debug('Delivery Report Response:- '+str(del_response_str))
|
|
|
84 |
|
|
|
85 |
del_response_vals = del_response_str.split(' ')
|
|
|
86 |
|
|
|
87 |
status = ''
|
|
|
88 |
if len(del_response_vals) == 8 and del_response_vals[7][:-2] == '0':
|
|
|
89 |
status = 'SMS_DELIVRD'
|
|
|
90 |
elif "Submitted to SMSC" in del_response_str:
|
|
|
91 |
status = 'SMS_INPROCESS'
|
|
|
92 |
else:
|
|
|
93 |
status = 'SMS_FAILED'
|
|
|
94 |
|
| 19259 |
manish.sha |
95 |
get_mongo_connection(host=mongoHost).User.pushnotificationsnew.update({'_id':smsResponse.get('_id')},{"$set":{'sms_type':status,'sms_timestamp':to_java_date(datetime.now())}})
|
| 19107 |
manish.sha |
96 |
|
|
|
97 |
except Exception as e:
|
|
|
98 |
logging.debug('Error while getting response for message id:- '+ str(smsResponse.get("sms_id")))
|
|
|
99 |
traceback.print_exc()
|
|
|
100 |
continue
|
|
|
101 |
|
|
|
102 |
if __name__=='__main__':
|
|
|
103 |
main()
|
|
|
104 |
try:
|
|
|
105 |
session.close()
|
|
|
106 |
except:
|
|
|
107 |
print 'Error while closing session'
|