Skip to content

Updated HTML and Python code for Tweetable Office Door Sign

See the original post for the basic information. This post exists only to highlight a few bug fixes.

Since running with my sign for a few days, the main bug I’ve run into is that Twitter doesn’t let you post identical status messages over a short period of time. One recommended fix is to append some sort of unique data to each message. So I’ve decided to

  1. Convince my browser to append a millisecond time value to each message, and enclose the value in curly braces. So a status message of “In office” becomes “In office {339}”, for example.
  2. Modify the Python script to strip out the millisecond value so it doesn’t show up on the door sign. We’ll use a regular expression to filter out any pair of curly braces surrounding 1-3 digits.

Updated code is below the break.

Page of Twitter Status Bookmarks

Links are now of the form

<a href="javascript:void(0)" onclick="gotostatuspage('In office'); return false">In office</a>

and the gotostatusurl() Javascript function is added to the page header

<script type="text/javascript">
  function gotostatuspage(message) {
  // Navigate to the status page, append a pseudo-random number to
  // trick Twitter into letting us post the same status message
  // repeatedly. The feed filter will edit out the added number.
  var d = new Date();
  var ms = d.getMilliseconds();
  var urlvar = "http://twitter.com/?status="+message+" {"+ms+"}";
  window.location.assign(urlvar);
  // Refs:
  // http://stackoverflow.com/questions/1691781/i-need-to-build-my-url-in-a-javascript-function-then-return-the-string-back-to-hr
  // http://www.w3schools.com/jsref/met_loc_assign.asp
  }
</script>

Python script

#!/usr/bin/python

# Grabs most recent tweet from RSS and reformats it for Chumby RSS
# Reader app

# Final Chumby display format:
#
# - feed_title
# - item_title
# - item_description (with Twitter ID removed, and appending timestamp
#   in local time)

# Credits:
# http://stackoverflow.com/questions/1766823/how-can-i-generate-rss-with-arbitrary-tags-and-enclosures

import feedparser, PyRSS2Gen, string
from datetime import datetime, timedelta
from pytz import timezone
import pytz
import re
twitter_id = "REPLACEME"
feed_title = "Mike Renfro <renfro@tntech.edu>"
item_title = "Where's Mike?"
tz = timezone('US/Central')
timestamp_format = ' (%A, %B %-d, %-I:%M %p)'

try:
    parsed_feed = feedparser.parse("http://twitter.com/statuses/user_timeline/%s.rss" % (twitter_id))
    items = [
        PyRSS2Gen.RSSItem(
        title = item_title,
        link = "",
        description = re.sub(r' {[0-9]{1,3}}',r'',\
                             string.replace(x.summary,"%s: " % (twitter_id),"")+\
                             datetime(x.modified_parsed[0],
                                      x.modified_parsed[1],
                                      x.modified_parsed[2],
                                      x.modified_parsed[3],
                                      x.modified_parsed[4],
                                      x.modified_parsed[5],
                                      tzinfo=pytz.utc).astimezone(tz).strftime(timestamp_format)),
                             guid = "")
        for x in parsed_feed.entries[:1] ]

except:
    # Occasionally, the feed will fail, and be fine the next time I
    # check it. Since this isn't critical information, I'll just not
    # write any output at all.
    pass

else:

    try:
        rss = PyRSS2Gen.RSS2(
            title = feed_title,
            link = "", # parsed_feed.feed.link,
            description = parsed_feed.feed.description,
            items = items[:1]
            )

    except:
        # Occasionally, the feed will fail, and be fine the next time
        # I check it. Since this isn't critical information, I'll just
        # not write any output at all.
        pass

    else:
        f = open('REPLACEME.rss','w')
        f.write(rss.to_xml())
        f.close()
<script type=”text/javascript”>
function gotostatuspage(message) {
// Navigate to the status page, append a pseudo-random number to
// trick Twitter into letting us post the same status message
// repeatedly. The feed filter will edit out the added number.
var d = new Date();
var ms = d.getMilliseconds();
var urlvar = “http://twitter.com/?status=”+message+” {“+ms+”}”;
window.location.assign(urlvar);
// Refs:
// http://stackoverflow.com/questions/1691781/i-need-to-build-my-url-in-a-javascript-function-then-return-the-string-back-to-hr
// http://www.w3schools.com/jsref/met_loc_assign.asp
}
</script>

Post a Comment

Your email is never published nor shared. Required fields are marked *