Reddit Flair Badges Bot
This bot creates and maintains date-based flair (badges) on the website reddit.com. No database required.
- praw (python reddit api wrapper)
So let's say a user quit doing something, like drinking caffeine, on a certain day. And let's say that they want reddit flair (a "badge") commemorating this oh-so-special occassion. If they want the flair to say "X days", you're gonna need a bot to log in at 12:01 am each morning and increment everyone's flair by one day. That's what this bot does. It also watches its inbox for PM's with the subject line "Badge" and creates new flair or updates existing flair for the requesting user.
This bot works without an external database. It accomplishes this by stashing the user's start date in the flair css class field. A sample entry looks like this: "year1 startdate-20111122". The browser applies the 'year1' class, but ignores the startdate-YYYYMMDD class. That's the date that the bot works off of. So we're essentially using the reddit css field as our database.
Advantages of this approach: No single point of failure. No need to worry about the third-party database disappearing. If one bot disappears, someone can simply start up another one & it'll pick up where the last one left off. In fact, if you wanted to, you could have multiple people running this bot on the same subreddit, one as the "primary" and one as a "backup." For example, bot A would be scheduled to run at 12:01 am, and bot B would be scheduled to run 1:00 am. As long as bot A kept running, bot B would check the data & see that it had already been updated & do nothing. But if Bot A failed to run for whatever reason, bot B would then make the necessary flair updates.
I did a test using ~3500 flair entries, the update process for 3500 entries took approximately 2 minutes.
This bot was inspired by the original badgebot, created by reddit user sodypop. He graciously allowed me to include the badge images and css in this package.
- Copy redditbadges.cfg.sample to redditbadges.cfg and fill in the info
- Add the styles in reddit_stylesheet.css to your subreddit's stylesheet and upload sprites.png
- Run redditbadges from the command line specifying one of two options, either "new" or "all". "new" checks the bot user's inbox for badge requests. Badge requests should have the subject line "Badge". You can configure a link from your sidebar that pre-populates this information, see /r/decaf for an example. "all" does the daily rotation and should be run once per day.
- Set up a cron job to run the bot every so often. See the included sample cron file for an example.
- PS this works by only grabbing the unread messages, processing them, then marking them as "read." So if you check your bot's PM messages via reddit, they'll get marked as "read" & won't be processed. So don't do that.
- testing :)
- add logging option
- error handling
- remove (delete) flair on user request.
- option to forward all non matching messages (that is, non-badge requests) to another account
- admin option to control the bot via PM with password. (e.g. tell the bot to add an entry for a user.) this will be useful if you check your bot's pm's via the reddit interface & they get marked as seen. actually, maybe the bot shouldn't check only unread messages, maybe it should always process the last X messages. that would require pulling a full flair list every time though, or at least updating & re-updating the same record again & again. i suppose a small local db would solve this problem. meh. i dunno if it's really necessary.