Thursday, January 29, 2009

Automated Twitter Politeness in Python

Today's post is a mixed bag; I think you'll enjoy it. It combines a discussion of Encouraging Retweeting, with a report on some of my experience, and an example Python/Twitter Automation (automating politeness!) and a little bit of a treasure hunt with prizes!

Part 1: Encouraging Retweeting
If you've seen this before, skip down to part 2!

The idea behind encouraging Retweeting is that you are trying to encourage your followers to re-post your tweet to their followers (perhaps to drive traffic to your blog). You can make this very simple for your readers, simply by providing a link, as I have below. This is a real link! Give it a try!

Click here to Retweet Please!

In case you didn't try the link, here's an explanation: the link will open the visitor's Twitter home page and pre-load the status update field with the Retweet. It doesn't automatically update the visitor's status, but it simplifies the process tremendously. (So you can click it, and see how it works, without committing to the Retweet.)

Here are my steps for encouraging a Retweet.

1. Open a free account at I like Tweetburner better than Tinyurl because you can track clicks (if you have an account!). For example, my last blog post got Retweeted more than 30 times, and I could see that it generated almost 500 hits in 50 hours. Those were pretty decent numbers for a crappy blogpost from a no-name blogger from the middle of nowhere, who is just learning Twitter! Frequent readers will recognize that I like to report my findings of this grand experiment and recognize that this is simply another report of my social experiments stats, and it's not intended to be bragging in any way!

2. Perform a couple of setup steps: Make sure you're signed into Tweetburner. (Can you tell, I've been burned by this?) Also write a ReTweet teaser headline. Something like "RT @amyiris Being automatically polite on Twitter using Python". This is the text that OTHER people will send out as their tweets. Don't start this with an @ - some twitter users have those messages turned off. Start it with RT.

3. Open another browser tab or window, and in your browser address field, paste the following:

and then at the end of that, paste in your headline that you created. Don't press enter yet, or try go to the page. Just use the browser address field as a copy-and-paste holding area.

