public JsonCommentsRequestContentBase RequestComments(int limit = -1, string after = "") { string resourceParameters = ""; if (limit != -1) { resourceParameters += "limit=" + limit + "&"; } if (after != "") { resourceParameters += "after=" + after + "&"; } List <KeyValuePair <string, string> > headers = new List <KeyValuePair <string, string> >(); List <KeyValuePair <string, string> > parameters = new List <KeyValuePair <string, string> >(); Method protocol = Method.GET; headers.Add(new KeyValuePair <string, string>("User-Agent", Config.AppDetails.UserAgent)); headers.Add(new KeyValuePair <string, string>("Authorization", "bearer " + RedditAccessToken.Token)); string resource = "comments?" + parameters; JsonCommentsRequestContentBase jsonDecoded = CallAPI(protocol, headers, resource, parameters); return(jsonDecoded); }
public List <JsonCommentsRequestContentBaseDataComment> GetNewComments(int maxComments = 100) { // At any point we can't guarantee we've seen every comment in the second in which we request the data // Therefore we need to re-request that second of comments // Therefore we need to track the comments we have already scanned for that second to remove them List <JsonCommentsRequestContentBaseDataComment> untrackedComments = new List <JsonCommentsRequestContentBaseDataComment>(); bool caughtUp = false; string after = ""; while (!caughtUp && untrackedComments.Count < maxComments) { JsonCommentsRequestContentBase jsonDataRequest = RequestComments(2, after); List <JsonCommentsRequestContentBaseDataComment> strippedJsonData = StripProcessedComments(jsonDataRequest); if (strippedJsonData.Count + untrackedComments.Count > maxComments) { strippedJsonData = strippedJsonData.GetRange(0, maxComments - untrackedComments.Count); } untrackedComments.AddRange(strippedJsonData); after = jsonDataRequest.data.after; if (strippedJsonData.Count != jsonDataRequest.data.children.Count || strippedJsonData.Count == 0 || after == null) { // We have stripped some comments, so we have reached comments we have already scanned // No more requests are required caughtUp = true; } //Console.WriteLine("Found " + untrackedComments.Count + " total new comments"); //Console.WriteLine("Found " + (jsonDataRequest.data.children.Count - strippedJsonData.Count) + " comments already tracked in this scan"); } TrackedComments.InsertRange(0, untrackedComments); if (untrackedComments.Count > 0) { Data.CommentData.LastScannedUTCValue = untrackedComments[0].data.created_utc; } TrimTrackedComments(); UpdateDataFile(); return(untrackedComments); }
public JsonCommentsRequestContentBase CallAPI(Method protocol, List <KeyValuePair <string, string> > headers, string resource, List <KeyValuePair <string, string> > parameters) { CheckToken(); var request = new RestRequest(protocol); foreach (KeyValuePair <string, string> header in headers) { request.AddHeader(header.Key, header.Value); } request.Resource = resource; foreach (KeyValuePair <string, string> parameter in parameters) { request.AddParameter(parameter.Key, parameter.Value); } var response = OauthRestClient.Execute(request); JsonCommentsRequestContentBase jsonDecoded = JsonConvert.DeserializeObject <JsonCommentsRequestContentBase>(response.Content); return(jsonDecoded); }
public List <JsonCommentsRequestContentBaseDataComment> StripProcessedComments(JsonCommentsRequestContentBase jsonData) { List <JsonCommentsRequestContentBaseDataComment> unstrippedComments = new List <JsonCommentsRequestContentBaseDataComment>(); for (int i = 0; i < jsonData.data.children.Count; i++) { if (!jsonData.data.children[i].isOlderThan(Data.CommentData.LastScannedUTCValue)) { // Not older than our last tracked comment // Check if already scanned bool alreadyScanned = false; for (int x = 0; x < TrackedComments.Count; x++) { if (jsonData.data.children[i].Equals(TrackedComments[x])) { alreadyScanned = true; } } if (!alreadyScanned) { unstrippedComments.Add(jsonData.data.children[i]); } } } return(unstrippedComments); }