Advertisement
Guest User

Untitled

a guest
Jan 24th, 2012
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.25 KB | None | 0 0
  1. #!/usr/bin/env python
  2. from __future__ import with_statement
  3. import sys
  4. import urllib2
  5. import os
  6. import datetime
  7. import tempfile
  8. import itertools
  9.  
  10. try:
  11.     import json
  12. except ImportError:
  13.     try:
  14.         import simplejson as json
  15.     except ImportError:
  16.         json = None
  17.  
  18. _DATE_FMT = '%Y%m%d-%H%M%S'
  19. _CACHE_TIME = 600
  20.  
  21. def get_cache_dir():
  22.     return os.path.join(tempfile.gettempdir(), 'epiclan')
  23.  
  24. def get_cache_file():
  25.     cache_dir = get_cache_dir()
  26.     if not os.access(cache_dir, os.F_OK):
  27.         os.mkdir(cache_dir)
  28.         return False
  29.     else:
  30.         files = os.listdir(cache_dir)
  31.         if len(files) == 0:
  32.             return False
  33.  
  34.         now = datetime.datetime.utcnow()
  35.         cache_expire = now - datetime.timedelta(seconds=_CACHE_TIME)
  36.  
  37.         for f in files:
  38.             d = datetime.datetime.strptime(f, _DATE_FMT)
  39.             fullpath = os.path.join(cache_dir, f)
  40.             if cache_expire > d:
  41.                 os.unlink(fullpath)
  42.                 fullpath = False
  43.  
  44.         return fullpath
  45.  
  46. def cache_data(data):
  47.     now = datetime.datetime.utcnow()
  48.     nowstr = now.strftime(_DATE_FMT)
  49.     fullpath = os.path.join(get_cache_dir(), nowstr)
  50.  
  51.     with open(fullpath, 'wb') as fp:
  52.         fp.write(data)
  53.  
  54. def get_current_count():
  55.     cached_file = get_cache_file()
  56.     if cached_file:
  57.         fp = open(cached_file, 'rb')
  58.     elif json:
  59.         fp = urllib2.urlopen('http://www.epiclan.co.uk/epic8/stats?json=1')
  60.     else:
  61.         fp = urllib2.urlopen('http://www.epiclan.co.uk/epic8/stats')
  62.     data = fp.read()
  63.     fp.close()
  64.  
  65.     if not cached_file:
  66.         cache_data(data)
  67.  
  68.     try:
  69.         if json:
  70.             stats = json.loads(data)
  71.             return stats['paid_participants']
  72.         else:
  73.             # Fallback
  74.             for l in data.split('\n'):
  75.                 l2 = l.strip()
  76.                 if l2.startswith('Paid Participants'):
  77.                     total = int(l2.split(': ')[1])
  78.                     return total
  79.     except:
  80.         pass
  81.  
  82.     return None
  83.  
  84. def main():
  85.     guesses = {
  86.         'Elsie':247,
  87.         'Riot':255,
  88.         'BA_RedPlague':300,
  89.         'Haden':287,
  90.         'Booti':261,
  91.         'Inferno':240,
  92.         'Bez':261,
  93.         'GotenXiao':256,
  94.         'MastacheifBOX':289,
  95.         'MRated':220,
  96.         'tommyV':274,
  97.         'daftchip':301,
  98.         'Jay':250,
  99.         'GraemeKustom':9001,
  100.         }
  101.  
  102.     guess_distance = []
  103.  
  104.     current = get_current_count()
  105.     if current is None:
  106.         print('Whoops, failed.')
  107.         exit(-1)
  108.  
  109.     print('Current paid participants: %d' % current)
  110.  
  111.     low_diff = None
  112.     high_diff = None
  113.     for nick, guess in guesses.items():
  114.         guess_distance.append((nick, abs(guess - current)))
  115.  
  116.     best = None
  117.     best_nick = None
  118.     keyfunc = lambda o: o[1]
  119.     guess_distance = sorted(guess_distance, key=keyfunc)
  120.  
  121.     print('Guess distances:')
  122.     for dist, items in itertools.groupby(guess_distance, keyfunc):
  123.         nicks = [i[0] for i in items]
  124.         if best is None or dist < best:
  125.             best = dist
  126.             best_nicks = nicks
  127.         print('%d\t%s' % (dist, ' '.join(nicks)))
  128.  
  129.     print('Closest guess: %s' % (' '.join(best_nicks)))
  130.  
  131. if __name__ == '__main__':
  132.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement