from os import popen, system
from urllib import urlencode
from urllib2 import urlopen, Request
import mechanize
import cookielib
from BeautifulSoup import BeautifulSoup
import sys, getopt
def sendsms():
#You will need to fill in field_from, field_contactno and field_msg"
global field_from
global field_contactno
global field_msg
field_counter = 150-len(field_msg)
# Extract the JSessionID from the cookie by loading the below page
URL_BASE='http://msgctr.m1.com.sg/guest/'
set_cookie = urlopen(URL_BASE).headers.getheader("Set-Cookie")
sess_id = set_cookie[set_cookie.index("=")+1:set_cookie.index(";")]
# Construct headers dictionary using the JSESSIONID
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0) Gecko/20100101 Firefox/10.0',
'Host':'msgctr.m1.com.sg',
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language':'en-us,en;q=0.5',
'Accept-Encoding':'gzip,deflate',
'Accept-Charset':'ISO-8859-1,utf-8;q=0.7,*;q=0.7','Cookie':'JSESSIONID='+sess_id}
# Extract the location for the captcha image
text = (urlopen(Request(URL_BASE+"index.jsp",headers=headers)).read())
for line in text.split("\n"):
if "/guest/captcha.jpg" in line:
line = (line.replace('<img src="','')).strip()
position = line.find('"')
captchaText = line[1:position].strip("guest/")
sessionID = captchaText.strip("captcha.jpg?session=")
# Save the captcha image to local disk
localFile = open('img.jpg', 'wb')
localFile.write(urlopen(Request(URL_BASE+captchaText,headers=headers)).read())
localFile.close()
# Use the function at myfonts.com to decode the captcha
br = mechanize.Browser()
# Set the mechanize handle options
br.set_handle_redirect(True)
br.set_handle_robots(False)
# Load the website
r = br.open('http://new.myfonts.com/WhatTheFont')
html = r.read()
# Upload the captcha image
br.select_form(nr=1)
filename='img.jpg'
br.form.add_file(open(filename,"rb"),'image/jpeg',filename,name='userfile')
br.submit()
soup = BeautifulSoup(br.response().read())
#Extract the captcha results
captcha1 = soup.find('input', id='wtfchar0')
captcha2 = soup.find('input', id='wtfchar1')
captcha3 = soup.find('input', id='wtfchar2')
if(captcha1!="" and captcha2!="" and captcha3!=""):
result = (str(captcha1['value'])+str(captcha2['value'])+str(captcha3['value'])).upper()
# Encode POST parameters for the capcha page
data = urlencode( {"from":field_from,"locale":"en",
"msisdn":field_contactno,"msg":field_msg,
"counter":field_counter,"sessionid":sessionID,"code":result} )
# Construct headers dictionary using the JSESSIONID
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0) Gecko/20100101 Firefox/10.0',
'Host':'msgctr.m1.com.sg',
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Referer':'http://msgctr.m1.com.sg/guest/index.jsp',
'Content-Type':'application/x-www-form-urlencoded',
'Accept-Language':'en-us,en;q=0.5',
'Accept-Encoding':'gzip,deflate',
'Accept-Charset':'ISO-8859-1,utf-8;q=0.7,*;q=0.7','Cookie':'JSESSIONID='+sess_id}
# Submit the form
POST_URL = "http://msgctr.m1.com.sg/guest/processMessage.jsp?msisdn="+str(field_contactno.strip())
postOutput = urlopen(Request(POST_URL,headers=headers),data).read()
if "notice=thankyou" in postOutput:
print "SMS sent"
else:
print "Cannot read captcha. Please retry"
else:
print "Cannot read captcha. Please retry"
def printSyntax():
"""Print application syntax."""
print "\nUsage:"
print "------------------------------"
print "python "+str(__file__)+" OPTIONS"
print "\nValid OPTIONS are:"
print "------------------------------"
print " -m <message> Specify the sms message to be sent"
print " -f <name> Specify the sender's name"
print " -t <mobile number> Specify the M1 mobile number"
print "\nContact:"
print "------------------------------"
print "[Web] http://milo2012.wordpress.com"
print "[Mail/Google+] keith.lee2012@gmail.com"
print "[twitter] @keith55"
def main():
global field_from
global field_contactno
global field_msg
field_msg = None
field_contactno = None
field_from = None
if len (sys.argv) < 6:
printSyntax()
sys.exit(1)
else:
try:
opts, args = getopt.getopt (sys.argv[1:], "m:f:t:")
except:
printSyntax()
sys.exit(1)
for opt, arg in opts:
if opt == '-m':
field_msg= arg
elif opt == '-f':
field_from = arg
elif opt == '-t':
field_contactno = arg
sendsms()
if __name__ == "__main__":
main()