Skip to content

Instantly share code, notes, and snippets.

Created December 16, 2012 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4307323 to your computer and use it in GitHub Desktop.
Save anonymous/4307323 to your computer and use it in GitHub Desktop.
The BlackBerry Choser.
#!/usr/bin/env python3
# This is the script to find the owner of the blackberry.
# logging
from logging import getLogger, StreamHandler, Formatter, DEBUG, INFO
# urllib
from urllib.request import urlopen
# re
from re import compile as reCompile
# collections
from collections import Counter
# random
from random import randint, choice
# hashlib
from hashlib import *
class BlackBerry :
url = 'http://www.v2ex.com/t/55009'
blacklist = ['thedevil7', 'OLIVIDA', 'zz', 'gongweixin']
member = reCompile(r'member\W(\w+)')
def __init__(self, DEBUG=False) :
self.getLogger()
self.getUsernames()
self.counter = Counter()
self.hashPoints()
self.choicePoints()
self.whoIsTheWinner()
def getLogger(self, DEBUG=False) :
logger = getLogger('BlackBerry')
logger.setLevel(DEBUG if DEBUG else INFO)
consoleHandler = StreamHandler()
consoleHandler.setLevel(DEBUG)
formatter = Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
consoleHandler.setFormatter(formatter)
logger.addHandler(consoleHandler)
self.info = logger.info
self.debug = logger.debug
def getUsernames(self) :
response = urlopen(self.url)
content = response.read()
decoded = content.decode()
usernames = set( self.member.findall(decoded) )
self.info('Found usernames : {}.'.format(usernames))
for username in self.blacklist :
try :
usernames.remove(username)
self.info('Username {} removed.'.format(username))
except :
msg = 'Username : {} NOT found in the page.'.format(username)
self.debug(msg)
self.info('Final Usernames : {}.'.format(usernames))
self.usernames = list(usernames) # random.choice does not support set.
def getRandomNumber(self, magicNumber=1000) :
counter = Counter()
for i in range(1, magicNumber) :
counter[randint(0, 7)] += 1
(result, count) = counter.most_common(1)[0]
result = str(result)
msg = 'Random number generated : {} - {}.'.format(result, count)
self.debug(msg)
return result
def hashPoints(self, magicNumber=1) :
counter = Counter()
usernames = self.usernames
availabelAlgorithms = algorithms_available
self.debug('Availabel algorithms : {}.'.format(availabelAlgorithms))
for algorithm in availabelAlgorithms :
for username in usernames :
newHash = new(algorithm, username.encode()).hexdigest()
msg = 'Username : {} algorithm : {}.'.format(username, algorithm)
self.debug(msg)
self.debug('result: {}.'.format(newHash))
for i in range(0, magicNumber) :
points = 1 * newHash.count(self.getRandomNumber())
counter[username] += points
msg = 'Added {} points for {}.'.format(points, username)
self.debug(msg)
self.info('Counter for hashPoints : {}.'.format(counter))
self.counter.update(counter)
def choicePoints(self, magicNumber=500) :
counter = Counter()
usernames = self.usernames
for i in range(0, magicNumber) :
username = choice(usernames)
counter[username] += 3
self.debug('Added 5 points for {}.'.format(username))
self.info('Counter for choicePoints : {}.'.format(counter))
self.counter.update(counter)
def whoIsTheWinner(self) :
(winner, points) = self.counter.most_common(1)[0]
msg = 'The Winner is {} with {} points!'.format(winner, points)
self.info(msg)
return winner
if __name__ == '__main__' :
blackBerry = BlackBerry()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment