public Counts GetCounts(string Query, string bucket = "day", DateTime?fromDateTime = null, DateTime?toDateTime = null) { var endPoint = GetEndPoint(Search_Endpoint.Counts); try { ErrorState = false; string content = ""; var searchData = new SearchPost(); searchData.query = Query; searchData.maxResults = 0; searchData.bucket = bucket; if (fromDateTime != null) { searchData.fromDate = fromDateTime.GetValueOrDefault(); } if (toDateTime != null) { searchData.toDate = toDateTime.GetValueOrDefault(); } if (hasMore) { searchData.next = nextToken; } var postSearch = BuildQueryJson(searchData); QueryJson = postSearch; var resultCode = Restful.GetRestResponse("Post", endPoint, Username, Password, out content, postSearch); if (resultCode == HttpStatusCode.OK) { var searchResult = JsonConvert.DeserializeObject <Counts>(content.ToString()); nextToken = searchResult.next; if (searchResult.results != null) { return(searchResult); } return(null); } else { ErrorMessage = "Invalid HTTP Response code." + resultCode + " " + content; ErrorState = true; return(null); } } catch (Exception ex) { ErrorState = true; ErrorMessage = ex.Message; return(null); } }
public List <Activity> GetResults(string Query, DateTime?fromDateTime = null, DateTime?toDateTime = null, int maxResults = 500) { var endPoint = @"https://search.gnip.com/accounts/" + AccountName + "/search/" + StreamName + ".json"; try { ErrorState = false; string content = ""; var searchData = new SearchPost(); searchData.query = Query; searchData.maxResults = maxResults; searchData.publisher = "twitter"; searchData.fromDate = fromDateTime.GetValueOrDefault(); searchData.toDate = toDateTime.GetValueOrDefault(); if (hasMore) { searchData.next = nextToken; } searchData.bucket = null; var postSearch = BuildQueryJson(searchData); QueryJson = postSearch; var resultCode = Restful.GetRestResponse("Post", endPoint, Username, Password, out content, postSearch); if (resultCode == HttpStatusCode.OK) { var searchResult = JsonConvert.DeserializeObject <Results>(content.ToString()); nextToken = searchResult.next; return(searchResult.results != null?searchResult.results.ToList() : null); } else { ErrorMessage = "Invalid HTTP Response code." + resultCode + " " + content; ErrorState = true; return(null); } } catch (Exception ex) { ErrorState = true; ErrorMessage = ex.Message; return(null); } }
private static string BuildQueryJson(SearchPost searchPost) { // custom serializer to prevent the escaping of quotes on propertynames var sb = new StringBuilder(); var sw = new StringWriter(sb); using (JsonWriter jw = new JsonTextWriter(sw)) { jw.WriteStartObject(); jw.WritePropertyName("query", false); jw.WriteValue(searchPost.query); jw.WritePropertyName("publisher", false); jw.WriteValue(searchPost.publisher); if (searchPost.maxResults > 0) { jw.WritePropertyName("maxResults", false); jw.WriteValue(searchPost.maxResults.ToString()); } if (searchPost.fromDate > DateTime.Parse("1/1/0001")) { jw.WritePropertyName("fromDate", false); jw.WriteValue(AsUtcString(searchPost.fromDate)); } if (searchPost.toDate > DateTime.Parse("1/1/0001")) { jw.WritePropertyName("toDate", false); jw.WriteValue(AsUtcString(searchPost.toDate)); } if (searchPost.next != null) { jw.WritePropertyName("next", false); jw.WriteValue(searchPost.next); } if (searchPost.bucket != null) { jw.WritePropertyName("bucket", false); jw.WriteValue(searchPost.bucket); } jw.WriteEndObject(); return(sb.ToString()); } }