Skip to content

Instantly share code, notes, and snippets.

@theSage21
Last active August 29, 2015 14:21
Show Gist options
  • Save theSage21/6eabcd643b20065697cb to your computer and use it in GitHub Desktop.
Save theSage21/6eabcd643b20065697cb to your computer and use it in GitHub Desktop.
Cbse result fetcher.
from requests import post
def save_page(html):
f = open('html/' + str(hash(html)), 'w')
f.write(html)
f.close()
def mark_done(roll):
f = open('done', 'w')
f.writelines([str(roll)])
f.close()
def get_done():
try:
f = open('done', 'r')
roll = int(f.readlines()[0])
f.close()
except IOError:
roll = 5855000
return str(roll).zfill(7)
def get_result(roll):
# Necessary to fool the CBSE website
URL = 'http://cbseresults.nic.in/class12/cbse122015_all.asp'
AGENT = 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:37.0) Gecko/20100101 Firefox/37.0'
REFERER = 'http://cbseresults.nic.in/class12/cbse122015_all.htm'
data = {'regno': roll,
'B1': 'Submit'}
headers = {'Host': 'cbseresults.nic.in',
'User-Agent': AGENT,
'Referer': REFERER,
'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',
'Connection': 'keep-alive',
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': 23
}
for i in range(4):
try:
response = post(URL, data=data, headers=headers)
except:
pass
else:
return response
def scan():
valid_first_two = '16,17,18,19,26,27,28,29,36,37'
valid_first_two += ',38,39,46,47,48,49,56,57,58,'
valid_first_two += '59,66,67,68,69,76,77,78,79,91'
valid_first_two += '66,67,68,69,76,77,78,79,91'
valid_first_two += ',92,93,94,95,96,97,98,99'
valid_first_two = valid_first_two.split(',')
valid_first_two.sort()
done = int(get_done())
not_found_last_three = 0
for first_two in valid_first_two:
for next_two in range(0, 99):
for last_three in range(0, 999):
roll = first_two + str(next_two).zfill(2)
roll += str(last_three).zfill(3)
if int(roll) <= int(done):
continue
response = get_result(roll)
if response is not None:
source = response.text
if 'Result Not Found' in source:
not_found_last_three += 1
if not_found_last_three > 10:
not_found_last_three = 0
break
continue
save_page(source)
mark_done(roll)
print(roll)
if __name__ == '__main__':
while True:
try:
scan()
except KeyboardInterrupt:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment