Skip to content

Instantly share code, notes, and snippets.

@geekpradd
Created December 27, 2014 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geekpradd/a3e7a590887cf7bbf161 to your computer and use it in GitHub Desktop.
Save geekpradd/a3e7a590887cf7bbf161 to your computer and use it in GitHub Desktop.
import requests,json,webbrowser,threading
from bs4 import BeautifulSoup
def getDict(start,stop,threadNo,final=None):
for argv in range(start,stop):
try:
if argv<10:
argv='00'+str(argv)
elif argv<100:
argv='0'+str(argv)
else:
argv=str(argv)
roll='JH0051-08-'+argv
print "Searching for {0} in database - Thread {1}\n".format(roll,str(threadNo))
data={'roll_no':roll,
'op':'Submit',
'form_build_id':'form-04828bfbedb6d64eca8371dbf1afea60',
'form_id':'ca_nso_result_form'
}
url="http://www.sofworld.org/nso-result"
match=match='''<div id="content"> <!--CONTENT-->
<div class="messages error">
<ul>
<li>The Roll No. does not exist.</li>
<li>The Roll No. {0} does not exist.</li>
</ul>
</div>
Please enter the Roll No. in the following format: YourSchoolCode-ClassNumber-RollNo. (AN0001-04-001) </div>'''.format(roll)
page=requests.post(url,data=data).text
soup=BeautifulSoup(page)
tablecont=soup.select("div#content")[0]
if str(tablecont)==match:
print "Roll {0} does not exist.. Skipping It.. -- Thread {1}\n".format(roll,str(threadNo))
else:
table=tablecont.select("table")[0]
name_row=table.select("tr")[1]
name=str(name_row.select("td")[1].string)
roll=roll
marks=table.select("tr")[5]
mark=str(marks.select("td")[1].string)
school=table.select("tr")[10]
rank=str(school.select("td")[1].getText())
print "Appending {0} to list.. -- Thread {1}".format(roll,str(threadNo))
person={'name':name,'roll':roll,'marks':mark,'rank':rank}
appendToFile(person)
except:
continue
def finish():
script='''function sortTable(){
var tbl = document.getElementById("b").tBodies[0];
var store = [];
for(var i=0, len=tbl.rows.length; i<len; i++){
var row = tbl.rows[i];
var sortnr = parseFloat(row.cells[0].textContent || row.cells[0].innerText);
if(!isNaN(sortnr)) store.push([sortnr, row]);
}
store.sort(function(x,y){
return x[0] - y[0];
});
for(var i=0, len=store.length; i<len; i++){
tbl.appendChild(store[i][1]);
}
store = null;
}
sortTable();
'''
bottom='\n\n\n##How was this Table created?\n\nWell, firstly I\'m Pradipta (as you know) and this page is hosted on my custom server (my site). I used a Python script to crawl through SOF\'s website and save the result of each one of you into this Table. The Program is very complicated but you can view the source here. I\'ll be creating such pages for NSO and IMO as well.</center></xmp>\n<script>{0}</script><script src="http://strapdownjs.com/v/0.2/strapdown.js"></script></html>\n'.format(script)
with open('res.html','a')as f:
f.write(bottom)
webbrowser.open("res.html")
def appendToFile(obj):
string='<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td></tr>'.format(obj['name'],obj['roll'],obj['marks'],obj['rank'])
with open('res.html','a')as f:
f.write(string)
def initFile():
head='<html>\n<head>\n<script src="sorttable.js"><title>NCO Result-- Class 8</title>\n<xmp theme="united" style="display:none;">\n <center>## Result of My School\n <table id=\'b\' class="sortable"><tr><td>Rank</td><td>Name</td><td>Marks</td><td>Roll No</td></tr>\n'
with open('res.html','w')as f:
f.write(head)
def createThreads():
global threads
threads=[]
thread1=threading.Thread(target=getDict,args=(1,20,1,))
thread2=threading.Thread(target=getDict,args=(20,40,2,))
thread3=threading.Thread(target=getDict,args=(40,60,3,))
thread4=threading.Thread(target=getDict,args=(60,80,4,))
thread5=threading.Thread(target=getDict,args=(80,100,5,True,))
threads.append(thread1)
threads.append(thread2)
threads.append(thread3)
threads.append(thread4)
threads.append(thread5)
[x.start() for x in threads]
wait()
def wait():
print "Waiting Script... Waiting for threads to finish\n"
[x.join() for x in threads]
finish()
def main():
print "Starting Script..."
initFile()
createThreads()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment