get data from twitter csharp

get data from twitter csharp:

public string getTwitterData(string resource_url, string str)
{

string numPosts = “200”;

// oauth implementation details
var oauth_version = “1.0”;
var oauth_signature_method = “HMAC-SHA1”;

// unique request details
var oauth_nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
var timeSpan = DateTime.UtcNow – new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
var oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();
// create oauth signature
var baseFormat = “numPosts={7}&oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}” +
“&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&screen_name={6}”;

var baseString = string.Format(baseFormat,
oauth_consumer_key,
oauth_nonce,
oauth_signature_method,
oauth_timestamp,
oauth_token,
oauth_version,
Uri.EscapeDataString(str),
Uri.EscapeDataString(numPosts)
);

baseString = string.Concat(“GET&”, Uri.EscapeDataString(resource_url), “&”, Uri.EscapeDataString(baseString));

var compositeKey = string.Concat(Uri.EscapeDataString(oauth_consumer_secret),
“&”, Uri.EscapeDataString(oauth_token_secret));

string oauth_signature;
using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey)))
{
oauth_signature = Convert.ToBase64String(
hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString)));
}

// create the request header
var headerFormat = “OAuth oauth_nonce=\”{0}\”, oauth_signature_method=\”{1}\”, ” +
“oauth_timestamp=\”{2}\”, oauth_consumer_key=\”{3}\”, ” +
“oauth_token=\”{4}\”, oauth_signature=\”{5}\”, ” +
“oauth_version=\”{6}\””;

var authHeader = string.Format(headerFormat,
Uri.EscapeDataString(oauth_nonce),
Uri.EscapeDataString(oauth_signature_method),
Uri.EscapeDataString(oauth_timestamp),
Uri.EscapeDataString(oauth_consumer_key),
Uri.EscapeDataString(oauth_token),
Uri.EscapeDataString(oauth_signature),
Uri.EscapeDataString(oauth_version)
);

ServicePointManager.Expect100Continue = false;

// make the request
var postBody = string.Format(“screen_name={0}&numPosts={1}”, Uri.EscapeDataString(str), Uri.EscapeDataString(numPosts));

resource_url += “?” + postBody;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url);
request.Headers.Add(“Authorization”, authHeader);
request.Method = “GET”;
request.ContentType = “application/x-www-form-urlencoded”;
var objResult = “”;
try
{
var response = (HttpWebResponse)request.GetResponse();

var reader = new StreamReader(response.GetResponseStream());
objResult = reader.ReadToEnd();
}
catch(Exception ex)
{

}

try
{
JArray jsonDat = JArray.Parse(objResult);

for (int x = 0; x < jsonDat.numPosts(); x++)
{
//post details

jsonDat[x][“created_at”].ToString() ;//Post_Time
jsonDat[x][“id”].ToString();//Post_Id
jsonDat[x][“text”].ToString();//Post_Message
jsonDat[x][“retweet_numPosts”].ToString() ;//Share_numPosts
jsonDat[x][“favorite_numPosts”].ToString() ;//Post_Like

jsonDat[x][“created_at”].ToString(); //Post Creation time
jsonDat[x][“user”][“id”].ToString(); //page id
jsonDat[x][“id”].ToString();//post id
jsonDat[x][“text”].ToString(); // post message
Int64.Parse( jsonDat[x][“retweet_numPosts”].ToString());//number of shares
Int64.Parse(jsonDat[x][“favorite_numPosts”].ToString());//number of like
jsonDat[x][“user”][“screen_name”].ToString(); // page name

}
}
catch(Exception e ){}
}

Get tweets from hashtag :

get data from twitter cscharp
public void getHashtagTwitter(string q, String grp_Nme)
{

string resource_url_hashtag = “https://api.twitter.com/1.1/search/tweets.json”;
string html = “”;

//limit
string count = “200”;

// oauth implementation details
var oauth_version = “1.0”;
var oauth_signature_method = “HMAC-SHA1”;

// unique request details

var oauth_nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
var timeSpan = DateTime.UtcNow – new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
var oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();
// create oauth signature
var baseFormat=””;
var baseString = “”;
if (lastPostId == “0”)
{
baseFormat = “count={7}&oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}” +
“&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&q={6}”;
baseString = string.Format(baseFormat,
oauth_consumer_key,
oauth_nonce,
oauth_signature_method,
oauth_timestamp,
oauth_token,
oauth_version,
q,
Uri.EscapeDataString(count)
);
}
else
{
baseFormat = “count={7}&oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}” +
“&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&q={6}&since_id={8}”;
baseString = string.Format(baseFormat,
oauth_consumer_key,
oauth_nonce,
oauth_signature_method,
oauth_timestamp,
oauth_token,
oauth_version,
q,
Uri.EscapeDataString(count),
Uri.EscapeDataString(lastPostId)
);
}
baseString = string.Concat(“GET&”, Uri.EscapeDataString(resource_url_hashtag), “&”, Uri.EscapeDataString(baseString));

var compositeKey = string.Concat(Uri.EscapeDataString(oauth_consumer_secret),
“&”, Uri.EscapeDataString(oauth_token_secret));

string oauth_signature;
using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey)))
{
oauth_signature = Convert.ToBase64String(
hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString)));
}

// create the request header
var headerFormat = “OAuth oauth_nonce=\”{0}\”, oauth_signature_method=\”{1}\”, ” +
“oauth_timestamp=\”{2}\”, oauth_consumer_key=\”{3}\”, ” +
“oauth_token=\”{4}\”, oauth_signature=\”{5}\”, ” +
“oauth_version=\”{6}\””;

var authHeader = string.Format(headerFormat,
Uri.EscapeDataString(oauth_nonce),
Uri.EscapeDataString(oauth_signature_method),
Uri.EscapeDataString(oauth_timestamp),
Uri.EscapeDataString(oauth_consumer_key),
Uri.EscapeDataString(oauth_token),
Uri.EscapeDataString(oauth_signature),
Uri.EscapeDataString(oauth_version)
);

ServicePointManager.Expect100Continue = false;

// make the request

var postBody = “”;
if(lastPostId == “0”) //here lastPostId is the last Post id that exists with u 
postBody = string.Format(“q={0}&count={1}”, q, Uri.EscapeDataString(count));
else
postBody = string.Format(“q={0}&count={1}&since_id={2}”, q, Uri.EscapeDataString(count), Uri.EscapeDataString(lastPostId));

resource_url_hashtag += “?” + postBody;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url_hashtag);
request.Headers.Add(“Authorization”, authHeader);
request.Method = “GET”;
request.ContentType = “application/x-www-form-urlencoded”;
var objText = “”;
try
{
var response = (HttpWebResponse)request.GetResponse();

var reader = new StreamReader(response.GetResponseStream());
objText = reader.ReadToEnd();
}
catch (Exception e)
{
using (StreamWriter w = File.AppendText(path))
{
Log(“Exception occured: ” + e.Message, w);
}
}

}