#!/usr/bin/env python
# ----------------------------------------------------------------------
# Cusat Results
# By:Sreekumar K.R
# http://sreekumarkr.wordpress.com
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# ----------------------------------------------------------------------
# Please report errors and suggestions to sreekumar.dsja@gmail.com
#
import sys
import os
import string
def help():
print "Invalid number of arguments!"
print "-------------------------------------------------------------------"
print "Usage:"
print " -r <regno>"
print " -m <month>"
print " -y <year>"
print " -s <semester>"
print " -supply"
print " Example usage:-\n ./result -r 12080082 -m 6 -y 2008 -s 1"
print " For supplimentary results:-\n ./result -r 12080082 -m 6 -y 2008 -s 1 -supply\n"
print "-------------------------------------------------------------------"
agent="Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3"
# url to post data
url="http://results.cusat.ac.in/regforms/mrklst-main.php"
# Flags to validate commandline arguments
rok=mok=sok=yok=0
exam = "Regular"
months=['JANUARY','FEBRUARY','MARCH','APRIL','MAY','JUNE','JULY','AUGUST','SEPTEMBER','OCTOBER','NOVEMBER','DECEMBER']
semesters=['','I&II','I&II','III','IV','V','VI','VII','VIII']
# Validating the number of arguments
if(len(sys.argv)<9):
print "Invalid number of arguments!"
help() # print help
exit() # exit from the program
# Processing commandline arguments
for k in range(1,len(sys.argv)):
a = sys.argv[k]
if(a=="-r"):
regno = sys.argv[k+1]
rok=1
elif(a=="-m"):
month = sys.argv[k+1]
mok=1
elif(a=="-s"):
sem = sys.argv[k+1]
sok=1
elif(a=='-y' and (len(sys.argv[k+1])==4)):
year = sys.argv[k+1]
yok=1
elif(a=="-supply"):
exam = "Supplimentary"
else:
continue
#Again validating the arguments
if(not(rok and mok and sok and yok)==1):
help()
exit()
if(not(int(sem)>0 and int(sem)<9)):
print "Invalid number of arguments!"
help()
exit()
if(not(int(month)>0 and int(month)<13)):
print "Invalid Month, Try again"
help()
exit()
# Formating the edate variable from user date
# 'JUNE 2007' - format of edate variable
edate=months[int(month)-1]+" "+year
# Formating semester id
semid=semesters[int(sem)]
print "Please wait........."
# curl command to post data to the server
print exam
curlcommand = "curl --silent -F \"reg=R\" -F \"rno="+regno+"\" -F \"degree=B.Tech\" -F \"sem="+semid+"\" -F \"exam="+exam+"\" -F \"edate="+edate+"\" -A \""+agent+"\" "+url
# Executing curl command
htmltext = os.popen(curlcommand).read()
#Following section process the htmltext(html responce text ) from the server
n=""
start = htmltext.index("Subject")
end = htmltext.index("Disclaimer")
starttag=0
print "----------------------------------------------------------"
#Print the marklist
print "Marklist"
for i in range(start+110,end-1):
if(htmltext[i]=="<"):
starttag=1
elif(htmltext[i]==">"):
starttag=0
if(starttag==0):
n+=htmltext[i]
arr = n.rsplit(">>>>")
for i in range(0,len(arr)):
#print "----------------------------------------------------------"
col = arr[i].rsplit(">>")
row=""
for j in range(0,len(col)):
row+=col[j].ljust(30)+" "
print row
#This section is for debugging purpose, if 'html' is the first argument, then this section will print the whole responce html.
if(len(sys.argv)>1):
if(sys.argv[1]=="html"):
print htmltext
Copy paste the above code in to a file and save it with some name say 'result' without extenstion and then run the program from a terminal by typing
$ chmod +x result
the above code sets permission to the program to run
$ ./result
that will give you a help screen
Example :
$ ./result -r 12080082 -m 6 -y 2008 -s 1
Python!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!