<s> """<STR_LIT>""" <EOL> from django . conf import settings <EOL> import urllib2 <EOL> import urllib <EOL> import re <EOL> import logging <EOL> import math <EOL> from . base import BaseSmsBackend <EOL> from decimal import Decimal <EOL> logger = logging . getLogger ( __name__ ) <EOL> SMSGLOBAL_USERNAME = getattr ( settings , '<STR_LIT>' , '<STR_LIT>' ) <EOL> SMSGLOBAL_PASSWORD = getattr ( settings , '<STR_LIT>' , '<STR_LIT>' ) <EOL> SMSGLOBAL_CHECK_BALANCE_COUNTRY = getattr ( settings , '<STR_LIT>' , False ) <EOL> SMSGLOBAL_API_URL_SENDSMS = '<STR_LIT>' <EOL> SMSGLOBAL_API_URL_CHECKBALANCE = '<STR_LIT>' <EOL> class SmsBackend ( BaseSmsBackend ) : <EOL> """<STR_LIT>""" <EOL> def get_username ( self ) : <EOL> return SMSGLOBAL_USERNAME <EOL> def get_password ( self ) : <EOL> return SMSGLOBAL_PASSWORD <EOL> def get_balance ( self ) : <EOL> """<STR_LIT>""" <EOL> if not SMSGLOBAL_CHECK_BALANCE_COUNTRY : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> params = { <EOL> '<STR_LIT:user>' : self . get_username ( ) , <EOL> '<STR_LIT:password>' : self . get_password ( ) , <EOL> '<STR_LIT>' : SMSGLOBAL_CHECK_BALANCE_COUNTRY , <EOL> } <EOL> req = urllib2 . Request ( SMSGLOBAL_API_URL_CHECKBALANCE , urllib . urlencode ( params ) ) <EOL> response = urllib2 . urlopen ( req ) . read ( ) <EOL> if response . startswith ( '<STR_LIT>' ) : <EOL> raise Exception ( '<STR_LIT>' % response . replace ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> return dict ( [ ( p . split ( '<STR_LIT::>' ) [ <NUM_LIT:0> ] . lower ( ) , p . split ( '<STR_LIT::>' ) [ <NUM_LIT:1> ] ) for p in response . split ( '<STR_LIT:;>' ) if len ( p ) > <NUM_LIT:0> ] ) <EOL> def send_messages ( self , sms_messages ) : <EOL> """<STR_LIT>""" <EOL> if not sms_messages : <EOL> return <EOL> num_sent = <NUM_LIT:0> <EOL> for message in sms_messages : <EOL> if self . _send ( message ) : <EOL> num_sent += <NUM_LIT:1> <EOL> return num_sent <EOL> def _send ( self , message ) : <EOL> """<STR_LIT>""" <EOL> charset = '<STR_LIT>' <EOL> params = { <EOL> '<STR_LIT:action>' : '<STR_LIT>' , <EOL> '<STR_LIT:user>' : self . get_username ( ) , <EOL> '<STR_LIT:password>' : self . get_password ( ) , <EOL> '<STR_LIT>' : message . from_phone , <EOL> '<STR_LIT:to>' : "<STR_LIT:U+002C>" . join ( message . to ) , <EOL> '<STR_LIT:text>' : message . body , <EOL> '<STR_LIT>' : charset , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : int ( math . ceil ( len ( message . body ) / <NUM_LIT> ) ) <EOL> } <EOL> req = urllib2 . Request ( SMSGLOBAL_API_URL_SENDSMS , urllib . urlencode ( params ) ) <EOL> result_page = urllib2 . urlopen ( req ) . read ( ) <EOL> results = self . _parse_response ( result_page ) <EOL> if results is None : <EOL> if not self . fail_silently : <EOL> raise Exception ( "<STR_LIT>" + result_page + "<STR_LIT:]>" ) <EOL> return False <EOL> code , sendqmsgid , msgid = results <EOL> if code != '<STR_LIT:0>' : <EOL> if not self . fail_silently : <EOL> raise Exception ( "<STR_LIT>" % ( result_page , results ) ) <EOL> return False <EOL> else : <EOL> logger . info ( '<STR_LIT>' % ( <EOL> message . to , <EOL> message . from_phone , <EOL> code , <EOL> sendqmsgid , <EOL> msgid , <EOL> message . body <EOL> ) ) <EOL> return True <EOL> def _parse_response ( self , result_page ) : <EOL> """<STR_LIT>""" <EOL> resultline = result_page . splitlines ( ) [ <NUM_LIT:0> ] <EOL> if resultline . startswith ( '<STR_LIT>' ) : <EOL> raise Exception ( resultline . replace ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> patt = re . compile ( r'<STR_LIT>' , re . IGNORECASE ) <EOL> m = patt . match ( resultline ) <EOL> if m : <EOL> return ( m . group ( <NUM_LIT:1> ) , m . group ( <NUM_LIT:2> ) , m . group ( <NUM_LIT:3> ) ) <EOL> return None </s>
