Home > Blog > 2011 > 04

All Posts From : April 2011

Using the Twitter for Umbraco Package with the Feedcache Package in Umbraco

I often use Warren's rather excellent Twitter for Umbraco package to display Twitter feeds on websites. To save on hits to the Twitter server (especially on busier sites) I cache the macro, often for about 20 minutes (unless the feed is updated on a more regular basis). This has always worked fine, until recently, when I had an issue on a client site where the feed kept showing up empty.

I'd not come accross this before, so I did some digging and found out that the Twiiter for Umbraco package was getting a 403 error, and therefore no Tweets, and the Macro was caching as empty. After some digging around, I found out that Twitter have started limiting access to the open feeds, to roughly 60 or so requests an hour (this is called Rate Limiting, you can read about it here). This was still a bit odd, as the site should only have been hitting the feed three times an hour. Then I discovered that they had some other apps hitting the feed as well, that was causing us to hit the limit.

The Twitter for Umbraco feed doesn't cache the response from Twitter, so if it can't get a list of Tweets because you've fallen foul of Rate Limiting (as we had), or if the Twitter feed is unavailable (which also happens more than I realised), then you're stuck with no Tweets.

I looked into a solution for this, and came up with the idea of using Darren Ferguson's Feed Cache package to download and cache the feed (as that way if the feed was unavailable we'd still have a cached copy to read). It turned out to be quite simple, with only a few tweaks needed, so I thought I'd cover it here for anyone who's interested!

First, make sure you have the Twitter for Umbraco package installed, and make sure that you have the Macro up and running on a page and that it's pulling out Tweets and displaying them. Full instructons for setting up the feed are on the Twitter for Umbraco project page.

Next, install the Feed Cache package, again, the setup instructions are included on the project page.

Once both are installed, you can get everything set up. First up, you need the URL that your Twitter Macro is using to get the Tweets. This is pretty easy to get. Go into the developer section in the back office, and open the XSLT file called "cws_twitterStatus.xslt". Scroll down to the beginning of the "match" part of the template, and you'll see a variable called "twitterXMLUrl". Use an XSL value of just underneath this variable to get the feed (don't forget to get rid of this once you've got the feed URL!).

<xsl:value-of select="$twitterXMLUrl" />

Armed with the feed URL, you can now open the config file for the feedCache package, which is called "feedcache2.config" and is located in the "config" folder of the site. Below is an example of the file, with a feed set up:

<?xml version="1.0" encoding="utf-8"?>
<Configuration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

Notice that we set a path to the feed to save its XML to (make sure that App Pool identity can read, write and modify to the location you specify, I normally use the "App_Data" folder). We also set a file name for the twitter feed.

Now all we need to do is schedule the feed to run on a regular basis. Open your "umbracoSettings.config" file from the config folder of the website, and scroll down to the scheduled tasks section. You can then add a scheduled task to the section for the feed cache. The interval is in seconds, the example task below will check the feed every 15 minutes. Here is an example of the task:

    <task log="true" alias="feedcache2" interval="1800" url="http://[ENTER YOUR URL HERE]/umbraco/plugins/FergusonMoriyama/FeedCache2/FmFeedCache.aspx" />

You can run the link from the task in a brower to make sure that it's working. If it did, there should be an XML file with the name you specified in the folder you specified. If there isn't, the main things to check are that your Twitter URL is correct, and that the permissions are set properly on the path you specified for the feed to save to!

Once that's done, you just need to make a small change to the "cws_twitterStatus.xslt" file in the developer section. First go back to the section that sets the Twitter URL and comment it out. Replace it with the folowing code:

<xsl:variable    name="twitterXMLUrl"    select="[RELATIVE PATH TO XML FILE HERE, e.g. '../App_Data/FeedCache/mytwitter.xml']"/>

Make sure that you set the path to the file that the feed is saving to correctly! Once that's done, scroll slightly further down the page, and you'll see two for-each loops that actually display the Tweets. They're currently using an umbraco.library call, we need to change that slightly. Change the first for-each to:

<xsl:for-each select="document($twitterXMLUrl)//statuses/status">

and the second for-each loop to:

<xsl:for-each select="document($twitterXMLUrl)//statuses/status [in_reply_to_screen_name = '']">

Save the macro, and you're done! You now have a Twitter feed that's cached, so if the feed is unreachable, your site will still display the tweets!

Upcoming Umbraco Articles

One of the reasons that I started this blog was so that I could post about some of the things I've worked on, but also to share how I've done some stuff. I've had a few requests for more information on some Umbraco related topics recently, so I'll be posting them up over the coming week or so. In case you're interested, the posts will cover:

Welcome to My New Blog!

Welcome to my new technical blog! In this blog I will be posting stuff about Web Development, Umbraco, SQL Server, ASP.Net and other web/technical areas. I have a backlog of requests for posts on some of the Umbraco things I've worked on recently, so I'll be trying to get those up on the site over the coming weeks. I hope you find the stuff that I post interesting/useful!