Skip to content

Instantly share code, notes, and snippets.

@nailor
Created October 2, 2009 13:25
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 nailor/199732 to your computer and use it in GitHub Desktop.
Save nailor/199732 to your computer and use it in GitHub Desktop.
Thing that retweets for Turku Agile Day
#!/usr/bin/python
# This code is public domain and can be freely used.
#
# This script echoes all replies it has received that are either
# prefixed or suffixed with @username
#
# Requirements:
# Python 2.5+
# python-twitter: http://code.google.com/p/python-twitter/
# simplejson: http://www.undefined.org/python/
#
# Short documentation:
#
# By default, tadtwit searches configuration files in directory
# ~/.tadtwit. The program uses two configuration files, a file named
# *state*, that remembers the tweets already tweeted and config, which
# contains the essential configuration of the program. The
# configuration directory can be overridden by environmental variable
# TADTWIT_DIR.
#
# State file TADTWIT_DIR/state is created by tadtwit. Config file is a
# JSON file in TADTWIT_DIR/config that contains following JSON:
#
# {
# "username": "username",
# "password": "mypassword",
# "users": ["alloweduser1", "alloweduser2"]
# }
#
# "users" is a list of users whose tweets are allowed to be echoed
#
# To run just start the tadtwit.py.
#
# Protip: You might want to put this program to cron
from __future__ import with_statement
import os
import twitter
import simplejson
if 'TADTWIT_DIR' not in os.environ:
tadtwit_dir = os.path.expanduser('~/.tadtwit/')
else:
tadtwit_dir = os.environ['TADTWIT_DIR']
config_file = os.path.join(tadtwit_dir, 'config')
state_file = os.path.join(tadtwit_dir, 'state')
with open(config_file) as f:
config = simplejson.load(f)
allowed_users = config.get('users', [])
if not allowed_users:
print >> sys.stderr, 'No allowed users listed in %s!' % config_file
sys.exit(1)
username = config['username']
api = twitter.Api(username=username, password=config['password'])
if not os.path.exists(state_file):
state = []
else:
with open(state_file) as f:
state = simplejson.load(f)
username_prefix = len('@%s ' % username)
try:
for reply in api.GetReplies():
if (reply.id in state or
reply.user.screen_name == username or
reply.user.screen_name not in config['users'] or
not reply.text.startswith('@%s' % username) and
not reply.text.endswith('@%s' % username)):
if reply.id not in state:
state.append(reply.id)
continue
suffix = ' from @%s' % reply.user.screen_name
if reply.text.endswith('@%s' % username):
msg = reply.text[:-username_prefix]
else:
msg = reply.text[username_prefix:]
if len(msg + suffix) > 140:
msg = msg[:140-len(suffix)] + '...'
state.append(reply.id)
msg = msg + suffix
api.PostUpdate(msg)
except:
raise
finally:
with open(state_file, 'w') as f:
simplejson.dump(state, f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment