}
[Fact]
+ public async Task SearchTweets_Test()
+ {
+ using (var twitterApi = new TwitterApi())
+ {
+ var mock = new Mock<IApiConnection>();
+ mock.Setup(x =>
+ x.GetAsync<TwitterSearchResult>(
+ new Uri("search/tweets.json", UriKind.Relative),
+ new Dictionary<string, string> {
+ { "q", "from:twitterapi" },
+ { "result_type", "recent" },
+ { "include_entities", "true" },
+ { "include_ext_alt_text", "true" },
+ { "lang", "en" },
+ { "count", "200" },
+ { "max_id", "900" },
+ { "since_id", "100" },
+ },
+ "/search/tweets")
+ )
+ .ReturnsAsync(new TwitterSearchResult());
+
+ twitterApi.apiConnection = mock.Object;
+
+ await twitterApi.SearchTweets("from:twitterapi", "en", count: 200, maxId: 900L, sinceId: 100L)
+ .ConfigureAwait(false);
+
+ mock.VerifyAll();
+ }
+ }
+
+ [Fact]
public async Task ListsOwnerships_Test()
{
using (var twitterApi = new TwitterApi())
return this.apiConnection.PostLazyAsync<TwitterStatus>(endpoint, param);
}
+ public Task<TwitterSearchResult> SearchTweets(string query, string lang = null, int? count = null, long? maxId = null, long? sinceId = null)
+ {
+ var endpoint = new Uri("search/tweets.json", UriKind.Relative);
+ var param = new Dictionary<string, string>
+ {
+ ["q"] = query,
+ ["result_type"] = "recent",
+ ["include_entities"] = "true",
+ ["include_ext_alt_text"] = "true",
+ };
+
+ if (lang != null)
+ param["lang"] = lang;
+ if (count != null)
+ param["count"] = count.ToString();
+ if (maxId != null)
+ param["max_id"] = maxId.ToString();
+ if (sinceId != null)
+ param["since_id"] = sinceId.ToString();
+
+ return this.apiConnection.GetAsync<TwitterSearchResult>(endpoint, param, "/search/tweets");
+ }
+
public Task<TwitterLists> ListsOwnerships(string screenName, long? cursor = null)
{
var endpoint = new Uri("lists/ownerships.json", UriKind.Relative);
this.CreateApiCalllback("/favorites/list"));
}
- public HttpStatusCode Search(string words, string lang, int? count, long? maxId, long? sinceId, ref string content)
- {
- Dictionary<string, string> param = new Dictionary<string, string>();
- if (!string.IsNullOrEmpty(words)) param.Add("q", words);
- if (!string.IsNullOrEmpty(lang)) param.Add("lang", lang);
- if (count != null) param.Add("count", count.ToString());
- if (maxId != null) param.Add("max_id", maxId.ToString());
- if (sinceId != null) param.Add("since_id", sinceId.ToString());
-
- if (param.Count == 0) return HttpStatusCode.BadRequest;
-
- param.Add("result_type", "recent");
- param.Add("include_entities", "true");
- param.Add("include_ext_alt_text", "true");
- return httpCon.GetContent(GetMethod,
- this.CreateTwitterUri("/1.1/search/tweets.json"),
- param,
- ref content,
- this.CreateRatelimitHeadersDict(),
- this.CreateApiCalllback("/search/tweets"));
- }
-
public HttpStatusCode SavedSearches(ref string content)
{
return httpCon.GetContent(GetMethod,
p.Report("Search refreshing...");
- await Task.Run(() =>
+ await Task.Run(async () =>
{
WebApiException lastException = null;
if (string.IsNullOrEmpty(tab.SearchWords))
continue;
- this.tw.GetSearch(read, tab, false);
+ await this.tw.GetSearch(read, tab, false)
+ .ConfigureAwait(false);
if (loadMore)
- this.tw.GetSearch(read, tab, true);
+ await this.tw.GetSearch(read, tab, true)
+ .ConfigureAwait(false);
}
catch (WebApiException ex)
{
return minimumId;
}
- private long? CreatePostsFromSearchJson(string content, TabClass tab, bool read, int count, bool more)
+ private long? CreatePostsFromSearchJson(TwitterSearchResult items, TabClass tab, bool read, int count, bool more)
{
- TwitterSearchResult items;
- try
- {
- items = TwitterSearchResult.ParseJson(content);
- }
- catch (SerializationException ex)
- {
- MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
- throw new WebApiException("Json Parse Error(DataContractJsonSerializer)", content, ex);
- }
- catch (Exception ex)
- {
- MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
- throw new WebApiException("Invalid Json!", content, ex);
- }
-
long? minimumId = null;
foreach (var result in items.Statuses)
throw new WebApiException(lastException.Message, lastException);
}
- public void GetSearch(bool read,
- TabClass tab,
- bool more)
+ public async Task GetSearch(bool read, TabClass tab, bool more)
{
- HttpStatusCode res;
- var content = "";
var count = GetApiResultCount(MyCommon.WORKERTYPE.PublicSearch, more, false);
+
long? maxId = null;
long? sinceId = null;
if (more)
sinceId = tab.SinceId;
}
- try
- {
- // TODO:一時的に40>100件に 件数変更UI作成の必要あり
- res = twCon.Search(tab.SearchWords, tab.SearchLang, count, maxId, sinceId, ref content);
- }
- catch(Exception ex)
- {
- throw new WebApiException("Err:" + ex.Message, ex);
- }
- switch (res)
- {
- case HttpStatusCode.BadRequest:
- throw new WebApiException("Invalid query", content);
- case HttpStatusCode.NotFound:
- throw new WebApiException("Invalid query", content);
- case HttpStatusCode.PaymentRequired: //API Documentには420と書いてあるが、該当コードがないので402にしてある
- throw new WebApiException("Search API Limit?", content);
- case HttpStatusCode.OK:
- break;
- default:
- throw new WebApiException("Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")", content);
- }
+ var searchResult = await this.Api.SearchTweets(tab.SearchWords, tab.SearchLang, count, maxId, sinceId)
+ .ConfigureAwait(false);
if (!TabInformations.GetInstance().ContainsTab(tab))
return;
- var minimumId = this.CreatePostsFromSearchJson(content, tab, read, count, more);
+ var minimumId = this.CreatePostsFromSearchJson(searchResult, tab, read, count, more);
if (minimumId != null)
tab.OldestId = minimumId.Value;