Twitter is my randomness entropy

Inspired by Henry’s Facebook comment.

import urllib2, hashlib

TWITTER = 'http://twitter.com/statuses/public_timeline.json'

def hashed(seq):
    sha1 = hashlib.sha1()
    for x in seq:
        sha1.update(x)
        yield sha1.digest()

def words(wordsize, seq):
    for x in seq:
        for i in xrange(0, len(x), wordsize):
            yield int(x[i:(wordsize + i)].encode('hex'), 16)

def twitrandoms(wordsize = 4, chunksize = 37):
    def infinite_twitrandom():
        while True:
            data = urllib2.urlopen(TWITTER).read()
            for i in range(0, len(data), chunksize):
                yield data[i:(chunksize + i)]
    return words(wordsize, hashed(infinite_twitrandom()))

Posted in , | | Leave a response

Leave a Reply