To get complete code of step by step tutorial to Get Tweets and Twitter Data Csharp C#.Click the download button at the end of post.
Step by step tutorial to Get Tweets And Twitter Data Csharp C#
From Twitter?
This is the step by step tutorial to get twitter data using twitter API LinqToTwitter and C#. This tutorial “get twitter data using Csharp” also cover the following topic.
- Get user id twitter using c#
- Get Created Date of twitter user using C#
- Get Favorite count of user twitter c#
- Get retweet count of all tweets twitter using c#
- Get tweet id of all tweets twitter using c#
- Get all past tweets twitter using c#
- Get follower count of user twitter using c#
- Get friend count of user twitter using c#
- Get screen name of user twitter using c#
- Get Statuses Count of specified person twitter using c#
- Get Time zone of current user twitter using c#
- Get Hashtag of all tweet twitter using c#
- Get User mentions in the tweet twitter using c#
- Get Location or city of user twitter using c#
Things to know before going forward.
- Visual studio 2013
- C#
- LinqToTwitter
Twitter LinqToTweet API Limitations
- It only allows 200 requests per 15 minutes per user.
- It fetches 200 record per request.
Step 1)
The First most step to use twitter API is to register your app to twitter Search Console. For this purpose Login to Twitter and Go to the link https://dev.twitter.com/
Click “My apps” on upper right corner. Then click on create new app. The below page shown.
Fill all the required fields. And then click on the “create your twitter application” button.
Following page is shown.
Here you go to link “manage keys and access token”.
From this page we require these four Keys to get twitter data using Csharp C#.
- Consumer key
- Consumer secret key
- Access token
- Access token secret
To get Access Token and Access Tokensecret click on “generate access token” button. Now the Access token and Access token secret is visible.
Step 2)
Open Visual stutio 2013 and create a new C# console application to get twitter data using Csharp. Then install LinqToTwitter library from nugget.
//config file
Here is Main Method
static void Main(string[] args)
{
//input file for input screen name. give as many as you want.and output for fetched records
//please put scrren names of user whom you want to get data in input file
string inputpath = "C:\\Users\\Shehzad Amjad\\Desktop\\tweetfolder\\input.txt";
string outputpath = "C:\\Users\\Shehzad Amjad\\Desktop\\tweetfolder\\output.txt";
//check if output file already exists then delete it
if (File.Exists(outputpath))
{
File.Delete(outputpath);
File.Create(outputpath).Dispose();
}
else
{
File.Create(outputpath).Dispose();
}
var file = new System.IO.StreamWriter(outputpath);
//read screen names from input file and fetch record one by one user
//and write record on output files after some cleansing
//it will remove or replace \n and \t with sapce and put \t on all attribute right site
//to make the output sensible and readable
var ReadAllLines = File.ReadAllLines(inputpath);
foreach(var line in ReadAllLines)
{
Console.WriteLine("working on "+line+", Fetching Tweets...");
var tweetList = GetTwitterFeeds(line);
Console.WriteLine("Fetch Tweets Count " + tweetList.Count);
string tweet_header = "User ID\t" + "Created At\t" + "Favourite Count\t" + "ReTweet count\t" + "ReTweeted\t" + "Tweet ID\t" + "Tweet\t" + "Follower Count\t" + "Friends Count\t" + "Screen Name\t" + "Statuses Count\t" + "Time Zone\t" + "URL\t" + "Verified\t" + "HashTag\t" + "User Mentions\t" + "Location\t";
file.WriteLine(tweet_header);
foreach (var item in tweetList)
{
string user_id = item.User.UserIDResponse.Replace("\r", "").Replace("\n", "").Replace("\t", "") + "\t";
string created_at = item.CreatedAt.ToString().Replace("\r", "").Replace("\n", "").Replace("\t", "") + "\t";
string favourite_count = item.User.FavoritesCount.ToString().Replace("\r", "").Replace("\n", "").Replace("\t", "") + "\t"; //
string rt_count = item.RetweetCount.ToString().Replace("\r", "").Replace("\n", "").Replace("\t", "") + "\t";
string retweeted = item.Retweeted.ToString().Replace("\r", "").Replace("\n", "").Replace("\t", "") + "\t";
string tweet_id = item.StatusID.ToString().Replace("\r", "").Replace("\n", "").Replace("\t", "") + "\t";
string tweet_text = item.Text.Replace("\r", "").Replace("\n", "").Replace("\t", "") + "\t";
string follower_count = item.User.FollowersCount.ToString().Replace("\r", "").Replace("\n", "").Replace("\t", "") + "\t";
string friends_count = item.User.FriendsCount.ToString().Replace("\r", "").Replace("\n", "").Replace("\t", "") + "\t";
string screen_name = item.User.ScreenNameResponse.Replace("\r", "").Replace("\n", "").Replace("\t", "") + "\t";
string statuses_count = item.User.StatusesCount.ToString().Replace("\r", "").Replace("\n", "").Replace("\t", "") + "\t";
string time_zone = "null";
if (item.User.TimeZone!=null)
{
time_zone = item.User.TimeZone.Replace("\r", "").Replace("\n", "").Replace("\t", "") + "\t";
}
else
{
time_zone = time_zone.Replace("\r", "").Replace("\n", "").Replace("\t", "") + "\t";
}
string url = "url\t";//item.User.Url.Replace("\r", "").Replace("\n", "").Replace("\t", "") + "\t";
string verified = item.User.Verified.ToString().Replace("\r", "").Replace("\n", "").Replace("\t", "") + "\t";
string hashtag = "";
foreach (var tags in item.Entities.HashTagEntities)
{
hashtag = hashtag + tags.Tag + @" \ ";
}
hashtag = hashtag.Replace("\r", "").Replace("\n", "").Replace("\t", "") + "\t";
string user_mentions = "";
foreach (var mentions_user in item.Entities.UserMentionEntities)
{
user_mentions = user_mentions + mentions_user.ScreenName + @" \ ";
}
user_mentions = user_mentions.Replace("\r", "").Replace("\n", "").Replace("\t", "") + "\t";
string location = item.User.Location.Replace("\r", "").Replace("\n", "").Replace("\t", "") + "\t";
string tweet_record = user_id + created_at + favourite_count + rt_count + retweeted + tweet_id + tweet_text + follower_count + friends_count + screen_name + statuses_count + time_zone + url + verified + hashtag + user_mentions + location;
file.WriteLine(tweet_record);
}
Console.WriteLine("Done "+line+". written in file");
}
file.Close();
Console.WriteLine("Done! .check Your Drive File");
Console.ReadLine();
}
Here is GetTwitterfeed Method
//fuction to get twitter feed by screen name
public static List
{
var auth = new SingleUserAuthorizer
{
CredentialStore = new InMemoryCredentialStore()
{
//getting keys from config file .dont for got to palce these files
ConsumerKey = ConfigurationManager.AppSettings["consumerkey"],
ConsumerSecret = ConfigurationManager.AppSettings["consumersecret"],
OAuthToken = ConfigurationManager.AppSettings["accessToken"],
OAuthTokenSecret = ConfigurationManager.AppSettings["accessTokenSecret"]
}
};
var twitterCtx = new TwitterContext(auth);
var ownTweets = new List
ulong maxId = 0;
bool flag = true;
var statusResponse = new List
statusResponse = (from tweet in twitterCtx.Status
where tweet.Type == StatusType.User
&& tweet.ScreenName == screenname
&& tweet.Count == 200
select tweet).ToList();
if (statusResponse.Count > 0)
{
maxId = ulong.Parse(statusResponse.Last().StatusID.ToString()) - 1;
ownTweets.AddRange(statusResponse);
}
do
{
int rateLimitStatus = twitterCtx.RateLimitRemaining;
if (rateLimitStatus != 0)
{
statusResponse = (from tweet in twitterCtx.Status
where tweet.Type == StatusType.User
&& tweet.ScreenName == screenname
&& tweet.MaxID == maxId
&& tweet.Count == 200
select tweet).ToList();
if (statusResponse.Count != 0)
{
maxId = ulong.Parse(statusResponse.Last().StatusID.ToString()) - 1;
ownTweets.AddRange(statusResponse);
}
else
{
flag = false;
}
}
else
{
flag = false;
}
} while (flag);
return ownTweets;
}
}
}
To Download step by step tutorial to Get Tweets and Twitter Data Csharp C# complete Code example Click the below Download button.
Upload, download, delete files on amazon S3 cloud using c#.Click Here