Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import json
- import urllib.request
- import urllib.error
- import numpy as np
- import matplotlib.pyplot as plt
- import matplotlib.cm as cm
- TEST_START = time.mktime(time.strptime("12 Mar 20", "%d %b %y"))
- TEST_END = time.mktime(time.strptime("13 Apr 20", "%d %b %y"))
- CONTROL_START = time.mktime(time.strptime("26 Sep 19", "%d %b %y"))
- CONTROL_END = time.mktime(time.strptime("27 Oct 19", "%d %b %y"))
- CF = "https://codeforces.com/api/"
- def request (req):
- cnt = 0
- while True:
- if cnt > 100:
- print(req)
- exit()
- try:
- ans = json.loads(urllib.request.urlopen(CF + req).read())
- if ans["status"] == "OK":
- return ans["result"]
- except KeyboardInterrupt:
- exit()
- except: # spaghetti
- cnt += 1
- pass
- print("Downloading list of active users... ")
- # Note that "active" in this case means _last month_, not last 6 months
- # Take this into account when modifying this.
- active_users = request("user.ratedList?activeOnly=true")
- def analyze_user (handle, intervals):
- rating_hist = request("user.rating?handle=" + handle)
- cnt = [0 for i in range(len(intervals))]
- diffs = [[None, None] for i in range(len(intervals))]
- for change in rating_hist:
- change_at = change["ratingUpdateTimeSeconds"]
- for idx, (l, r) in enumerate(intervals):
- if l <= change_at and change_at < r:
- cnt[idx] += 1
- if diffs[idx][0] == None:
- diffs[idx][0] = change["oldRating"]
- diffs[idx][1] = change["newRating"]
- return (cnt, diffs)
- SCALE = 50
- test_grid = np.zeros((4000 // SCALE, 4000 // SCALE))
- control_grid = np.zeros((4000 // SCALE, 4000 // SCALE))
- print("Analyzing users...")
- for idx, user in enumerate(active_users):
- if idx % 10 == 0:
- print(idx, "of", len(active_users))
- cnt, diffs = analyze_user(user["handle"], [(CONTROL_START, CONTROL_END), (TEST_START, TEST_END)])
- if cnt[0] >= 3 and cnt[1] >= 3:
- # let's filter out the trolls with negative rating, they cause negative indices
- if diffs[0][0] >= 0 and diffs[0][1] >= 0 and diffs[1][0] >= 0 and diffs[1][1] >= 0:
- control_grid[diffs[0][0] // SCALE][diffs[0][1] // SCALE] += 1
- test_grid[diffs[1][0] // SCALE][diffs[1][1] // SCALE] += 1
- test_grid /= np.maximum(np.ones(4000 // SCALE), test_grid.sum(axis = 1))[:, np.newaxis]
- control_grid /= np.maximum(np.ones(4000 // SCALE), control_grid.sum(axis = 1))[:, np.newaxis]
- fig = plt.figure()
- ax1 = fig.add_subplot(121)
- ax1.imshow(test_grid, interpolation = 'nearest', origin = 'lower',
- cmap = cm.Greys, extent = [0, 4000, 0, 4000])
- ax2 = fig.add_subplot(122)
- ax2.imshow(control_grid, interpolation = 'nearest', origin = 'lower',
- cmap = cm.Greys, extent = [0, 4000, 0, 4000])
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment