Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import urllib.request as re
- import urllib.error as err
- import json
- import time
- def clearify(s):
- pos = s.find('\\x')
- while pos != -1:
- s = s[:pos]+'0'+s[pos+4:]
- pos = s.find('\\x')
- return s
- def getusers():
- out = open("users.txt", "w")
- req = re.urlopen("http://codeforces.com/api/user.ratedList")
- data = req.read()
- print(str(data))
- data = clearify(str(data))[26:-3]+']'
- data = json.loads(data)
- for d in data:
- out.write(d['handle'] + "\n")
- out.close()
- res = [[] for x in range(700)]
- users = []
- contests = []
- def addUser(n):
- try:
- req = re.urlopen("http://codeforces.com/api/user.rating?handle=" + users[n].decode())
- data = req.read()
- data = clearify(str(data))[26:-3]+']'
- data = json.loads(data)
- for i in range(len(data)):
- res[data[i]['contestId']].append([data[i]['newRating'], data[i]['handle'].encode()])
- if (i != len(data) - 1):
- for j in range(data[i]['contestId'] + 1, data[i + 1]['contestId']):
- res[j].append([data[i]['newRating'], data[i]['handle'].encode()])
- for j in range(data[-1]['contestId'] + 1, len(res)):
- res[j].append([data[-1]['newRating'], data[-1]['handle'].encode()])
- except (err.URLError, NameError, SyntaxError, ValueError):
- print("Fail")
- def getContests():
- for n in range(len(res)):
- if (len(res[n]) == 0):
- contests.append(b"")
- else:
- try:
- req = re.urlopen("http://codeforces.com/api/contest.standings?contestId="+ str(n) + "&from=1&count=1")
- data = req.read()
- pos = data.find(b"name")
- pos1 = data[pos + 7:].find(b'"') + pos + 7
- contests.append(data[pos + 7:pos1])
- except (err.URLError, NameError, SyntaxError, ValueError):
- continue
- if (n % 5 == 4):
- time.sleep(0.6)
- out = open("contests.txt", "wb")
- for i in contests:
- out.write(i + b"\n")
- #getusers()
- for user in open("users.txt", "rb"):
- users.append(user.split()[0])
- for i in range(len(users)):
- addUser(i)
- print(i, '. ', users[i], sep="")
- if (i % 5 == 4):
- time.sleep(0.6)
- #getContests()
- for contest in open("contests.txt", "rb"):
- contests.append(b' '.join(contest.split()))
- out = open("res.txt", "w")
- out.close()
- for i in range(len(contests)):
- res[i].sort(reverse=True)
- if (len(res[i]) == 0):
- continue
- out = open("res.txt", "ab")
- out.write(contests[i] + b"\n\n")
- for j in range(min(len(res[i]), 100)):
- out.write(str(j + 1).encode() + b". " + res[i][j][1] + b" " + str(res[i][j][0]).encode() + b"\n")
- out.write(b"\n")
- out.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement