Autotweeting update

Back near the end of August, I set up a special Twitter account, drangposts, from which to tweet notices of new blog posts. And to make the tweeting semi-automatic, I wrote a little script that connected to the blog through the WordPress API, grabbed the title and URL of the most recent post, and then tweeted the notice through that special account. This was a fun exercise, but like much of what I post here, it needed some tweaking.

First, the drangposts account didn’t collect enough followers to make it worthwhile. I could have promoted it more on my regular account, but since my purpose in setting it up was to avoid doing a lot of promotion on my regular account, that seemed stupid. So I’ve stopped tweeting to drangposts and will shut it down in the near future. I appreciate the 31 of you who took part in the experiment, and I apologize to the three of you who followed drangposts but don’t follow my regular account.

I suppose I should also apologize to the followers of my regular Twitter account who don’t like seeing tweets for articles they’re already getting through RSS, because I’m going to switch those leancrew link tweets over to the drdrang account. I promise not to do any repetitive “in case you missed it” tweets, and you should be able to filter out the links if you want.

Second, I’m switching from semi-automatic tweeting to fully automatic tweeting by incorporating the code that sends a tweet into my post-from-BBEdit script. From now on, when I publish a new blog post, a function named tweetlink will be called, and it’ll issue the tweet without any other work on my part.

Rather than recapitulate the entire script, I’ll just post the code to tweetlink itself:

58:  def tweetlink(text, url):
59:    "Tweet a link to the given URL. Return the URL to the tweet."
61:    # Authorize Twitter and establish a connection.
62:    auth = tweepy.OAuthHandler('consumer_key',
63:             'consumer_secret')
64:    auth.set_access_token('access_token',
65:             'access_token_secret')
66:    api = tweepy.API(auth)
68:    # How long will the shortened URL to the post be?
69:    short_length = api.configuration()['short_url_length']
71:    # Construct the tweet.
72:    max_text = 140 - short_length - 3
73:    if len(text) > max_text:
74:      text = u'⛄ ' + text[:max_text-1] + u'…'
75:    else:
76:      text = u'⛄ ' + text
77:    tweet = '''{}
78:    {}'''.format(text.encode('utf-8'), url)
80:    # Send the tweet.
81:    out = api.update_status(tweet)
82:    return '' % out.id_str

The script depends on my fork of the tweepy library, although I’m pretty sure my additions aren’t required. And, as you can see in Line 74, there are non-ASCII characters in the source, so an encoding line

# -*- coding: utf-8 -*-

is needed just under the shebang line.

To run a function like this, you need to get the various keys and secrets used to access the API in Lines 62–65. You do this by registering an application with Twitter, which is a relatively painless process. I know it’s de rigueur to hate Twitter for the restrictions it put on its API, but it’s still pretty easy to use for little toy projects like this.

You see in Line 74 the Emoji snowman,⛄, that starts the tweet. This is your cue that the tweet will be a link to here. (It’s also what you can use to filter out those tweets, if you’re so inclined.) I was torn between him and the less colorful snowman, ☃, from the Miscellaneous Symbols block. ☃ has a longer history than ⛄, which I respect, but ⛄ stands out more. In the end, flash beat out tradition.

I’ll probably continue to tweak this—there really should be some error handling in there somewhere—but I won’t ask you to read about it anymore.