Advertisement
Guest User

Untitled

a guest
May 25th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. import requests
  2. from bs4 import BeautifulSoup
  3. import json
  4. from pprint import pprint
  5. import sys
  6. import time
  7. import datetime
  8. s = requests.session()
  9.  
  10.  
  11. def login():
  12. url = "http://informatics.mccme.ru/login/index.php"
  13. data = dict(
  14. username = "",
  15. password = ""
  16. )
  17. s.post(url, data)
  18.  
  19.  
  20. def get_table(url):
  21. result = s.get(url).text
  22. return json.loads(result)["result"]["text"]
  23.  
  24.  
  25. urls = [
  26. "http://informatics.mccme.ru/moodle/ajax/ajax.php?problem_id=0&group_id=5100&user_id=0&lang_id=-1&status_id=-1&statement_id=10529&objectName=submits&count=10000&with_comment=&page=0&action=getHTMLTable",
  27. "http://informatics.mccme.ru/moodle/ajax/ajax.php?problem_id=0&group_id=5100&user_id=0&lang_id=-1&status_id=-1&statement_id=10599&objectName=submits&count=10000&with_comment=&page=0&action=getHTMLTable",
  28. "http://informatics.mccme.ru/moodle/ajax/ajax.php?problem_id=0&group_id=5100&user_id=0&lang_id=-1&status_id=-1&statement_id=10639&objectName=submits&count=10000&with_comment=&page=0&action=getHTMLTable",
  29. "http://informatics.mccme.ru/moodle/ajax/ajax.php?problem_id=0&group_id=5100&user_id=0&lang_id=-1&status_id=-1&statement_id=10718&objectName=submits&count=10000&with_comment=&page=0&action=getHTMLTable",
  30. "http://informatics.mccme.ru/moodle/ajax/ajax.php?problem_id=0&group_id=5100&user_id=0&lang_id=-1&status_id=-1&statement_id=10779&objectName=submits&count=10000&with_comment=&page=0&action=getHTMLTable",
  31. "http://informatics.mccme.ru/moodle/ajax/ajax.php?problem_id=0&group_id=5100&user_id=0&lang_id=-1&status_id=-1&statement_id=10817&objectName=submits&count=10000&with_comment=&page=0&action=getHTMLTable",
  32. "http://informatics.mccme.ru/moodle/ajax/ajax.php?problem_id=0&group_id=5100&user_id=0&lang_id=-1&status_id=-1&statement_id=10843&objectName=submits&count=10000&with_comment=&page=0&action=getHTMLTable",
  33.  
  34. ]
  35. students = dict()
  36.  
  37. for url in urls:
  38. soup = BeautifulSoup(get_table(url), 'html.parser')
  39.  
  40.  
  41. trs = list(soup.find_all("tr"))[1:]
  42. for tr in trs:
  43. #print tr
  44. student = tr.find_all("td")[1].find("a").text
  45. if not (student in students):
  46. students[student] = dict()
  47. lang = tr.find_all("td")[4].text
  48. status = tr.find_all("td")[5].text.strip()
  49. score = tr.find_all("td")[7].text.strip()
  50. problem = unicode(tr.find_all("td")[2].find("a").text).encode("utf8")
  51. try:
  52. score = int(score)
  53. except:
  54. score = 0
  55. # print student, status, problem, lang
  56. if "OK" in status and ("Java" in lang or not lang):
  57. # print "yeee"
  58. if problem in students[student].keys():
  59. students[student][problem] = max(students[student][problem],score)
  60. else:
  61. students[student][problem] = score
  62.  
  63.  
  64.  
  65. result = []
  66.  
  67. for student in students.keys():
  68. sum = 0
  69. plist = []
  70. for problem in students[student].keys():
  71. plist.append((problem, students[student][problem]))
  72. sum += students[student][problem]
  73. item = (student,plist,sum)
  74. result.append(item)
  75.  
  76. result.sort(key=lambda item: item[2])
  77. result.reverse()
  78.  
  79. header = """
  80. <!DOCTYPE html>
  81. <html>
  82. <head>
  83. <meta charset="UTF-8">
  84. </head>
  85. <body>
  86. """
  87.  
  88. footer = """
  89. </body>
  90. </html>
  91. """
  92.  
  93. while True:
  94. with open("results.html","w") as f:
  95. sum = 0
  96. f.write(header)
  97. for item in result:
  98. f.write("%s %s %s<br>" % (item[0].encode('utf8'), item[2], len(item[1])))
  99. sum += len(item[1])
  100. for problem in item[1]:
  101. f.write("%s %s<br>" % (problem[0],problem[1]))
  102. f.write("<br>")
  103.  
  104. #print sum
  105. f.write(footer)
  106. print "Last update at:%s\n" % time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
  107. time.sleep(60*30)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement