So I wrote a bot for Reddit…
My python has been a little rusty, so I decided to fill a small niche that I found on Reddit. It started off from spending way to much time on /r/minecraft, a subreddit I help moderate. Every time someone from Mojang would make a tweet, someone would post it to the subreddit. Another user and I had an informal competition of who could post the content of the tweet in the comments first. This had a few useful purposes for the community and served for a friendly pissing-contest between a few of us.
Enter tweet_poster. It has evolved a bit since the beginning where it would just post the tweet in the comments (well, technically, it never *just* did that, but I digress); it now has a small feature-set that is stretched about as far as I can go for a bot that fits as small of a niche that it does. From it’s wiki, it:
- Finds all context for any given tweet, not just the tweet being replied to like twitter shows [ie: conversation threads].
- Unshortens most url shorteners [now with more recursion!].
- Turns @replies into links to the @user’s profile.
- Turns #tags in to links to a search for that tag.
- For user profile submissions, a comment-ified version will be posted along with the last tweet/conversation by the user.
- Tries to ignore submissions that have the tweet’s text as the title.
- Includes a big endian formatted timestamp for each tweet.
- Includes a [Translate] button for easily translating tweets.
- Tries to rehost images linked in tweets to imgur.
The first feature is one that has been there from the beginning and its my favorite. When you view a tweet that is in reply to someone on twitter you only see the tweet that it’s in reply to. With t_p, you’ll see the entire conversation for as long as it went. The next feature is another favorite and one that took a little work to get right (I am rusty at python, after-all). I use a web service to unshorten urls, but not just that, it does it recursively (I hate url shorteners being where they’re not needed!). The next two features (the linking of @users and #tags) is just replication of what twitter already does and weren’t really hard to implement.
Next up on the feature list is a somewhat controversial and misunderstood feature: comment-ified profile pages. Judging by the votes these comments receive, it’s not a favorite feature, but appreciated in some submissions. I think the main problem (and where it’s misunderstood) is that people still don’t entirely know how to create a direct link to the tweet they’re trying to share. I’ve gotten a few comments in t_p’s inbox stating that things were wrong and that it linked the wrong tweet, when in fact, the bot did exactly what it was supposed to. Oh well, I still get people that confuse its comment footer with a claim that I’m stating that they’re a bot (it says: [This comment was posted by a bot][FAQ][Did I get it wrong?]). You can’t expect everyone to be sharp on their toes.
I’m trying to figure out better heuristics for my ‘is the tweet in the submission title’ detection, but the current version works well enough. All I do is strip all the non-word characters, make everything lowercase, and do a check if the tweet’s text is in the title. Simple, but mostly effective.
The big endian dates aren’t anything special, though there is a small story behind the wording of that line in the feature list: I had originally called it ISO 8601 formatted (which it *is*), but some user messaged t_p and argued that I was wrong in calling it that. They wouldn’t give up, so I told them fine, I’ll just call it big endian (which is also correct). I’m a internet pendant, but there was nothing worth arguing in that one. The translate button was a small feature that I didn’t think I’d be able to add. I had originally dismissed it as I had read blog posts that the Google translate API was being shutdown (which it is in Dec.). T_p gets a lot of attention in /r/starcraft and in fact they give me the most support there. Every now and then, they get tweets that are posted in Korean (I wonder why? :) and a translate button was asked for a few times. Since /r/starcraft has been such a supportive community, I decided to give it a look and found a super simple solution: just throw the tweet’s text in a big-ass url and send it to Google translate.
Last on the feature list is the newest and probably the most involved feature. Twittpic and yfrog were small challenges to build a regex to yank the direct links to pictures. After I got those, I had to start work on interfacing with imgur’s API. There was a small quirk I had to work-around with httplib2, but it works and I don’t want to deal with it right now. The real problem I want to solve in that area is finding a way to yank images off of twitter’s newish picture hosting service. If anyone knows a way to do that, let me know :).
There were a few internal, mini-accomplishments that I figured out (a rewrite, adding a database, and learning some small tricks were among them), but those aren’t entirely worth writing about. I might try to write about them if I ever get around to rewriting them all.
Anyways, I just wanted to make a smallish write-up on my reddit bot. It makes more karma on reddit in a month than I do in a year, and I’m rather proud of it.
I’ll end this post with a picture of bot’s console just after I learned about terminal color codes (it’s a little different now):
color all the things!