Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- from __future__ import with_statement
- import sys
- import urllib2
- import os
- import datetime
- import tempfile
- import itertools
- try:
- import json
- except ImportError:
- try:
- import simplejson as json
- except ImportError:
- json = None
- _DATE_FMT = '%Y%m%d-%H%M%S'
- _CACHE_TIME = 600
- def get_cache_dir():
- return os.path.join(tempfile.gettempdir(), 'epiclan')
- def get_cache_file():
- cache_dir = get_cache_dir()
- if not os.access(cache_dir, os.F_OK):
- os.mkdir(cache_dir)
- return False
- else:
- files = os.listdir(cache_dir)
- if len(files) == 0:
- return False
- now = datetime.datetime.utcnow()
- cache_expire = now - datetime.timedelta(seconds=_CACHE_TIME)
- for f in files:
- d = datetime.datetime.strptime(f, _DATE_FMT)
- fullpath = os.path.join(cache_dir, f)
- if cache_expire > d:
- os.unlink(fullpath)
- fullpath = False
- return fullpath
- def cache_data(data):
- now = datetime.datetime.utcnow()
- nowstr = now.strftime(_DATE_FMT)
- fullpath = os.path.join(get_cache_dir(), nowstr)
- with open(fullpath, 'wb') as fp:
- fp.write(data)
- def get_current_count():
- cached_file = get_cache_file()
- if cached_file:
- fp = open(cached_file, 'rb')
- elif json:
- fp = urllib2.urlopen('http://www.epiclan.co.uk/epic8/stats?json=1')
- else:
- fp = urllib2.urlopen('http://www.epiclan.co.uk/epic8/stats')
- data = fp.read()
- fp.close()
- if not cached_file:
- cache_data(data)
- try:
- if json:
- stats = json.loads(data)
- return stats['paid_participants']
- else:
- # Fallback
- for l in data.split('\n'):
- l2 = l.strip()
- if l2.startswith('Paid Participants'):
- total = int(l2.split(': ')[1])
- return total
- except:
- pass
- return None
- def main():
- guesses = {
- 'Elsie':247,
- 'Riot':255,
- 'BA_RedPlague':300,
- 'Haden':287,
- 'Booti':261,
- 'Inferno':240,
- 'Bez':261,
- 'GotenXiao':256,
- 'MastacheifBOX':289,
- 'MRated':220,
- 'tommyV':274,
- 'daftchip':301,
- 'Jay':250,
- 'GraemeKustom':9001,
- }
- guess_distance = []
- current = get_current_count()
- if current is None:
- print('Whoops, failed.')
- exit(-1)
- print('Current paid participants: %d' % current)
- low_diff = None
- high_diff = None
- for nick, guess in guesses.items():
- guess_distance.append((nick, abs(guess - current)))
- best = None
- best_nick = None
- keyfunc = lambda o: o[1]
- guess_distance = sorted(guess_distance, key=keyfunc)
- print('Guess distances:')
- for dist, items in itertools.groupby(guess_distance, keyfunc):
- nicks = [i[0] for i in items]
- if best is None or dist < best:
- best = dist
- best_nicks = nicks
- print('%d\t%s' % (dist, ' '.join(nicks)))
- print('Closest guess: %s' % (' '.join(best_nicks)))
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement