Skip to content

Instantly share code, notes, and snippets.

@jz5
Last active September 30, 2015 16:18
Show Gist options
  • Save jz5/2038cefe289e00c10bb8 to your computer and use it in GitHub Desktop.
Save jz5/2038cefe289e00c10bb8 to your computer and use it in GitHub Desktop.
public class SharedCountReulst
{
public string Url { get; set; }
public long MaxId { get; set; }
public int Count { get; set; }
}
public static SharedCountReulst Count(string url, long sinceId)
{
var tokens = CoreTweet.Tokens.Create(consumerKey, consumerSecret, accessToken, accessTokenSecret);
var result = new SharedCountReulst() { Url = url };
var d = new Dictionary<string, object> {
{"result_type","recent" },
{"count",100 },
{"include_entities", false },
{"q", url },
{"since_id", sinceId }};
var isFirstTime = true;
for (;;)
{
// GET search/tweets
var searchResult = tokens.Search.Tweets(d);
// ループ内1回目の検索の場合、max_id を記録
// (次回新たに最新のツイートから検索するときの since_id となる)
if (isFirstTime)
{
result.MaxId = searchResult.SearchMetadata.MaxId;
isFirstTime = false;
}
// シェア数
result.Count += searchResult.Count;
// 一度に全件取得できなかった場合、max_id = (取得したツイートの最小の ID) - 1 と設定し
// 古いツイートを検索・取得する
if (searchResult.SearchMetadata.NextResults != null)
{
d["max_id"] = searchResult.Min(s => s.Id) - 1;
continue;
}
break;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment