Skip to content

Commit

Permalink
Merge pull request #16 from lanjelot/fix-ctftime-import
Browse files Browse the repository at this point in the history
dont show finished CTFs under Import
  • Loading branch information
hugsy authored Dec 22, 2020
2 parents 4c4e672 + b7c34f6 commit bed5092
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 26 deletions.
38 changes: 17 additions & 21 deletions ctfpad/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,40 +106,36 @@ def ctftime_parse_date(date: str) -> datetime:
return ""


@lru_cache(maxsize=128)
def ctftime_fetch_running_ctf_data(limit=100) -> list:
"""Retrieve the currently running CTFs from CTFTime API. I couldn't do this by only using the CTFTime API.
def ctftime_ctfs(running=True, future=True) -> list:
"""Return CTFs that are currently running and starting in the next 6 months.
Returns:
list: JSON output from CTFTime
list: current and future CTFs
"""
ctfs = ctftime_fetch_ctf_data()
now = datetime.now()
try:
res = requests.get(f"{CTFTIME_API_EVENTS_URL}?limit={limit}&start={time()-(3600*24*7):.0f}&finish={time()+(3600*24*7):.0f}",
headers={"user-agent": CTFTIME_USER_AGENT})
if res.status_code != requests.codes.ok:
raise RuntimeError(f"CTFTime service returned HTTP code {res.status_code} (expected {requests.codes.ok}): {res.reason}")
result = []
for ctf in res.json():
start, finish, now = ctftime_parse_date(ctf["start"]), ctftime_parse_date(ctf["finish"]), now
if start < now and finish > now:
result.append(ctf)
except Exception as e:
print(e)
result = []
return result

result = []
for ctf in ctfs:
start, finish = ctftime_parse_date(ctf["start"]), ctftime_parse_date(ctf["finish"])
if running and start < now < finish:
result.append(ctf)
if future and now < start < finish:
result.append(ctf)
return result


@lru_cache(maxsize=128)
def ctftime_fetch_next_ctf_data(limit=100) -> list:
"""Retrieve upcoming CTFs from CTFTime API
def ctftime_fetch_ctf_data(limit=100) -> list:
"""Retrieve CTFs from CTFTime API with a wide start/finish window (-1/+26 weeks) so we can later run our own filters
on the cached results for better performance and accuracy.
Returns:
list: JSON output from CTFTime
"""
try:
res = requests.get(f"{CTFTIME_API_EVENTS_URL}?limit={limit}", headers={"user-agent": CTFTIME_USER_AGENT})
res = requests.get(f"{CTFTIME_API_EVENTS_URL}?limit={limit}&start={time()-(3600*24*7):.0f}&finish={time()+(3600*24*7*26):.0f}",
headers={"user-agent": CTFTIME_USER_AGENT})
if res.status_code != requests.codes.ok:
raise RuntimeError(f"CTFTime service returned HTTP code {res.status_code} (expected {requests.codes.ok}): {res.reason}")
result = res.json()
Expand Down
4 changes: 2 additions & 2 deletions ctfpad/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from model_utils.fields import MonitorField, StatusField
from model_utils import Choices, FieldTracker

from ctfpad.helpers import ctftime_fetch_next_ctf_data
from ctfpad.helpers import ctftime_ctfs


from ctftools.settings import (
Expand Down Expand Up @@ -682,7 +682,7 @@ def search_in_ctftime(cls, query: str) -> list:
list: [description]
"""
results = []
for entry in ctftime_fetch_next_ctf_data():
for entry in ctftime_ctfs(running=False, future=True):
if query in entry["title"].lower() or query in entry["description"].lower():
results.append(
SearchResult(
Expand Down
5 changes: 2 additions & 3 deletions ctfpad/views/ctfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
)
from ctfpad.models import Ctf
from ctfpad.helpers import (
ctftime_fetch_running_ctf_data,
ctftime_fetch_next_ctf_data,
ctftime_ctfs,
ctftime_get_ctf_info,
ctftime_parse_date
)
Expand All @@ -30,7 +29,7 @@ class CtfListView(LoginRequiredMixin, ListView):
paginate_by = 25
ordering = ["-id"]
extra_context = {
"ctftime_ctfs": ctftime_fetch_running_ctf_data() + ctftime_fetch_next_ctf_data(),
"ctftime_ctfs": ctftime_ctfs(running=True, future=True),
}

def get_queryset(self):
Expand Down

0 comments on commit bed5092

Please # to comment.