Skip to content

Commit 1bae439

Browse files
committed
modularize urltosite - avoid manual ifs
1 parent 7da9763 commit 1bae439

9 files changed

Lines changed: 52 additions & 31 deletions

File tree

modules/sites/atcoder.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,18 @@ class Profile(object):
3232
# -------------------------------------------------------------------------
3333
def __init__(self, handle=""):
3434
"""
35-
@param handle (String): Spoj handle
35+
@param handle (String): AtCoder handle
3636
"""
3737

3838
self.site = Profile.site_name
3939
self.handle = handle
4040

41+
# -------------------------------------------------------------------------
42+
@staticmethod
43+
def is_valid_url(url):
44+
return url.__contains__("kenkoooo.com/") or \
45+
url.__contains__("atcoder.jp/")
46+
4147
# -------------------------------------------------------------------------
4248
@staticmethod
4349
def is_website_down():
@@ -74,8 +80,6 @@ def get_problem_setters():
7480
def get_editorial_link(problem_link):
7581
"""
7682
"""
77-
# @Todo fill this
78-
7983
try:
8084
contest_id = re.search("contests/.*/tasks",
8185
problem_link).group().split("/")[1]
@@ -118,7 +122,7 @@ def is_invalid_handle(handle):
118122
# -------------------------------------------------------------------------
119123
@staticmethod
120124
def rating_graph_data(handle):
121-
url = "https://atcoder.jp/users/%s/history" % handle
125+
url = "%susers/%s/history" % (current.SITES["AtCoder"], handle)
122126

123127
response = get_request(url)
124128
if response in REQUEST_FAILURES:
@@ -151,7 +155,7 @@ def rating_graph_data(handle):
151155
def get_submissions(self, last_retrieved,
152156
atcoder_problem_dict, is_daily_retrieval):
153157
"""
154-
Retrieve Spoj submissions after last retrieved timestamp
158+
Retrieve AtCoder submissions after last retrieved timestamp
155159
156160
@param last_retrieved (DateTime): Last retrieved timestamp for the user
157161
@param atcoder_problem_dict (Dict): Problem ID to name mapping

modules/sites/codechef.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ def __init__(self, handle=""):
5757
self.is_daily_retrieval = False
5858
self.last_retrieved_reached = False
5959

60+
# -------------------------------------------------------------------------
61+
@staticmethod
62+
def is_valid_url(url):
63+
return url.__contains__("codechef.com/")
64+
6065
# -------------------------------------------------------------------------
6166
@staticmethod
6267
def is_website_down():

modules/sites/codeforces.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ def __init__(self, handle=""):
3838
self.site = Profile.site_name
3939
self.handle = handle
4040

41+
42+
# -------------------------------------------------------------------------
43+
@staticmethod
44+
def is_valid_url(url):
45+
return url.__contains__("codeforces.com/")
46+
4147
# -------------------------------------------------------------------------
4248
@staticmethod
4349
def is_website_down():

modules/sites/hackerearth.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ def __init__(self, handle=""):
3838
self.site = Profile.site_name
3939
self.handle = handle
4040

41+
# -------------------------------------------------------------------------
42+
@staticmethod
43+
def is_valid_url(url):
44+
return url.__contains__("hackerearth.com/")
45+
4146
# -------------------------------------------------------------------------
4247
@staticmethod
4348
def is_website_down():

modules/sites/hackerrank.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ def __init__(self, handle=""):
4040

4141
# -------------------------------------------------------------------------
4242
@staticmethod
43+
def is_valid_url(url):
44+
return url.__contains__("hackerrank.com/")
45+
46+
# --------------------------------------------------------------------------
47+
@staticmethod
4348
def is_website_down():
4449
"""
4550
@return (Boolean): If the website is down

modules/sites/spoj.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ def __init__(self, handle=""):
4040
self.submissions = []
4141
self.retrieval_failure = None
4242

43+
# -------------------------------------------------------------------------
44+
@staticmethod
45+
def is_valid_url(url):
46+
return url.__contains__("spoj.com/") or url == current.spoj_lambda_url
47+
4348
# -------------------------------------------------------------------------
4449
@staticmethod
4550
def is_website_down():

modules/sites/timus.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ def __init__(self, handle=""):
3838
self.site = Profile.site_name
3939
self.handle = handle
4040

41+
# -------------------------------------------------------------------------
42+
@staticmethod
43+
def is_valid_url(url):
44+
return url.__contains__("acm.timus.ru/")
45+
4146
# -------------------------------------------------------------------------
4247
@staticmethod
4348
def is_website_down():

modules/sites/uva.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ def __init__(self, handle=""):
3838
self.site = Profile.site_name
3939
self.handle = handle
4040

41+
# -------------------------------------------------------------------------
42+
@staticmethod
43+
def is_valid_url(url):
44+
return url.__contains__("uva.onlinejudge.org") or \
45+
url.__contains__("uhunt.felix-halim.net")
46+
4147
# -------------------------------------------------------------------------
4248
@staticmethod
4349
def is_website_down():

modules/utilities.py

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -640,33 +640,13 @@ def urltosite(url):
640640
641641
@param url (String): Site URL
642642
@return url (String): Site
643-
644-
@Todo: Move this to individual sites?
645643
"""
646-
if url.__contains__("uva.onlinejudge.org") or url.__contains__("uhunt.felix-halim.net"):
647-
return "uva"
648-
if url.__contains__("acm.timus.ru"):
649-
return "timus"
650-
if url.__contains__("codechef.com"):
651-
return "codechef"
652-
if url.__contains__("spoj.com"):
653-
return "spoj"
654-
if url.__contains__("codeforces.com"):
655-
return "codeforces"
656-
if url == current.spoj_lambda_url:
657-
return "spoj"
658-
if url.__contains__("kenkoooo.com/") or \
659-
url.__contains__("atcoder.jp"):
660-
return "atcoder"
661-
662-
# Note: try/except is not added because this function is not to
663-
# be called for invalid problem urls
664-
site = re.search(r"www\..*?\.com", url).group()
665-
666-
# Remove www. and .com from the url to get the site
667-
site = site[4:-4]
668-
669-
return site
644+
import sites
645+
for site in current.SITES:
646+
if getattr(sites, site.lower()).Profile.is_valid_url(url):
647+
return site.lower()
648+
649+
return "unknown_site"
670650

671651
# -----------------------------------------------------------------------------
672652
def problem_widget(name,

0 commit comments

Comments
 (0)