/// <summary> /// Given a BSTThread in the form of a JObject, parse out all its comments into a list of BSTComments /// </summary> /// <param name="BstThread"></param> /// <returns></returns> public List <BSTComment> ParseComments(JArray BstThread, List <RegexCategory <Company> > companies, List <RegexCategory <Product> > products, DateTimeOffset?until = null) { List <BSTComment> comments = new List <BSTComment>(); until = until ?? DateTimeOffset.MinValue; try { JArray jarr = BstThread[1]["data"]["children"] as JArray; foreach (JObject jobj in jarr) { try { JObject commentObj = jobj.GetValue("data") as JObject; DateTimeOffset createdAt = DateTimeOffset.FromUnixTimeSeconds((long)commentObj["created_utc"]); if (createdAt <= until) { // Only scan until we reach something we've seen before. break; } BSTComment comment = MakeComment(commentObj, companies, products); if (comment != null) { comments.Add(comment); } } catch (Exception e) { Logger.Log($"Parser failed while looking at the comments of a BST thread, likely a key wasn't present: {e.Message}. Ignoring this index and searching for others."); } } } catch (Exception e) { Logger.Log($"Parser failed while looking at the comments of a BST thread, likely a key wasn't present: {e.Message}"); } return(comments); }
/// <summary> /// Given a comment in the form a JObject, parse it into a more manageable BSTComment /// </summary> /// <param name="jobj"></param> /// <returns></returns> private BSTComment MakeComment(JObject jobj, List <RegexCategory <Company> > companies, List <RegexCategory <Product> > products) { try { string author = (string)jobj["author"]; DateTimeOffset createdAt = DateTimeOffset.FromUnixTimeSeconds((long)jobj["created_utc"]); string permalink = "https://old.reddit.com" + (string)jobj["permalink"]; string body = (string)jobj["body"]; BST bstStatus = ParseBuySellTradeStatus(body); if (bstStatus == BST.NONE) { return(null); } TransactionStatus tStatus = ParseTransactionStatus(body); if (tStatus != TransactionStatus.OPEN) { return(null); } (Currency, int)CurrencyPrice = ParseCurrencyAndPrice(body); Company company = ParseCompany(body, companies); Product product = ParseProduct(body, products); if (product != UnknownProduct) { company = product.Company; } string url = "https://reddit.com" + jobj["permalink"]; BSTComment comment = new BSTComment() { BuySellTradeStatus = bstStatus, Company = company.CompanyName, Product = product.ProductName, Currency = CurrencyPrice.Item1.Code, DatePosted = createdAt, Location = "", NumberOfReplies = 0, Price = CurrencyPrice.Item2, Seller = author, TransactionStatus = tStatus, Url = url, Text = body }; return(comment); } catch (Exception) { // Parsing error, ignore it. return(null); } }