Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter refresh page via API

I'm using the Twitter API (via TweetSharp) and was wondering if it's possible to automatically refresh the page from the API so that all users see the update? If so, is it also possible to take it one step further by only have a partial page update so only the relevant change is updated instead of the entire page?

Thanks for any help

like image 462
XSL Avatar asked Mar 12 '10 22:03

XSL


1 Answers

I think I understand your question, that you want to display a bunch of users and their last tweet - but keep checking if their last tweet has changed and update the screen when the user posts a tweet?

If so the answer is that you need to call the twitterapi asynchronously every so often and pull down the last status (tweet) for each user - and if it is a new one then use ajax to update the part of the screen with the old status (tweet) in it.

In TweetSharp if you have a List of friends, you can pull in their last tweet with something like:

    string profileImageUrl = String.Empty;
    string name = String.Empty;
    string statusText = String.Empty;
    string createdAt = String.Empty;
    string screenName = String.Empty;

    foreach (TwitterUser friend in friends)
    {
        try
        {
            profileImageUrl = String.IsNullOrEmpty(friend.ProfileImageUrl) ? "" : friend.ProfileImageUrl;
            name = String.IsNullOrEmpty(friend.Name) ? "" : friend.Name;
            statusText = (friend.Status == null || friend.Status.Text.Length == 0) ? "unknown" : friend.Status.Text; //stops nullreferenceexception on instance
            createdAt = String.IsNullOrEmpty(friend.CreatedDate.ToString()) ? "" : friend.CreatedDate.ToString();
            screenName = String.IsNullOrEmpty(friend.ScreenName) ? "" : friend.ScreenName;
        }
        catch (NullReferenceException)
        {
            profileImageUrl = "";
            name = "unknown";
            statusText = "unknown";
            createdAt = "";
            screenName = "unknown";

So you can display it on the screen initially.

Then use jquery (or javascript) to periodically hit a web service that reads the twitter api and then use the data returned to update the last tweet if it has changed.

Let me know if I have the wrong end of the stick.

EDIT:

An example of using Tweetsharp posting a new tweet to Twitter is:

var query = FluentTwitter.CreateRequest().AuthenticateAs(username,password).Statuses().Update("Posting status on StackOverflow").AsJson();
like image 179
amelvin Avatar answered Oct 19 '22 21:10

amelvin