|
|
by Naveen
21. June 2010 13:28
Download Sample Project
In my last post Using OAuth Authentication For Twitter Applications, I showed it
with the help of a console application how you can use OAuth for twitter applications. Your
real world applications are not going to be console applications. You are developing either desktop (windows)
or Web applications. In this post I will show how you can use OAuth authentication in
desktop or windows applications.
The fundamentals for using OAuth in console application or windows application remain the same. The steps
involved are as follows:
- Get a request token from twitter authorization site.
- Send a request to twitter site for authorization.
- Use the PIN supplied from above step to get access token from twitter authorization site
- Save the access token from above step for later use.
Step #4 is something that is very anoying. In console application, default browser was launched to allow
user to provide their twitter login information and then get authorization pin. And then manually enter
that PIN. You will end up doing the same for windows application as well.
But there is a solution available that extracts this authorization PIN from the page and then automatically
get the access token. Let's see how this happens.
WebBrowser Control
In stead of letting your desktop or windows application launch default browser, make use of handy
WebBrowser control. Create a new windows form and put this WebControl on it. Do
not set Source of browser yet because we do not know URL that is going to be used to
authorize the user. During loading of WebControl form, get RequestToken same way as we
did in our console application. Now use this request token to get authorization URL from twitter. Use
this URL to call Navigate method on WebBrowser control. Your windows form
will have authentication page from twitter open. Make sure that you have registered an event handler
for LoadCompleted event for WebBrowser control. This will get called every time a new
page is loaded into WebControl. This is where you will be performing the checks.
Check Response
After a user enters twitter credentials and authorizes your application, a new page will be loaded
in WebControl in your form. In event handler for LoadCompleted you will check
URL of the page along with a DIV that has ID of oauth_pin.
If this PIN is there, that means user has authorized your application and extract this PIN to be used
to get access token. If there is no PIN then user has not authorized the application.
Use this extracted PIN to request AccessToken from twitter authorization site as was done
in console application. Serialize the returned access token for later use.
Following code snippet shows how PIN was extracted in LoadCompleted event of WebControl.
private void OnUrlLoadCompleted(object sender,
System.Windows.Navigation.NavigationEventArgs e)
{
if (string.Compare(e.Uri.AbsoluteUri, "http://api.twitter.com/oauth/authorize", true) == 0)
{
if (!e.Uri.Query.Contains("oauth_token"))
{
// Check if there is DIV with id="oauth_pin"
var doc = this._authWebBrowser.Document as mshtml.HTMLDocument;
var oauthPinElement = doc.getElementById("oauth_pin") as mshtml.IHTMLElement;
if (null != oauthPinElement)
{
var div = oauthPinElement as mshtml.HTMLDivElement;
if (null != div)
{
var pinText = div.innerText;
if (!string.IsNullOrEmpty(pinText))
{
// We have validation
OAuthPin = pinText.Trim();
Authorized = true;
}
}
}
else
{
// Uses has deined access.
Authorized = false;
}
this.DialogResult = true;
this.Close();
}
}
}
Sample Project
Attached sample is a VS2010 WPF application. I use TwitterSharp library to
perform all twitter related operations. The binaries for that library are containied
in BIN folder of the project.
|
|
|
by Viper
21. June 2009 08:20
I have gotten lot of messages asking more details about how Bayesian spam filter works. When I posted Bayesian Spam Filter Trainer, i did not added reference to lot of wonderful literature that is available describing what Baye's theorem is and how is used to detect spam. So before I discuss how you can refine the process for twitter, let me give you a list of reference material.
How does it work?
I will quickly give an overview. For details I will definitely recommend reading articles that I mentioned above. Back bone of Bayesian approach is good and bad corpus to train the filter. In simple terms, we take a huge chunk of text, then split the text into individual words, remove the words that may not be of any interest and then calculate frequencies and based on that calculate probabilities of words appearing in a good or spam text.
Small context makes it harder
Using standard corpus to train filter for twitter messages works for most part. I noticed success rate of close to 90%. Well that is not bad. The issue with Twitter messages is that you only have 140 characters to establish meaningful context. And out of that 140, good 20 to 30 characters are taken by compressed URLs that spammers will add to advertise their products or trick you into going to some advertisement affiliate redirect site. So we have about 100 characters available to us to detect spam. If you notice, lot of time a good and spam message looks very close to each other. That causes messages to fall through the cracks or you get false positives.
Refine the filter
Here are some of the refinements that I added to my Spam filter service.
- The filter has been trained with live twitter messages. More data you use to train Bayesian filter, better results you will get.
- Manually go through results and classify messages as good and spam
- Make sure that sender's screen name is included in the corpus. This is based on the discussion in A Plan For Spam where it is mentioned that it is very important that you include an email's header, sender name etc. in corpus. Same applies to twitter corpus as well. You do not have elaborate meta data available. Including screen name in corpus makes sure that spammer's account gets included in spam corpus. So in edge cases where message body may be ambiguous, screen name may end up breaking the tie and correctly classify the text.
-
Link included in message helps in cleaning up some corpus. The service does some post processing on the messages in the background. It checks each message for any links posted in it. And then it expands short URLs into actual URLs and then gets some meta data about the target site. It looks at information like title, keywords, description etc. to establish if the user is attempting to redirect to adult content or key loggers or other questionable sites. Based on the results, the message is re-classified.
-
There is always something new to learn. Yes, the spam filter can never have enough of training. The service continuously updates the corpora and reloads new statistical data.
These are some of the techniques that I have deployed in current spam filter service. As I collect more data, these techniques will be refined further. Any suggestions are most welcome. I am learning too like my spam service.
|
|
|
by Viper
20. June 2009 20:11
Download SpamTrainer Binaries
Download SpamTrainer Source
As more and more people are tweeting, spam is growing with it as well. Every time I search for some topic, almost half of the messages seem to fall in one of the following categories:
- Somebody is trying to sell something
- Somebody is posting links to get you affiliate web sites to make some money
- Job agencies are posting jobs
- .... and more
This week I decided to use Bayesian spam filter, that is used in most email servers to filter spam, on twitter messages. While searching around I found Bayesian Spam Filter for C#. That gave a good starting point. Without making any changes or training with any additional corpus, I was able to get very good filtering results. I observed close to 90% spam detection. I studied the messages that fell through the cracks and also studies false positives. Based on the observations I figured that issue is very limited context of 140 characters in twitter. A lot of good and spam twitter messages look pretty much the same. So the key to improving spam filtering results was to train the filter with twitter messages and not use just rely on corpus taken from emails or things like that. So I decided to build an application that I could use to generate corpus that is classified as spam and good twitter messages.
How does it work
-
Start the application.
- Enter a search term and click on "More Data" button.
- Application will do initial classification of messages. All spam messages are displayed in Orange or light blue color.
- Double on any message to change its classification.