4. Clean up the URL, by changing all the spaces to %20 and all the "at" signs (@) to %40. Hashtags (#) become %23. So your address bar should contain something like: on%20Twitter%20using%20Python%20

You'll change that ending "xxxxxx" part in a minute.

5. Create and Publish the Blog Post with placeholders for the Retweet links. My blogpost had this text at the top and bottom:

Click here to Retweet Please!

But when I first published the blogpost, I didn't have the links active, because I don't know the short URL yet. It's sort of a chicken-or-egg problem. I publish my blogpost to obtain the URL, that I am going to shorten. Then I go back and edit the blogpost to put the links in there that contain the short URL to the blogpost itself. That's why I do all the setup steps first - to minimize the amount of time that my blog post is published without links.

6. Copy the URL that will take me straight to the blog post. For me, it's something like Then paste it into Tweetburner. Make sure you are signed into Tweetburner, so you can track it! Then press the "Shorten it!" button. You'll get back a URL like this:

7. Take that URL and modify your "xxxxxx" from step 4 to include the correct Tweetburner link information which you just got in step 6. So now you have a long URL with %20s and the short URL embedded in it. Copy this long URL to your clipboard, and then test it. You should be taken to your Twitter home page with the RT all filled out, and ready to update your status. This is how it will behave for your visitors.

8. Edit your blog post, and find all the places where you have the words:

Click here to Retweet Please!

Now make them links, by pasting the link code that you copied. Doesn't matter to me that this is a long URL, as long as it contains the short URL in the tweet.

Have I mentioned enough times in this blog post that you should
Click here to Retweet Please!

(Credits: This is only a slight variation of Yellow Candy's tip.)

Part 2: Automated Politeness
Stupid Python tricks!

I was inspired by this tweet. (Thanks, @ces614.)

My Thank-You to @ces614 was indeed created by a human - me, the person behind Amy Iris the bot (actually, Amy Iris likes to be called a "Conversational Interface", not a bot). But it's not so far fetched that one could create an automation to thank people for Retweets.

In fact, if you Retweet this for me, Amy Iris (the bot,er um, Conversational Interface) will politely try to send you an automated response (a direct message) thanking you. But wait, there's more! Just to make it interesting, she's going to give you a clue to a treasure hunt. (Make sure you are "following amyiris" before you Retweet, or you won't get the direct message - if I understand how Twitter works.) [Edit 2/2/2009: I have now turned off the monitor for RT's of this article. I haven't gotten any RT's on this for a few days. You are welcome to RT it, or even try to solve the puzzle through creative searching. Just let me know if you solve it, so you can be considered for the prize. I will live up to my agreement on the prizes, it'll just be a little different challenge (probably easier!) to find the answer online and qualify for a prize.]

As I mentioned, there would be prizes. Prizes go to the first people who figure out a secret message that Amy Iris has, and tweet the 140-character message. She'll give you hints at the message, in your Thank You message. I'll give a free T-shirt to the first ten people who I see that Tweet the secret message.

All standard contest rules apply (like "void where prohibited; decision of judges is final; I am not liable for failure of twitter or your computer, or my program" etc.) Hey, it's just for fun, so let's not get all legal!

OK, so one more time... here's how it works:

1. Amy Iris has a Python Program running [Edit: see note above, it's no longer running] (program listing is below, but you don't need to know ANYTHING about Python to participate. Ability to comprehend my program will not likely improve your capability to figure out the secret message.)

2. The Python program is looking for Retweets that link back here.

3. You Retweet for me, by clicking on this:

Click here to Retweet Please!

4. The Python program sends you a Thank-You via direct message, along with a clue to the secret message. [edit: program is turned off.]

5. You figure out the secret message, and post it in your Twitter timeline.

6. The first ten people who I spot posting the secret message into their Twitter timeline win a T-shirt. Please allow sufficient time for me to get some cool T-shirts printed.

Yes, this is a social experiment, but hey, I'm snowed in. May as well have some fun.

Ready to play along:

Click here to Retweet Please!

Here's the Automated Thank You program that is running [edit: was running]:

# This program is an example of an auto Thank You program.
# It scans for ReTweets, and when it finds one, it sends a thank you
# via Direct Message to the user who ReTweeted.
# The Thank-You includes a clue to the secretMessage.
# Built for Python 2.x (2.5 is running here),
# will not work on Python 3.0 without modification.

import twitter as twitterapi
import random, re, time, urllib2

thanked = {}

fil=open ("thanksguys.txt","r")
for t in fil.readlines():
print thanked

mypassword = open("pwd.txt","r").read() #hide it, of course
secretMessage = open("sm.txt","r").read() #you're trying to figure this out
query = ""
restring1 = r'<div class="msg">.*?<a href="'
restring1+= r'(.*?)".*? class="msgtxt.*?">(.*?)</span>'
restring2 = r'<a href="/search.max_id=(.*?)&page=.*?&q=(.*?)">Older</a>'
thankyou = "Thanks for the RT! Here's your clue: The secret message %ss with %s"

assert len(secretMessage)==140 #clue: it's 140 characters!
assert secretMessage.count("*")==0 #no asterisks in it!

api = twitterapi.Api(username='amyiris', password=mypassword)

while True:
for i in range(2,10): #search through up to 8 search result pages

for g in retweeters:
if g[0] not in thanked: #sorry, one per person!
cointoss = random.randrange(2)
codedmsg = secretMessage[cointoss*70:(cointoss+1)*70]
while codedmsg.count("*")<56: #give em 10% of the message
codedmsg = codedmsg[:rnd]+"*"+codedmsg[rnd+1:]
fil=open ("thanksguys.txt","a")

print "sent"



Click here to Retweet Please!


Anonymous said...

In your profile you say that your not so smart; in your posts you call yourself a no-name blogger from the middle of nowhere... Is that a social experiment too?

daonb said...

smart post, maybe too smart.

I'm not sure how polite your robot makes you. I means it's nice to get a thank you note and I wish I'd do it more often, but getting a bot to send it for you is not the real thing. I'd say it's more about "keeping up appearances" then being polite.

I'm not sure if I should thank you for your code, I mean what's next? a polite service where for $5 a month we can all be polite and get a lot of meaningless Thank You messages?

tom said...

Amy -

I've written up a snippet of javascript that automates the process of putting a retweet link on posts, saving the hassle of manually escaping the title and URL. It's also good for those of using automated posting services like Posterous, since we may post from places that we can't access our blog admin pages directly.

tom said...

Sorry, I thought Blogger would auto link that URL. Here it is properly linked:

Amy Iris said...

To Anon:
Yeah, I'm all about the social experiments and reporting the results. And really, Amy Iris is not very smart. She's like a new born - a sponge, absorbing all that she can.

To daonb:
Sure, it's about keeping up appearances of politeness. For years, Emily Post and other experts in etiquette have debated proper ways of saying thank you. Is email inappropriate because you didn't undertake enough inconvenience as you would by hand-writing a thank-you note and sending it via the postal service? Same concept. Automations are debated because they're too convenient! But isn't saying thanks in an an automated way better than not saying thanks at all?

Some people have automatic responses when you follow them in Twitter. I found it interesting that one user's auto response said "(auto) Thanks for following me." Maybe that's the approach, so as not to be rude, when you're trying to be polite; tell them it's "auto".

You may want to check out my previous blog entry on Asymetrical Communications where I cover this topic.

Anyway, it's just code examples that you can use or ignore. Hope there's something of value in there. I know I had fun writing it.

To Tom:
Thanks! Looks real cool!

To everyone else:
Check out Tom's code!

Ching Ya said...

Thanks Amy, actually I'm doing almost the exact same thing as you, although I'm not using feed in this case, but I basically need to manually inserted the tinyurl's in the 'TweetThis' type of widget. I might give Tom's page a browse too. But I would still like to compliment what you've shared with your readers. Keep it up. ^^