def test_get_contest_details(self): lcc = LeetCodeCrawler() contestId = lcc.generateContestId(Contest.BIWEEKLY, 29) res, contest = lcc.getContestDetails(contestId) self.assertTrue(res) # later use: time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(contest['start_time'] results = contest['title'] + " -> " + lcc.contestToJson(contest) + "\n" print(results)
def test_get_contest_results(self): page = 1 lcc = LeetCodeCrawler() contestId = lcc.generateContestId(Contest.BIWEEKLY, 29) res, rank = lcc.getContestRankPage(contestId, page) self.assertTrue(res) results = "" for user in rank: username, result = lcc.resultToJson(contestId, user) results += username + " -> " + result + "\n" print(results) self.assertEqual(25, len(rank))
class DataProxy(): def __init__(self, config: ConfigParser): self.config = config self.leetcode = LeetCodeCrawler(config) self.storage = RedisStorage(config) def pushContest(self, type: Contest, id: int) -> bool: contestId = self.leetcode.generateContestId(type, id) # get the contest details res, contest = self.leetcode.getContestDetails(contestId) if not res: return False # get the contest rank ranks = self.leetcode.getContestRankFull(contestId) # try to acquire the lock and store the new data res, token = self.storage.acquireLock(600) if not res: return False self.storage.addContest(self.leetcode.contestToJson(contest)) for userRank in ranks: username, result = self.leetcode.resultToJson( contestId, userRank, contest['start_time']) self.storage.addContestResult(username, result) self.storage.releaseLock(token) return True def getUser(self, username: str) -> list: # if the storage is empty, try to recover it from the last backup if self.storage.isEmpty(): res, token = self.storage.acquireLock(240) if not res: return False self.storage.importStorage('') self.storage.releaseLock(token) return self.storage.getAllContestsResults(username) def exportStorage(self, fileName: str) -> bool: return self.storage.exportStorage(fileName) def importStorage(self, fileName: str) -> bool: return self.storage.importStorage(fileName)
def test_get_all_contest_results(self): lcc = LeetCodeCrawler() contestId = lcc.generateContestId(Contest.BIWEEKLY, 29) fullRank = lcc.getContestRankFull(contestId, 310) print(len(fullRank))
def test_generate_contest_id(self): lcc = LeetCodeCrawler() self.assertEqual(lcc.generateContestId(Contest.STANDARD, 123), "weekly-contest-123") self.assertEqual(lcc.generateContestId(Contest.BIWEEKLY, 456), "biweekly-contest-456")
def __init__(self, config: ConfigParser): self.config = config self.leetcode = LeetCodeCrawler(config) self.storage = RedisStorage(config)