Skip to content

Instantly share code, notes, and snippets.

@petr-kalinin
Created October 9, 2015 15:04
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 petr-kalinin/2478c5f964b04096447c to your computer and use it in GitHub Desktop.
Save petr-kalinin/2478c5f964b04096447c to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys,codecs
import urllib.request
import re
import json
import datetime
sys.stdout = codecs.getwriter('cp866')(sys.stdout.buffer, 'replace')
# Лицей 40
#tableBaseUrl = 'http://informatics.mccme.ru/moodle/ajax/ajax.php?problem_id=0&group_id=3696&user_id=0&lang_id=-1&status_id=-1&statement_id=0&objectName=submits&count=10&with_comment=&page=%d&action=getHTMLTable'
# Заоч
tableBaseUrl = 'http://informatics.mccme.ru/moodle/ajax/ajax.php?problem_id=0&group_id=3644&user_id=0&lang_id=-1&status_id=-1&statement_id=0&objectName=submits&count=10&with_comment=&page=%d&action=getHTMLTable'
AC = 'Зачтено/Принято'
startDateStr = '2015-10-04'
endDateStr = '2015-10-10'
startDate = datetime.datetime.strptime(startDateStr + " 00:00:00", "%Y-%m-%d %H:%M:%S")
endDate = datetime.datetime.strptime(endDateStr + " 23:59:59", "%Y-%m-%d %H:%M:%S")
def readUrl(url, cookies=''):
print('Retrieving %s...' % url, end='');
sys.stdout.flush()
headers = {'Cookie': cookies}
req = urllib.request.Request(url, None, headers)
response = urllib.request.urlopen(req)
res = str(response.read(), encoding='utf-8', errors='replace')
print('Done')
return res
def processSubmit(name, prob, date, outcome, childrenResults):
if date > endDate: # we do not need this, but continue search
return True
if date < startDate: # stop search
return False
#print(name, prob, date, outcome)
if not (name in childrenResults.keys()):
childrenResults[name] = {}
if not (prob in childrenResults[name].keys()):
childrenResults[name][prob] = ""
if (childrenResults[name][prob] != AC) and (outcome == AC):
childrenResults[name][prob] = AC
return True
def parseSubmits(submitsTable, childrenResults):
submitsRows = submitsTable.split("<tr>")
result = False
for row in submitsRows:
data = re.search(r'<td>[^<]*</td>\s*<td><a [^>]*>([^<]*)</a></td>\s*<td><a [^>]*>([^<]*)</a></td>\s*<td>([^<]*)</td>\s*<td>[^<]*</td>\s*<td>([^<]*)</td>', row, re.DOTALL)
if (not data):
continue
name = data.group(1)
prob = data.group(2)
dateStr = data.group(3)
outcome = data.group(4).strip()
date = datetime.datetime.strptime(dateStr, "%Y-%m-%d %H:%M:%S")
resultSubmit = processSubmit(name, prob, date, outcome, childrenResults)
result = result or resultSubmit
return result
childrenResults = {}
page = 0
while True:
submitsUrl = tableBaseUrl % (page)
submitsJson = readUrl(submitsUrl)
submits = json.loads(submitsJson)
result = parseSubmits(submits["result"]["text"], childrenResults)
if not result:
break
page = page + 1
for name, res in childrenResults.items():
cnt = 0
half = 0
for prob, outcome in res.items():
if outcome == AC:
cnt = cnt + 1
else:
half = 0.5
print(name, cnt+half)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment