Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix API for new kattis website layout #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
implement problem caching on local drive
aureliony committed Jun 24, 2023
commit 342019029b4d721a587a57e9a066204e0c0013b6
23 changes: 23 additions & 0 deletions kattis/database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import requests
import os
from .utils import Utils
from bs4 import BeautifulSoup

CACHE_DIR = os.path.expanduser("~") + "/.cache/kattis/"

class Database:
def __init__(self):
if not os.path.exists(CACHE_DIR):
os.makedirs(CACHE_DIR)
self.db = set()
for root, dirs, files in os.walk(CACHE_DIR):
self.db.update(files)

def get(self, filename, url):
filename += ".html"
if filename in self.db:
return BeautifulSoup(open(CACHE_DIR + filename, 'r', encoding = 'utf-8').read(), "html.parser")
else:
r = requests.get(url)
open(CACHE_DIR + filename, 'w', encoding = 'utf-8').write(r.text)
return Utils.html_page(r)
8 changes: 4 additions & 4 deletions kattis/problems.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import requests
import re
import json
from .utils import Utils
from kattis.database import Database

database = Database()
URL = "https://open.kattis.com/problems/"


def problems(pages=1) -> dict:
"""
Fetches all Kattis problems
@@ -33,8 +33,8 @@ def problem(problem_id: str) -> dict:
"stats_url": URL + problem_id + "/statistics",
}

problem_page = Utils.html_page(requests.get(obj["url"]))
stats_page = Utils.html_page(requests.get(obj["stats_url"]))
problem_page = database.get(problem_id, obj["url"])
stats_page = database.get(problem_id + "_statistics", obj["stats_url"])

add_problem_information(problem_page, obj)
add_problem_statistics(stats_page, obj)