Advertisement
Guest User

Kongregate American Calculator

a guest
Jun 19th, 2014
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.00 KB | None | 0 0
  1. import urllib.request
  2.  
  3. def main():
  4.     userID = -1;
  5.    
  6.     if userID == -1:
  7.         while True:
  8.             try: userID = int(input("Enter User ID: ")); break;
  9.             except: print("Invalid response.");
  10.  
  11.     print("Evaluating forum data from User ID: {}".format(userID));
  12.     print("Analyzing last 200 posts...");
  13.     times = [];
  14.  
  15.     for page in range(1,11):
  16.         print("Analyzing posts {}-{}...".format((20*(page-1))+1, 20*page))
  17.         fullUrl = "http://www.kongregate.com/users/{}/posts?page={}".format(userID, page);
  18.  
  19.         url = urllib.request.urlopen(fullUrl);
  20.         urlString = url.read().decode(encoding='UTF-8');
  21.         urlSplit = urlString.split("\n");
  22.  
  23.         count = 0;
  24.         for index in range(len(urlSplit)):
  25.             line = urlSplit[index];
  26.             if "<abbr class=\"updated\"" in line:
  27.                 times.append(line[35:-2]);
  28.                 count += 1;
  29.         if count == 0: break;
  30.  
  31.     if len(times) == 0: print("You don't have any forum posts."); return;
  32.  
  33.     hourDict = dict();
  34.  
  35.     for hour in range(24):
  36.         hourDict[hour] = 0;
  37.  
  38.     for time in times:
  39.         hour = time[11:-12];
  40.         hourDict[int(hour)] += 1;
  41.  
  42.     print("\nPosts by hour (PDT):");
  43.  
  44.     for hour in sorted(hourDict):
  45.         print("{:02d}: {}".format(hour, "x"*hourDict[hour]));
  46.  
  47.     minPost = 200;
  48.     sumPosts = 0;
  49.     sleep = 0;
  50.     current = 0;
  51.     for hour in sorted(hourDict):
  52.         sumPosts = 0;
  53.         current = hour;
  54.         for nextHour in range(6):
  55.             sumPosts += hourDict[current];
  56.             if current == 23: current = 0;
  57.             else: current += 1;
  58.         if sumPosts < minPost:
  59.             minPost = sumPosts;
  60.             sleep = hour;
  61.  
  62.     print("\nYou sleep at around: {} o'clock (PDT).".format(sleep));
  63.     if 0 < sleep < 20: print("Therefore, you are probably NOT an American!");
  64.     else: print("Therefore, you probably ARE an American!");
  65.     print("...Or you have terrible sleeping habits.");    
  66.    
  67. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement