Conky + Twitter

I finally decided to get a Twitter account.  The first thing I wanted to do was interface it with my Conky script.  The Twitter API was a pain to use and there wasn’t much documentation on how to use it properly.  So I did some trickery and created a work around.

I found a command line interface for twitter called Twidge that was easy to set up and could easily read my Twitter friend’s feed and output it to the console.  This allowed me to pipe the output to a text file and then use a custom Python script to format the text the way I liked it.  The two lines that needed to be added to Conky are as follows:

${execi 1200 twidge lsrecent >  /home/commander/scripts/tweets.txt}
${execi 1200 python /home/commander/scripts/tw.py /home/commander/scripts/tweets.txt}

The following is the tw.py python code that reads in the text file generated by Twidge and then reformats it to my choosing.  This code can be ignored if you just want to use the standard output that Twidge uses.  In which case the first line in the Conky script should not pipe the output to the text file.

#!/usr/bin/python
#filename: tw.py
import re
import sys
from time import strftime

def format(text, indent=2, width=70):
    """
    Format a text block.

    This function formats a block of text. The text is broken into
    tokens. (Whitespace is NOT preserved.) The tokens are reassembled
    at the specified level of indentation and line width.  A string is
    returned.

    Arguments:
        `text`   -- the string to be reformatted.
        `indent` -- the integer number of spaces to indent by.
        `width`  -- the maximum width of formatted text (including indent).
    """
    width = width - indent
    out = []
    stack = [word for word in text.replace("\n", " ").split(" ") if word]
    while stack:
        line = ""
        while stack:
            if len(line) + len(" " + stack[0]) > width: break
            if line: line += " "
            line += stack.pop(0)
        out.append(" "*indent + line)
    return "\n".join(out)

if len(sys.argv) != 2:
    print "Error: No input file"
    exit()

f=open(sys.argv[1], 'r')

df = f.read().split("\n")
strin = ""
name = ""
names = []
tweets = []
for l in df:
    match = re.search( "<(\w*)> *([\w\W]*)", l)
    if match:
         names.append(name)
         tweets.append(strin)
         name = match.group(1)
         strin = match.group(2)

    else:
         strin = strin + " " + l.strip()

# All the names are stored in an array called names, and the tweets in an array called tweets
# You cay customize the output the way you like. This method was the best for me

print "                                        TWITTER FEED"
print "                                        Last update: " + strftime("%H:%M:%S")
print "                                       ================================================="
for i in range(1,12):
    ff = format( tweets[i], 50, 100 )
    print "                                        " + names[i] + ":"
    print ff
    print ""

 

The final Conky output looks like (blurred for privacy):

2 thoughts on “Conky + Twitter”

  1. hello gavin
    I find it very useful your conky settings, excellent design, and I wonder if you could share all files of conky.

    whatever your answer on this favor I ask, I congratulate you for interest in innovation.

    1. Hello Paco,

      Sorry I did not see this comment until now. I found a conky script that I liked and then heavily edited it to suit my needs. This is the original script I used which gave me access to all the circular meters

      http://www.omgubuntu.co.uk/2011/01/this-minimal-conky-orange-would-look-great-on-any-desktop

      My edited version is below:

      http://www.rabidgeek.net/wp-content/uploads/2012/07/conky-orange.zip

      You will have to change line 50 on the conky script to point to the location where you stored the .lua file.

Comments are closed.