Generating Twitter Summary Cards for your site using Python.

Twitter recently announced support for "Twitter Cards", a simple way to annotate your content so Twitter apps can provide content previews or inline viewing of media from with the Twitter timeline. For example, if I added the following metadata to one of my blog posts:

			
 <!-- start of Twitter Summary Card -->
 <meta name="twitter:card" content="summary">
 <meta name="twitter:title" content="The Wiki is in French...">
 <meta name="twitter:description" content="A few years ago, I was managing a team that, 
               among other things, was responsible for the administration of our internal 
               tools, including the company's wiki. One day, while in a meeting, my phone st...">
 <meta name="twitter:site" content="@igb">
 <meta name="twitter:creator" content="@igb">
 <!-- end of Twitter Summary Card -->	
		
then any tweet that contained a link to that post would (in Twitter apps or sites) would let you see a preview or "summary" of that page in the timeline directly:




This is great, though if you have a large-ish site or blog, it can be a pain in the ass to go back and add content-specific metdata to each article or page. Certain CMS systems may make this easier, but some folks out there (like yours truly) don't use a blogging or publishing platform. So, if you host your own blog or site, here's a hack-y way to auto-generate summary cards for your content. The code can be found on GitHub at https://github.com/igb/Twitter-Card-Generator

Usage:


 usage: TwitterSummaryCardGenerator.py [-h] [-t TWITTERID]
                                      (-f FILE | -r RSSFEED) [-d OUTPUTDIR]

 optional arguments:
  -h, --help            show this help message and exit
  -t TWITTERID, --twitterid TWITTERID
                        your Twitter user name, without the '@'
  -f FILE, --file FILE  local file to summarize and add card data to
  -r RSSFEED, --rssfeed RSSFEED
                        url of RSS feed to download and summarize
  -d OUTPUTDIR, --outputdir OUTPUTDIR
                        output directory for to write file(s) with summary


Generating Summary Cards from RSS

Retrieves the provided RSS XML, extracts the link and title for each entry in the feed, then downloads the linked document, generates a summary from the first 200 non-markup characters of the body, and then inserts a card summary into the head of the document, writing each document out in the provided directory.

Example:
		
    > 	python2.7  src/python/TwitterSummaryCardGenerator.py -t igb -r "http://www.hccp.org/rss.xml"  -d /tmp/
	

Generating a Summary Card from a local file.

Reads the local file, generates a summary from the first 200 non-markup characters of the body, and then inserts a card summary into the head of the document, writing a document out (with the same name as the source) in the provided directory.

Example:


    >   python2.7  src/python/TwitterSummaryCardGenerator.py -t igb -f ../hccp/blog.html  -d /tmp/
		

Disclaimer: at the time I wrote this post, I was employed by Twitter. To be clear, the information and opinions here are mine and mine alone. In no way do they reflect the opinions, direction, etc. of Twitter. Cheers!