Advertisement
PikMike

Untitled

Mar 15th, 2016
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.78 KB | None | 0 0
  1. import urllib.request as re
  2. import urllib.error as err
  3. import json
  4. import time
  5.  
  6. def clearify(s):
  7.     pos = s.find('\\x')
  8.     while pos != -1:
  9.         s = s[:pos]+'0'+s[pos+4:]
  10.         pos = s.find('\\x')
  11.     return s
  12.  
  13. def getusers():
  14.     out = open("users.txt", "w")
  15.     req = re.urlopen("http://codeforces.com/api/user.ratedList")
  16.     data = req.read()
  17.     print(str(data))
  18.     data = clearify(str(data))[26:-3]+']'
  19.     data = json.loads(data)
  20.     for d in data:
  21.         out.write(d['handle'] + "\n")
  22.     out.close()
  23.  
  24. res = [[] for x in range(700)]
  25. users = []
  26. contests = []
  27.  
  28. def addUser(n):
  29.     try:
  30.         req = re.urlopen("http://codeforces.com/api/user.rating?handle=" + users[n].decode())
  31.         data = req.read()
  32.         data = clearify(str(data))[26:-3]+']'
  33.         data = json.loads(data)
  34.         for i in range(len(data)):
  35.             res[data[i]['contestId']].append([data[i]['newRating'], data[i]['handle'].encode()])
  36.             if (i != len(data) - 1):
  37.                 for j in range(data[i]['contestId'] + 1, data[i + 1]['contestId']):
  38.                     res[j].append([data[i]['newRating'], data[i]['handle'].encode()])
  39.         for j in range(data[-1]['contestId'] + 1, len(res)):
  40.             res[j].append([data[-1]['newRating'], data[-1]['handle'].encode()])
  41.     except (err.URLError, NameError, SyntaxError, ValueError):
  42.         print("Fail")
  43.  
  44. def getContests():
  45.     for n in range(len(res)):
  46.         if (len(res[n]) == 0):
  47.             contests.append(b"")
  48.         else:
  49.             try:
  50.                 req = re.urlopen("http://codeforces.com/api/contest.standings?contestId="+ str(n) + "&from=1&count=1")
  51.                 data = req.read()
  52.                 pos = data.find(b"name")
  53.                 pos1 = data[pos + 7:].find(b'"') + pos + 7
  54.                 contests.append(data[pos + 7:pos1])
  55.             except (err.URLError, NameError, SyntaxError, ValueError):
  56.                 continue
  57.         if (n % 5 == 4):
  58.             time.sleep(0.6)
  59.     out = open("contests.txt", "wb")
  60.     for i in contests:
  61.         out.write(i + b"\n")
  62.  
  63. #getusers()
  64. for user in open("users.txt", "rb"):
  65.     users.append(user.split()[0])
  66. for i in range(len(users)):
  67.     addUser(i)
  68.     print(i, '. ', users[i], sep="")
  69.     if (i % 5 == 4):
  70.         time.sleep(0.6)
  71. #getContests()
  72. for contest in open("contests.txt", "rb"):
  73.     contests.append(b' '.join(contest.split()))
  74. out = open("res.txt", "w")
  75. out.close()
  76. for i in range(len(contests)):
  77.     res[i].sort(reverse=True)
  78.     if (len(res[i]) == 0):
  79.         continue
  80.     out = open("res.txt", "ab")
  81.     out.write(contests[i] + b"\n\n")
  82.     for j in range(min(len(res[i]), 100)):
  83.         out.write(str(j + 1).encode() + b". " + res[i][j][1] + b" " + str(res[i][j][0]).encode() + b"\n")
  84.     out.write(b"\n")
  85.     out.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement