Blog

January 9, 2009
Categories:

creating a group twitter repository

For the trip to Silicon Valley I wanted to have everyone be able to twitter about the experience using their own twitter account, but I also want to have a central place for everyone following the trip as a whole to see all of our tweets. How to do that? Python and the Twitter API to the rescue. You can see the results of this by checking out @lutherlive on twitter


import twitter

USER = “Your group account”
PASS = “your group password”
TAG = “tag contained in each message”

api = twitter.Api(username=USER,password=PASS)

# figure out when my last post was
statuses = api.GetUserTimeline(user=USER)
last_post = statuses[0].GetCreatedAt()

# Get the timelines for all friends since my last_post
tl = api.GetFriendsTimeline(user=USER,since=last_post)
for post in tl:
# since my posts may show up in friends timelines avoid reposting loop
if post.user.screen_name != USER and TAG in post.text:
api.PostUpdate(post.user.screen_name + “: “ + post.text)


This little script logs in using the twitter account you create for the group. Importantly, this account must follow all of the group members that you want to be able to re-tweet. When one of the group members wants a tweet to show up on the group account they must use the TAG, in our case @lutherlive, somewhere in their post.

After logging in, the script grabs the timeline for all of the members of the group. This timeline is restricted to the posts since the last a post was made by the group user. This is important to do otherwise you would end up with duplicate re-tweets every time the script is run.

Next the script simply loops over all of the posts and checks for the tag. If the tag is in the post the post is updated along with the screen name of the original group member that made the post.

I put the script into a cron-job on the computer science server at Luther to run every 15 minutes, so while the @lutherlive user isn’t a real time reflection of its group members tweets, its pretty darn close.