Skip to content

Instantly share code, notes, and snippets.

@dangayle
Created October 21, 2014 23:38
Show Gist options
  • Save dangayle/e82e325731e13fdf1a81 to your computer and use it in GitHub Desktop.
Save dangayle/e82e325731e13fdf1a81 to your computer and use it in GitHub Desktop.
import datetime
import random
import Twython
from twisted.internet import task
from twisted.internet import reactor
TIMEOUT = datetime.timedelta(hours=1).seconds
twitter = Twython("YOUR API KEY",
"YOUR API SECRET",
"YOUR ACCESS TOKEN",
"YOUR ACCESS TOKEN SECRET")
items = [
"To borrow Money on the credit of the United States;",
"To constitute Tribunals inferior to the supreme Court;",
"To provide and maintain a Navy;",
]
def reservoir(iterator):
"""Select item from iterator.
Reservoir algorithm from
http://stackoverflow.com/a/3540315/250241/
"""
line = next(iterator)
for num, item in enumerate(iterator):
if random.randrange(num + 2):
continue
line = item
return line
def tweet():
"""Update Twitter status."""
status = reservoir(iter(items))
twitter.update_status(status=status)
if __name__ == '__main__':
loop = task.LoopingCall(tweet)
loop.start(TIMEOUT)
reactor.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment