Twitter infrastructure

by ManniAT 5. April 2010 21:34

Technorati-Tags: ,

This series is about a promotion game we built to support our iPhone apps.
You can play the game here – it is “early stage” but you can really win!

I already told about the background of this series in my first post. And I handled some parts of the infrastructure in the second part.

Now lets focus on the game itself. Again – the primary goal is to spread information about iPhone software.
And we decided to use twitter as carrier for this.

I already played around with twitter before. http://manniat.pp-p.net/TwitterSL/Default.aspx

But this was nothing more than a proof of concept – I didn’t even put content on that page.
Anyhow – the first thing I did was to take a look a the twitter guidelines for “competition games” – yes there is a guideline for this.

Anyhow – I found that what I need was not covered by my previous work.
So I looked around a bit and stumbled into linq2twitter by Joe Mayo.

First of all I love Linq – next it looks as if this project is under active development (some others I found seem more or less to be “frozen”).
Anyhow – I gave it a try – and my first thoughts – there is something missing.

OK – I found something on how to send a direct message – but it looked “a bit strange”:

Snippet created with CBEnhancer
var message = twitterCtx.NewDirectMessage("16761255", "Direct Message Test - " + DateTime.Now);

But (reading the manual sometimes helps) it turned out that almost everything is available using a Linq query.

Snippet created with CBEnhancer
from A in m_twcContext.Status where A.Type == StatusType.Mentions 
&& A.Count == nSearchCount orderby A.StatusID descending select A

This query for an example searches for tweets where your account is mentioned. Since I want to get only a certain number of entries I say A.Count==nSearchCount.

m_twcContext is the “main token” which holds you account info and other things. Simply use it to make your queries. The “strange” thing I wrote about before is that you give twitter API parameters as “conditions” in your Linq statement.

But – after some experience with it you will learn the same as I did: that’s the correct way to do this.
And by the way – sometimes I was confused by my own code. The reason? I had to look twice to find out if I query my SQL Server or twitter Happy

So once again thank you Joe Mayo for sharing this great work.

And last not least a snippet which shows how easy it is to get a list of Mentions.
For those not familiar with the twitter API – you get max 200 entries per call – so if you get exactly 200 entries you have to check if more entries exist. The following code fills a list handling this paging in a simple manner.

Snippet created with CBEnhancer
List<Status> lRet = new List<Status>();
int nSearchCount = 200;
int nPage = 2;    //this is the first number for the second call :)
lTmpErg = (from A in m_twcContext.Status where A.Type == StatusType.Mentions && A.Count == nSearchCount orderby A.StatusID descending select A).ToList();
while(lTmpErg.Count() == nSearchCount) {    //maybe more exists
    lRet.AddRange(lTmpErg);
    lTmpErg = (from A in m_twcContext.Status where A.Type == StatusType.Mentions && A.Count == nSearchCount && A.Page == nPage orderby A.StatusID descending select A).ToList();
    nPage++;
}
if(lTmpErg.Count() > 0) {
    lRet.AddRange(lTmpErg);
}

Sending a direct message is also very simple:

Snippet created with CBEnhancer
public bool SendDirectMessage(string strUser, string strMessage) {
            try {
                if(!m_twAuth.IsAuthorized) {
                    m_twAuth.SignOn();
                }
                m_twcContext.NewDirectMessage(strUser, strMessage);
                return (true);
            }
            catch {
                return (false);
            }
        }

With this I’ll finish the part about the twitter access.
In the next part I’ll talk about the rest of the infrastructure – primarily about WCF and Caching.

Stay tuned
Manfred

Tags:

twitter

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading


Powered by BlogEngine.NET 2.0.0.0