Guest User

Untitled

a guest
Apr 15th, 2020
883
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import time
  2. import json
  3. import urllib.request
  4. import urllib.error
  5. import numpy as np
  6. import matplotlib.pyplot as plt
  7. import matplotlib.cm as cm
  8.  
  9. TEST_START = time.mktime(time.strptime("12 Mar 20", "%d %b %y"))
  10. TEST_END = time.mktime(time.strptime("13 Apr 20", "%d %b %y"))
  11.  
  12. CONTROL_START = time.mktime(time.strptime("26 Sep 19", "%d %b %y"))
  13. CONTROL_END = time.mktime(time.strptime("27 Oct 19", "%d %b %y"))
  14.  
  15. CF = "https://codeforces.com/api/"
  16. def request (req):
  17.     cnt = 0
  18.     while True:
  19.         if cnt > 100:
  20.             print(req)
  21.             exit()
  22.         try:
  23.             ans = json.loads(urllib.request.urlopen(CF + req).read())
  24.             if ans["status"] == "OK":
  25.                 return ans["result"]
  26.         except KeyboardInterrupt:
  27.             exit()
  28.         except: # spaghetti
  29.             cnt += 1
  30.             pass
  31.  
  32. print("Downloading list of active users... ")
  33. # Note that "active" in this case means _last month_, not last 6 months
  34. # Take this into account when modifying this.
  35. active_users = request("user.ratedList?activeOnly=true")
  36.  
  37. def analyze_user (handle, intervals):
  38.     rating_hist = request("user.rating?handle=" + handle)
  39.  
  40.     cnt = [0 for i in range(len(intervals))]
  41.     diffs = [[None, None] for i in range(len(intervals))]
  42.     for change in rating_hist:
  43.         change_at = change["ratingUpdateTimeSeconds"]
  44.         for idx, (l, r) in enumerate(intervals):
  45.             if l <= change_at and change_at < r:
  46.                 cnt[idx] += 1
  47.                 if diffs[idx][0] == None:
  48.                     diffs[idx][0] = change["oldRating"]
  49.                 diffs[idx][1] = change["newRating"]
  50.     return (cnt, diffs)
  51.  
  52. SCALE = 50
  53. test_grid = np.zeros((4000 // SCALE, 4000 // SCALE))
  54. control_grid = np.zeros((4000 // SCALE, 4000 // SCALE))
  55.  
  56. print("Analyzing users...")
  57. for idx, user in enumerate(active_users):
  58.     if idx % 10 == 0:
  59.         print(idx, "of", len(active_users))
  60.  
  61.     cnt, diffs = analyze_user(user["handle"], [(CONTROL_START, CONTROL_END), (TEST_START, TEST_END)])
  62.    
  63.     if cnt[0] >= 3 and cnt[1] >= 3:
  64.         # let's filter out the trolls with negative rating, they cause negative indices
  65.         if diffs[0][0] >= 0 and diffs[0][1] >= 0 and diffs[1][0] >= 0 and diffs[1][1] >= 0:
  66.             control_grid[diffs[0][0] // SCALE][diffs[0][1] // SCALE] += 1
  67.             test_grid[diffs[1][0] // SCALE][diffs[1][1] // SCALE] += 1
  68.  
  69. test_grid /= np.maximum(np.ones(4000 // SCALE), test_grid.sum(axis = 1))[:, np.newaxis]
  70. control_grid /= np.maximum(np.ones(4000 // SCALE), control_grid.sum(axis = 1))[:, np.newaxis]
  71.            
  72. fig = plt.figure()
  73. ax1 = fig.add_subplot(121)
  74. ax1.imshow(test_grid, interpolation = 'nearest', origin = 'lower',
  75.            cmap = cm.Greys, extent = [0, 4000, 0, 4000])
  76. ax2 = fig.add_subplot(122)
  77. ax2.imshow(control_grid, interpolation = 'nearest', origin = 'lower',
  78.            cmap = cm.Greys, extent = [0, 4000, 0, 4000])
  79.  
  80. plt.show()
RAW Paste Data