Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions curate.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python3

from curator.platforms import *
from curator import *
from curator.platforms import *

output.prep_output()

Expand All @@ -10,7 +10,7 @@
submissions = {
dmoj.platform_name(): dmoj.fetch(),
codeforces.platform_name(): codeforces.fetch(),
spoj.platform_name(): spoj.fetch()
spoj.platform_name(): spoj.fetch(),
}

print("Writing to folder...")
Expand Down
2 changes: 1 addition & 1 deletion curator/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__all__ = ["output", "readme", "settings"]
__all__ = ["output", "readme", "settings"]
10 changes: 6 additions & 4 deletions curator/output.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
from curator.util import lang_extensions

from curator import settings
from curator.util import lang_extensions

CONFIG = settings.load_config()
OUTPUT_PATH = CONFIG["output_path"]
Expand All @@ -22,14 +23,15 @@ def write_submissions(submissions):
name = submission["name"]
source = submission["source"]
folder = os.path.join(OUTPUT_PATH, platform)
path = os.path.join(folder, name + "." +
lang_extensions.extension(language))
path = os.path.join(
folder, name + "." + lang_extensions.extension(language)
)
if not os.path.exists(folder):
os.mkdir(folder)
elif not os.path.isdir(folder):
# Why would this even happen
continue
with open(path, 'w+', newline='\n') as file:
with open(path, "w+", newline="\n") as file:
file.write(source)
file.close()
print("Writing to " + path)
2 changes: 1 addition & 1 deletion curator/platforms/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__all__ = ["dmoj", "codeforces", "spoj"]
__all__ = ["dmoj", "codeforces", "spoj"]
12 changes: 7 additions & 5 deletions curator/platforms/codeforces.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from collections import defaultdict
import mechanicalsoup
import json
from collections import defaultdict

import mechanicalsoup
from curator import settings

PLATFORM_NAME = "Codeforces"
Expand All @@ -26,7 +26,9 @@ def platform_name():


def _get_source(contest_id, submission_id):
BROWSER.open("http://codeforces.com/contest/" + contest_id + "/submission/" + submission_id)
BROWSER.open(
"http://codeforces.com/contest/" + contest_id + "/submission/" + submission_id
)
page = BROWSER.get_current_page()
src = page.find("pre", class_="program-source")
if src:
Expand Down Expand Up @@ -61,8 +63,8 @@ def _get_best_submissions(candidates):
"language": submission_object["programmingLanguage"],
"platform": PLATFORM_NAME,
"difficulty": str(problem["points"]),
"link": PROBLEM_URL + contest_id + "/" + str(problem["index"])
"link": PROBLEM_URL + contest_id + "/" + str(problem["index"]),
}
result.append(submission)
result = sorted(result, key=lambda k: k['name'])
result = sorted(result, key=lambda k: k["name"])
return result
18 changes: 10 additions & 8 deletions curator/platforms/dmoj.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from collections import defaultdict
import mechanicalsoup
import json
from collections import defaultdict

import mechanicalsoup
from curator import settings

PLATFORM_NAME = "DMOJ"
Expand All @@ -14,9 +14,9 @@


def fetch():
BROWSER.session.headers['Referer'] = LOGIN_URL
BROWSER.session.headers["Referer"] = LOGIN_URL
BROWSER.open(LOGIN_URL)
BROWSER.select_form('form')
BROWSER.select_form("form")
BROWSER["username"] = CONFIG["username"]
BROWSER["password"] = CONFIG["password"]
auth = BROWSER.submit_selected()
Expand Down Expand Up @@ -44,14 +44,16 @@ def _get_source(submission_id):

def _get_best_submissions(candidates):
result = []
best_submissions = defaultdict(lambda: (0, 0, 2**32))
best_submissions = defaultdict(lambda: (0, 0, 2 ** 32))
for k, v in candidates.items():
if v["result"] == "AC":
pid = v["problem"]
points = v["points"]
time = v["time"]
current_id, current_points, current_time = best_submissions[pid]
if points > current_points or (time < current_time and points == current_points):
if points > current_points or (
time < current_time and points == current_points
):
best_submissions[pid] = (k, points, time)
for problem_id, v in best_submissions.items():
submission_id = str(v[0])
Expand All @@ -65,8 +67,8 @@ def _get_best_submissions(candidates):
"language": candidates[submission_id]["language"],
"platform": PLATFORM_NAME,
"difficulty": str(candidates[submission_id]["points"]),
"link": PROBLEM_URL + problem_id
"link": PROBLEM_URL + problem_id,
}
result.append(submission)
result = sorted(result, key=lambda k: k['name'])
result = sorted(result, key=lambda k: k["name"])
return result
32 changes: 17 additions & 15 deletions curator/platforms/spoj.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections import defaultdict
import mechanicalsoup