- Once you are satisfied with the results, click on "Accept" button and results are saved in appropriate good and spam files.
- You can load the new corpus results by clicking on "Reload Corpus".
Spam Filter Service
I have created a service that you can use to classify your text if you do not want to build one of your own. Following link provides
more details about the service.
Spam Filter Service
|
|
|
by Viper
13. June 2009 13:32
Last night twitter got hit by Twitpocalypse. What this means is that they made a bad decision about representing status id (aks tweet id) as signed INT. Last night they ran over that limit. This morning lot of applications were broken because of this change. All applications who were using signed INT for status id were getting overflow errors. For example in Tweetsharp in TwitterStatus object, there are two places where this status id is being used. Make sure that you change these properties to use "long" and not "int". This will fix the problem if you are running into conversion issues.
by Viper
9. June 2009 04:34
In my earlier posts Daily twitter trends and Weekly twitter trends I showed how you can get daily and weekly twitter trends. That api gets you to queries that are being searched for. Next step in the process is to take those queries and get the messages posted related to those queries. Here is a simple method that combines results of trend api with search api to get popular messages on twitter.
static Dictionary<string, List<TwitterSearchStatus>> GetPopularTweets()
{
List<TwitterSearchTrend> trends = GetDailyTrends();
if (null == trends ||
trends.Count == 0)
{
return null;
}
// For now only process first trend term.
List<TwitterSearchStatus> searchResults = SearchForTerm(trends[0].Query);
if (searchResults.Count == 0)
{
return null;
}
var resultsGrpByUser = from searchResult in searchResults
group searchResult by searchResult.FromUserScreenName
into userMessages
select new { FromUser = userMessages.Key, Messages = userMessages };
Dictionary<string, List<TwitterSearchStatus>> dict =
new Dictionary<string, List<TwitterSearchStatus>>();
foreach(var userMessage in resultsGrpByUser)
{
Console.WriteLine(userMessage.FromUser);
foreach(var twitterStatus in userMessage.Messages)
{
Console.WriteLine("\t{0}", twitterStatus.Text);
}
dict[userMessage.FromUser] = userMessage.Messages.ToList();
Console.WriteLine("*************");
}
return dict;
}
|
|
|
by Viper
1. June 2009 19:10
Recently I implemented BackYardTweets. Big part
of the site is geo targeting. What that means is that to figure out location of the user visiting the site and getting
longitude and latitude of that location. The implementation uses the following work flow:
- When user visits the site, only information that is available is IP adress assigned to your network connection by
user's internet service provider. The implementation translates that IP address to location including coordinates (longitude and
latitude).
-
After user has logged in, application allows user to change location. User
can pick country, region and city. From these three (some time only 2), application determines geolocation.
Now I will explain how these two steps are performed.
Convert IP Address To Geo Location
Application uses IP to geo location service provided by IP Location tools. A
simple HTTP request to this site's target URL sends xml response that containss all the data that you will need to get location of
site's visitor. Following is sample response.
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Ip>74.125.45.100</Ip>
<Status>OK</Status>
<CountryCode>US</CountryCode>
<CountryName>United States</CountryName>
<RegionCode>06</RegionCode>
<RegionName>California</RegionName>
<City>Mountain View</City>
<ZipPostalCode>94043</ZipPostalCode>
<Latitude>37.4192</Latitude>
<Longitude>-122.057</Longitude>
</Response>
Following code snippet shows the implementation that was done in the application to get geo location based on IP
address of site visitor.
public class UserGeoLocator
{
private const string ApiUrl = "http://ipinfodb.com/ip_query.php?ip={0}";
public SiteUser GetUserLocation(string ipAddress)
{
if (string.IsNullOrEmpty(ipAddress))
{
return null;
}
string reqUrl = string.Format(ApiUrl, ipAddress);
HttpWebRequest httpReq = HttpWebRequest.Create(reqUrl) as HttpWebRequest;
try
{
string result = string.Empty;
HttpWebResponse response = httpReq.GetResponse() as HttpWebResponse;
using (var reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
}
return ProcessResponse(result);
}
catch (Exception ex)
{
throw;
}
}
private SiteUser ProcessResponse(string strResp)
{
StringReader sr = new StringReader(strResp);
XElement respElement = XElement.Load(sr);
string callStatus = (string)respElement.Element("Status");
if (string.Compare(callStatus, "OK", true) != 0)
{
return null;
}
SiteUser user = new SiteUser() {IP = (string) respElement.Element("Ip"),
City = (string) respElement.Element("City"),
Country = (string)respElement.Element("CountryName"),
CountryCode = (string)respElement.Element("CountryCode"),
RegionCode = (string)respElement.Element("RegionCode"),
RegionName = (string)respElement.Element("RegionName"),
PostalCode = (string)respElement.Element("ZipPostalCode"),
Latitude = (decimal)respElement.Element("Latitude"),
Longitude = (decimal)respElement.Element("Longitude")};
return user;
}
}
Geo Location From Address
Application allows user to select Country, Region and City. This needs to
be translated to geo location. Application uses Google's Map API to accomplish the task. For this
you will need to apply for key from Google. Following code snippet is from the application. It takes
address of user as input and translates it to latitude and latitude. Following
code snippet is from class implemented in the application.
public static bool GetGeoLocationFromGoogle(string nearLocation,
out double longitude, out double latitude)
{
bool ret = false;
longitude = latitude = 0d;
string apiKey = ConfigHelper.GooleMapKey;
string reqUrl = string.Format("http://maps.google.com/maps/geo?q={0}&output={1}&key={2}",
HttpUtility.UrlEncode(nearLocation), "csv", apiKey);
WebClient httpClient = new WebClient();
try
{
string response = httpClient.DownloadString(reqUrl);
string[] values = response.Split(",".ToCharArray());
if (values.Length == 4)
{
if (values[0] == "200")
{
latitude = double.Parse(values[2]);
longitude = double.Parse(values[3]);
ret = true;
}
}
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
}
return ret;
}
See how it works
Use see all this in action at following link
|
|
|
by Viper
26. May 2009 19:25
Download Marketweet Projects
For quite some time I have been using a simple console application to promote my clients profile and products
on twitter. This application used the popular technique of adding people to follow list and then
removing them from follow list if they did not reciprocate with the request. The application did
try to honor all ratelimiting protocols enforced by Twitter. One of the dreaded error
you will receive is the one that tells you can not follow more people. If you receive this
error that means you have hit one of the following limits.
- 1,000 updates per day
- 1,000 direct messages per day
- 100 API requests per hour
- Follow limit
For more details about these limits and why they are there, I will strongly recommend that you read
details at I can't follow people: follow limits.
This is definitely worth reading before you get your account marked as spammer.
Work Flow
At high level following steps describe the process that is followed by the application.
- Select keywords that you want to target.
- Perform search for status updated and messages for the keywords
- Collect list of users with relevant keywords
- Remove all the users from this list who are already following you
- Send follow requests to rest of the users
- Send unfollow requests to users who have not added you to their follow list in 12 hours
- Repeat the process every few hours
Throttle Requests
Twitter has put a limit on how many users you can add to your follow list. This limit was established to make sure
that spammers do not spam people with requests. The first hard limit has been set to 2000 follows. There
is lot of confusion about this number. There is mis-conception that you can not have more than 2000 follows. This
is not correct. You can have as many follows as you want as long as the following conditions are satisfied.
- Number of people following you are more than number of people you are following
- Or Number of people following you are about 80% of number of people you are following
- And you are not sending more than 1000 follow requests in a day
These limits are not something that Twitter has published. This is something that I have established from experience
during all this time dealing with Twitter API. The main lesson is that as long as you play by the rules, the game will not
punish you and you can play the game to your advantage. I have incorporated these rules into Marketweet console
application. After each follow request application checks for limits. The moment application reaches the threshold values,
further requests are terminated.
Choose right search terms
This is the most important part of the game. You need to pick the search terms very carefully. A right combination of
search terms will find you users who can be good addition to your follow list. From my experience I have seen that it
takes few iterations and good experience to come up with effective keywords. One of my clients is seller of woman's clothing. I asked
them what they though would be keyword that they would like to target. This client is established on first page of
Google search for few years now. So they gave me one word. I tried the search in Twitter in front of them. To their
surprise 90% of the results that came up were for other merchants who were advertising their products. the lesson to learn
from this example is that on social networking or micro-blogging sites you want to search for words that are complimentary to
your target keywords.
Marketweet
This is what I call my marketing tool for twitter. It is a simple console application developed using .Net 3.5 framework. It uses
Tweetsharp API to consume Twitter REST API and perform all the actions. Recently I
published Twitter .Net Samples that
show how this API can be used. I have included the binary files for this API with the console application project included with this
post. You can download and compile the project. You will need to supply appropriate information in configuration file for the application
to run. Enter your target keywords on separate lines in SearchTerms.txt file.
Cost?
There is no cost for this application. If you are not a developer and want to use this application, drop me a line. I will compile
it and send you instructions on how to use it. I am working on creating a simple GUI application which I plan to post
some time next week.
|
|
|
23902386-0ba1-4ec5-94f9-bac03aeb1372|0|.0
Views: 3358
Tags:
Twitter
by Viper
26. May 2009 10:49
While working on new twitter marketing tool Marketweet, I ran into a situation. I need to get count
of people who a person is following. There is an API that gets you a user's profile. But if you notice the results
you will find that results are not current. Some time they are cached for hours. So I was tried few APIs. Finally
I found the one that does return current information. You can get IDs of friends from a user's Social Graph. There
are 2 main issues with this approach as well.
- You do not know how many total results will be returned.
- What is maximum number of results per page that can be returned by this API
So to work around this issue. Here is what I ended up doing. One I set an upper limit of 100000 on number
of users that an account may be following. Also grabbed user's cached profile as well and from there
got number of friends. And ended up using maximum of these two numbers. So when the API runs out of more
results, it returns response that translates to empty collection. And you can use that as sentinel
condition to break out of iterative loop.
static List<TwitterUser> GetFollowings()
{
IFluentTwitter ft = FluentTwitter.CreateRequest();
ft.AuthenticateAs(LOGIN_NAME, PASSWORD);
var profileReq = ft.Users().ShowProfileFor(LOGIN_NAME).AsJson();
var thisUser = profileReq.Request().AsUser();
var followersCount = thisUser.FriendsCount;
var upLimit = (followersCount < 100000) ? 100000 : followersCount;
var ceiling = Math.Ceiling(upLimit / 100m);
var results = new List<TwitterUser>();
for (var i = 1; i <= ceiling; i++)
{
var friendsResp = ft.Users().GetFriends().For(thisUser.Id)
.Skip(i).AsJson().Request();
if (null == friendsResp)
{
break;
}
IEnumerable<TwitterUser> friends = friendsResp.AsUsers();
if (friends == null ||
friends.Count() == 0)
{
break;
}
results.AddRange(friends);
}
return results;
}
|
|
|
by Viper
22. May 2009 14:50
This code sample will show how you can get weekly trends using REST API. The sample shows another feature of the API where you can specify a criteria to limit the results. You can specify a start date for the report. This is set by calling On method on ITwitterSearchTrends.
static List GetWeeklyTrends(DateTime ? onDate)
{
TwitterClientInfo clientInfo = new TwitterClientInfo();
clientInfo.ClientName = "Marketweet";
clientInfo.ClientUrl = "http://www.byteblocks.com";
clientInfo.ClientVersion = "1.1";
List trendNames = new List();
IFluentTwitter ft = FluentTwitter.CreateRequest(clientInfo);
ft.AuthenticateAs(LOGIN_NAME, PASSWORD);
ITwitterSearchTrends searchTrends = ft.Search().Trends().Weekly();
if (null != onDate)
{
searchTrends.On(onDate.Value).AsJson();
}
var resp = ft.Request();
if (null != resp)
{
var trends = resp.AsTrends();
if (null != trends)
{
foreach (TwitterSearchTrend trend in trends.Trends)
{
trendNames.Add(trend.Name);
}
}
}
return trendNames;
}
by Viper
22. May 2009 12:53
This code sample will show how you can use Twitter REST API to verify if one twitter user is following a specified user or not. The use of this API is little tricky. It requires you to specify two screen names or IDs that you want to verify. There is Exists method on Friendships object in Tweetsharp API but it only takes one parameter. If you try to use the API just like that it will return error telling you that you need to specify two screen names or ids to verify. The use of this API is not very intutive. So I had to dig down into the code to figure out how it can be done. There is property VerifyScreenName on IFluentTwitterParameters. You have to set second screen name here. See how it is done in code below.
static void VerifyFriendShip(string screenName1, string screenName2)
{
IFluentTwitter ft = FluentTwitter.CreateRequest();
ft.AuthenticateAs(LOGIN_NAME, PASSWORD);
ft.Friendships().Verify(screenName1);
ft.Parameters.VerifyScreenName = screenName2;
var resp = ft.Request();
if (null != resp)
{
Console.WriteLine(resp);
}
}
Return value from this API is true or false.
|
|
|
by Viper
22. May 2009 12:14
This code sample will demonstrate how you can get list of people who are following you on twitter using Twitter REST API. The reason I mentioned "following you" because this API requires authentication. So unless you have credentials of other users, you can execute this API against the authenticated user account only.
static void GetMyFollowers()
{
IFluentTwitter ft = FluentTwitter.CreateRequest();
ft.AuthenticateAs(LOGIN_NAME, PASSWORD);
ft.Users().GetFollowers().AsJson();
var resp = ft.Request();
if (null != resp)
{
var users = resp.AsUsers();
if (null != users)
{
foreach (TwitterUser user in users)
{
Console.WriteLine("User Id: {0}, Screen Name: {1}", user.Id, user.ScreenName);
}
}
}
}
by Viper
22. May 2009 11:47
In this code sample I will demonstrate how you can use twitter API to add a new status message to your account. In terms of Twitter API it is termed as Update of your status.
static void AddStausMessage(string msg)
{
IFluentTwitter ft = FluentTwitter.CreateRequest();
ft.AuthenticateAs(LOGIN_NAME, PASSWORD);
ft.Statuses().Update(msg).AsJson();
var resp = ft.Request();
if (null != resp)
{
var status = resp.AsStatus();
if (null != status)
{
Console.WriteLine("Status Id: {0}",status.Id);
Console.WriteLine("Message: {0}", status.Text);
}
}
}
When API executes successfully it returns status object in response. I tested this method using this call AddStausMessage("Publishing more code samples showing to use Twitter API");. You can see this on my
profile on twitter by clicking HERE
by Viper
22. May 2009 05:32
In this code snippet I will show you how you can remove a twitter user from your follow list. Twitter API calls it destroying friendship.
static void RemoveFromFollow(string userName)
{
IFluentTwitter ft = FluentTwitter.CreateRequest();
ft.AuthenticateAs(LOGIN_NAME, PASSWORD);
ft.Friendships().Destroy(userName);
var resp = ft.Request();
if (null != resp)
{
TwitterUser user = resp.AsUser();
if (null != user)
{}
}
}
by Viper
22. May 2009 05:28
This code snippet will demonstrate how you can add a twitter user to your follow list. This is called adding a user to your friends list.
static void AddToFollow(string userName)
{
IFluentTwitter ft = FluentTwitter.CreateRequest();
ft.AuthenticateAs(LOGIN_NAME, PASSWORD);
ft.Friendships().Befriend(userName);
var resp = ft.Request();
if (null != resp)
{
TwitterUser user = resp.AsUser();
if (null != user)
{
Console.WriteLine("User {0} added to follow list", user.ScreenName);
}
}
}
On successful execution of request, API returns details of the user that you added to your friend list.
by Viper
22. May 2009 05:11
In last post Search for twitter status updates I showed how you can do simple search. If you do not restrict or constrain your search, twitter will always return you maximum of latest 1500 updates. To make your query more efficient and reduce network traffic, you definitely want to only get results since your last search results. If you notice in the out put from last post, there is a property of status object named Id. Twitter always adds status entries in increasing order of this number. So if you have cached id of the latest results from last result, you can use that to tell twitter to return you the results since that Id only. You can also constrain the results by specifying a date as well. The following code snippet shows how you can perform search by specifying Since search criteria to get results from that point onwards only.
static void SearchForTermSince(string term, int id)
{
IFluentTwitter ft = FluentTwitter.CreateRequest();
ft.AuthenticateAs(LOGIN_NAME, PASSWORD);
ITwitterSearchQuery sq = ft.Search().Query().Containing(term).Since(id);
var searchResults = new List();
var resultsPerPage = 50;
// Maximum results returned from tweeter are 1500.
var totalPages = Math.Ceiling(1500m / resultsPerPage);
for (var i = 1; i <= totalPages; i++)
{
var search = sq.Return(resultsPerPage)
.Skip(i)
.AsJson();
var response = search.Request();
var results = response.AsSearchResult();
searchResults.AddRange(results.Statuses);
}
// Display results
foreach (var status in searchResults)
{
Console.WriteLine("id: {0}", status.Id);
Console.WriteLine(status.FromUserScreenName);
Console.WriteLine(status.CreatedDate);
Console.WriteLine(status.SinceId);
Console.WriteLine(status.Text);
Console.WriteLine("*************************");
}
}
To test it, I used SearchForTermSince("swine", 1881546655);. Notice that id value of 1881546655 is third result from the top from previous run when there was no constraint added to search. Now see the results when I added Since criteria to search. You will notice that results started from id = 1881550472 which was the result right after 1881546655.
id: 1881554462
SwineFluNewsGB
5/22/2009 11:58:39 AM
0
Swine Flu News: H1N1 Swine Flu - Update 46. May 22 http://tinyurl.com/q357wf
*************************
id: 1881553402
mgrabois
5/22/2009 11:58:29 AM
0
@thedateabledork In a world without swine flu, I would be leaving for Cancun this morning ;(
*************************
id: 1881551912
littlekelsey
5/22/2009 11:58:15 AM
0
sick, at home. i swear i have swine flu. wish i had company <3
*************************
id: 1881550472
MessiahKaeto
5/22/2009 11:58:00 AM
0
@envysays http://twitpic.com/5ooyq - You ain't kidding, I'd be all over that
like a swine-flu patients mask!!
|
|
|
by Viper
22. May 2009 05:03
This code snippets will demonstrate how you can use Tweetsharp API.
static void SearchForTerm(string term)
{
IFluentTwitter ft = FluentTwitter.CreateRequest();
ft.AuthenticateAs(LOGIN_NAME, PASSWORD);
ITwitterSearchQuery sq = ft.Search().Query().Containing(term);
var searchResults = new List();
var resultsPerPage = 50;
// Maximum results returned from tweeter are 1500.
var totalPages = Math.Ceiling(1500m/resultsPerPage);
for (var i = 1; i <= totalPages; i++)
{
var search = sq.Return(resultsPerPage)
.Skip(i)
.AsJson();
var response = search.Request();
var results = response.AsSearchResult();
searchResults.AddRange(results.Statuses);
}
// Display results
foreach (var status in searchResults)
{
Console.WriteLine("id: {0}", status.Id);
Console.WriteLine(status.FromUserScreenName);
Console.WriteLine(status.CreatedDate);
Console.WriteLine(status.SinceId);
Console.WriteLine(status.Text);
Console.WriteLine("*************************");
}
}
Here are some of the top results for query term swine that is one of the most talked about topics these days. You will notice that search results are returned in chronological order. For brevity I have only listed few results instead of all 1500 which is maximum number of results twitter API will return.
id: 1881551912
littlekelsey
5/22/2009 11:58:15 AM
0
sick, at home. i swear i have swine flu. wish i had company <3
*************************
id: 1881550472
MessiahKaeto
5/22/2009 11:58:00 AM
0
@envysays http://twitpic.com/5ooyq - You ain't kidding,
I'd be all over that like a swine-flu patients mask!!
*************************
id: 1881546655
AstroXa
5/22/2009 11:57:22 AM
0
@in_health #H1N1 #Swine Flu WHO Update #36, 11168 cases 86 deaths 42 countries,
22 May 2009, 06.00 GMT - http://bit.ly/YtLEj
*************************
id: 1881546160
jcesar18
5/22/2009 11:57:17 AM
0
Swine flu hit albany...
*************************
id: 1881545636
nationalpost
5/22/2009 11:57:12 AM
0
Season change could spread swine flu in Asia-Pacific http://tinyurl.com/oucylx
*************************
|
|
|
by Viper
22. May 2009 04:44
This code snippet shows you how you can get twitter daily trends using TweetSharp API. It is very important that you get latest code for the API. There was a bug in Trend API that I discovered and got fixed from the developers of the API.
static void GetDailyTrends()
{
IFluentTwitter ft = FluentTwitter.CreateRequest();
ft.AuthenticateAs(LOGIN_NAME, PASSWORD);
ft.Search().Trends().Daily().AsJson();
var resp = ft.Request();
if (null != resp)
{
var trends = resp.AsTrends();
if (null != trends)
{
foreach (TwitterSearchTrend trend in trends.Trends)
{
Console.WriteLine("Query: {0}\tName: {1}",
trend.Query.PadRight(40), trend.Name);
}
}
}
}
Outpt of this console application looks like below.
Query: "American Idol" OR #idol Name: American Idol
Query: Cavs Name: Cavs
Query: "Kris Allen" Name: Kris Allen
Query: "Adam Lambert" OR Lambert Name: Adam Lambert
Query: LeBron Name: LeBron
Query: Cleveland Name: Cleveland
Query: Kobe Name: Kobe
Query: Kiss Name: Kiss
Query: NBA Name: NBA
Query: #americanidol Name: #americanidol
Query: "Rashard Lewis" Name: Rashard Lewis
Query: "Top Echelon Network" Name: Top Echelon Network
Query: "Go Magic" OR Magic Name: Go Magic
Query: Glee Name: Glee
Query: "Star Trek" Name: Star Trek
Query: Lakers Name: Lakers
Query: Adam's Name: Adam's
Query: "Terminator Salvation" Name: Terminator Salvation
Query: Goodnight Name: Goodnight
Query: Queen Name: Queen
|
|
|
Powered by BlogEngine.NET 1.5.1.7
Theme by Naveen Kohli
|
|