PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/imo.py

https://gitlab.com/rithvikvibhu/batch-sof
Python | 71 lines | 57 code | 8 blank | 6 comment | 9 complexity | 60995a5ac866b0ab2d2b1b58a209a0e2 MD5 | raw file
  1. import requests
  2. import json
  3. import pprint
  4. from bs4 import BeautifulSoup
  5. print "batch-sof: IMO\nAuthor: Rithvik\n-----------------\n"
  6. print "This script can be used to dump student records of International Math Olympiyad (IMO).\n"
  7. jsondumpFinal = []
  8. logfile = open("log.txt", "w+")
  9. prettyfile = open("pretty.txt", "w+")
  10. ## Get target class
  11. grade = raw_input( "Enter class (1-12): " )
  12. while grade.isdigit() == False or int(grade) > 12 or int(grade) < 1:
  13. grade = raw_input("Enter class (1-12): ")
  14. print "Grade: ", grade
  15. ## Get target school (code)
  16. school = raw_input( "Enter school code: " )
  17. while len(school) != 6 or school[0:1].isdigit() == True or school[2:5].isdigit() == False:
  18. school = raw_input("Enter school code: ")
  19. print "School: ", school[0:1], school[2:3], school[4:5]
  20. ## Get range
  21. rollrange = raw_input( "Range (1-999): " )
  22. while rollrange.isdigit() == False or int(rollrange) >= 1000 or int(rollrange) <= 0:
  23. rollrange = raw_input("Range (1-999): ")
  24. rolllist = range(int(rollrange))
  25. print "Roll: ", rolllist
  26. ## Debug
  27. '''
  28. grade = 5
  29. #school = "KA0466"
  30. rolllist = range(10)
  31. '''
  32. ## Main
  33. for index in rolllist:
  34. i = index+1
  35. i = '%03d' % i
  36. payload = {"action":"result", "rollnumber1":school, "rollnumber2":grade, "rollnumber3":i}
  37. r = requests.post("http://server1.sofworld.org/imo-result/show.php", data = payload)
  38. soup = BeautifulSoup(r.text, "html5lib") # Soup up html
  39. table_data = [[cell.text for cell in row("td")]
  40. for row in BeautifulSoup(r.text, "html5lib")("tr")]
  41. jsondump = json.dumps(dict(table_data)) # Create json object
  42. data = json.loads(jsondump) # Load json into data
  43. if 'Class:' in data: # Check if valid record
  44. logfile.write("%s\n" % jsondump)
  45. print jsondump
  46. logfile.close() # Close log.txt
  47. logfile = open("log.txt", "r")
  48. jd2 = json.dumps('[' + ','.join( logfile.readlines() ) +']').decode('string_escape').replace("Parent\'s","Parent")
  49. print jd2[1:-1] # Remove trailing quotes
  50. prettyfile.write(jd2[1:-1]) #Log to pretty.txt
  51. logfile.close() # Close log.txt
  52. prettyfile.close() # Close pretty.txt
  53. exit()