import mechanicalsoup
from curator import settings

CONFIG = settings.load_config()["platforms"]["spoj"]
Expand All @@ -13,10 +13,11 @@

BROWSER = mechanicalsoup.StatefulBrowser()


def fetch():
BROWSER.session.headers['Referer'] = LOGIN_URL
BROWSER.session.headers["Referer"] = LOGIN_URL
BROWSER.open(LOGIN_URL)
BROWSER.select_form('form')
BROWSER.select_form("form")
BROWSER["login_user"] = CONFIG["username"]
BROWSER["password"] = CONFIG["password"]
auth = BROWSER.submit_selected()
Expand All @@ -25,7 +26,7 @@ def fetch():
if auth.status_code != 200 or submissions.status_code != 200:
# Error connecting to spoj, throw error
return list()

candidates = _parse_submissions(submissions.text)
return _get_best_submissions(candidates)

Expand All @@ -44,15 +45,15 @@ def _get_source(submission_id):

def _get_best_submissions(candidates):
result = []
best_submissions = defaultdict(lambda: (0, 2**32))
best_submissions = defaultdict(lambda: (0, 2 ** 32))
for k, v in candidates.items():
if v["result"] == "AC":
pid = v["problem"]
time = v["time"]
current_id, current_time = best_submissions[pid]
if time < current_time:
best_submissions[pid] = (k, time)

for problem_id, v in best_submissions.items():
submission_id = str(v[0])
source = _get_source(submission_id)
Expand All @@ -65,26 +66,27 @@ def _get_best_submissions(candidates):
"language": candidates[submission_id]["language"],
"platform": PLATFORM_NAME,
"difficulty": "N/A",
"link": PROBLEM_URL + problem_id
"link": PROBLEM_URL + problem_id,
}
result.append(submission)
result = sorted(result, key=lambda k: k['name'])
result = sorted(result, key=lambda k: k["name"])
return result


def _parse_submissions(plaintext):
plaintext = plaintext.split("\n")
numsubs = len(plaintext) - 22

formatted = {}
submissions = plaintext[9:9+numsubs]
submissions = plaintext[9 : 9 + numsubs]

for sub in submissions:
separated = sub.strip("|").split("|")
separated = list(map(str.strip, separated))
formatted[separated[0]] = {
"problem" : separated[2],
"result" : separated[3],
"time" : float(separated[4]),
"language" : separated[6]
"problem": separated[2],
"result": separated[3],
"time": float(separated[4]),
"language": separated[6],
}
return formatted
return formatted
20 changes: 12 additions & 8 deletions curator/readme.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import os
from curator.util import markdown
from curator.util import lang_extensions

from curator import settings
from curator.util import lang_extensions, markdown

CONFIG = settings.load_config()
FOLDER = CONFIG["output_path"]
README_PATH = os.path.join(FOLDER, CONFIG["readme_name"])


def write_readme(submissions):
with open(README_PATH, 'w+', newline='\n') as f:
with open(README_PATH, "w+", newline="\n") as f:
_write_header(f)
for platform, s in submissions.items():
_write_platform_submissions(f, s, platform)
Expand All @@ -23,15 +23,19 @@ def _write_header(file):

def _write_platform_submissions(file, submissions, platform):
file.write(markdown.h2(platform))
file.write(markdown.table_header(
"Problem", "Source", "Language", "Difficulty"))
file.write(markdown.table_header("Problem", "Source", "Language", "Difficulty"))
for submission in submissions:
name = submission["name"]
language = submission["language"]
difficulty = submission["difficulty"]
link = submission["link"]
file_name = name + "." + lang_extensions.extension(language)
file_path = submission["platform"] + "/" + file_name
file.write(markdown.table_row(
markdown.link(name, link),
markdown.link("Source", file_path), language, difficulty))
file.write(
markdown.table_row(
markdown.link(name, link),
markdown.link("Source", file_path),
language,
difficulty,
)
)
1 change: 1 addition & 0 deletions curator/util/lang_extensions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from curator import settings

TRANSLATIONS = settings.load_extensions()
EXACT = TRANSLATIONS["exact"]
FUZZY = TRANSLATIONS["fuzzy"]
Expand Down