Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.87 KB | None | 0 0
  1. # CODEFORCES =====================================================
  2. apiKey = "..."
  3. apiSecret = "..."
  4.  
  5. count_response = 0
  6. def get_cf_response(url):
  7.     global count_response
  8.     if count_response==5:
  9.         count_response = 0
  10.         time.sleep(2)
  11.     r = requests.get(url)
  12.     count_response += 1
  13.     return r.json()
  14.  
  15. def get_codeforces_contest_stadings(contestId, showUnofficial=False):
  16.     if showUnofficial:
  17.         showUnofficial = 'true'
  18.     else:
  19.         showUnofficial = 'false'
  20.  
  21.     t = int(time.time())
  22.     s = bytes("rororo/contest.standings?         
  23.     apiKey="+apiKey+"&contestId="+str(contestId)+"&showUnofficial="+showUnofficial+"&time="+str(t)+"#"+apiSecret, 'utf-8')
  24.     h = hashlib.sha512(s).hexdigest()
  25.  
  26.     url = "http://codeforces.com/api/contest.standings?          
  27.     contestId="+str(contestId)+"&showUnofficial="+showUnofficial+"&apiKey="+apiKey+"&time="+str(t)+"&apiSig=rororo"+h
  28.     #print(url)
  29.     return get_cf_response(url)
  30.  
  31. def get_contestName(contestId):
  32.     try:
  33.         standings = get_codeforces_contest_stadings(contestId)
  34.         return standings['result']['contest']['name']
  35.     except:
  36.         return None
  37.  
  38. # [ {'handle': "", 'points': []} ... ]
  39. def get_standings(contestId, showUnofficial=False):
  40.     standings = get_codeforces_contest_stadings(contestId, showUnofficial)
  41.     try:
  42.         ans = []
  43.         for x in standings['result']['rows']:
  44.             handle = x['party']['members'][0]['handle']
  45.             current_user = {'handle': handle, 'points': []}
  46.             for problem in x['problemResults']:
  47.                 points = problem['points']
  48.                 current_user['points'].append(points)
  49.  
  50.             flag = False
  51.             for i in range(len(ans)):
  52.                 if ans[i]['handle'] == current_user['handle']:
  53.                     flag = True
  54.                     for j in range(len(current_user['points'])):
  55.                         ans[i]['points'][j] = max(ans[i]['points'][j], current_user['points'][j])
  56.             if flag == False:
  57.                 ans.append(current_user)
  58.         return ans
  59.     except:
  60.         return None
  61.  
  62. # ============================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement