private void ClearText_Click(object sender, RoutedEventArgs e) { DisplayTextbox.Clear(); }
private void button1_Click(object sender, EventArgs e) { DisplayTextbox displayTextBox = new DisplayTextbox(textBox2); displayTextBox.Clear(); var reddit = new Reddit(); Post giveawayPost = null; int? randomNumber = null; Regex numberPost = new Regex(@"(\d{1,6})"); try { displayTextBox.Write("Getting giveaway post..."); giveawayPost = reddit.GetPost(new Uri(textBox1.Text)); } catch (Exception ex) { displayTextBox.Write("Failed getting giveaway post", "You sure that's the right URI (alsomake sure to get the full uri from the address bar)"); return; } displayTextBox.Write("Post title: ", giveawayPost.Title, "Comment count: ", giveawayPost.CommentCount); try { displayTextBox.Write("Getting random number from random.org..."); WebRequest randomDotOrgRequest = WebRequest.Create(string.Format(RANDOM_ORG_URI, decimal.Round(randomMin.Value, 0), decimal.Round(randomMax.Value, 0))); using (WebResponse resp = randomDotOrgRequest.GetResponse()) { using (StreamReader sr = new StreamReader(resp.GetResponseStream())) { randomNumber = int.Parse(sr.ReadToEnd()); } } } catch (Exception ex) { displayTextBox.Write("Failed getting giveaway post", ex); return; } displayTextBox.Write("Random Number: ", randomNumber, "Getting winning comment...", "This might take a while..."); Dictionary <string, int> nums = giveawayPost.Comments.Where(c => c.Body != null && numberPost.IsMatch(c.Body)).ToDictionary(k => k.Shortlink, elementSelector: x => int.Parse(numberPost.Match(x.Body).Captures[0].Value)); string winningNumKey = null; int? winningNumVal = null; int? diff = null; for (int i = 0; (winningNumKey == null && winningNumVal == null) && (randomNumber + i < decimal.Round(randomMax.Value, 0) || randomNumber - i > 0); i++) { foreach (var x in nums) { if ((x.Value == randomNumber + i && randomNumber + i < decimal.Round(randomMax.Value, 0)) || (x.Value == randomNumber - i && randomNumber - i > decimal.Round(randomMin.Value, 0))) { winningNumKey = x.Key; winningNumVal = x.Value; diff = i; break; } //stop if we're out of range if (randomNumber + i > decimal.Round(randomMax.Value, 0) && randomNumber - i < decimal.Round(randomMin.Value, 0)) { break; } } } if (!string.IsNullOrEmpty(winningNumKey) && winningNumVal.HasValue) { Comment winningComment = giveawayPost.Comments.FirstOrDefault(w => w.Shortlink == winningNumKey); displayTextBox.Write("Winning comment (link): ", winningNumKey, "Winning comment (body): ", winningComment.Body, "Winning comment (commenter): ", winningComment.Author, "Diff: ", diff ); } else { displayTextBox.Write("What the heck?? not one post in the range? you sure you have that range right?"); } }
private void button1_Click(object sender, EventArgs e) { DisplayTextbox displayTextBox = new DisplayTextbox(textBox2); displayTextBox.Clear(); var reddit = new Reddit(); Post giveawayPost = null; int? randomNumber = null; Regex numberPost = new Regex(@"(\d{1,6})"); try { displayTextBox.Write("Getting giveaway post..."); giveawayPost = reddit.GetPost(new Uri(textBox1.Text)); } catch (Exception ex) { displayTextBox.Write("Failed getting giveaway post", "You sure that's the right URI (alsomake sure to get the full uri from the address bar)"); return; } displayTextBox.Write("Post title: ", giveawayPost.Title, "Comment count: ", giveawayPost.CommentCount); try { displayTextBox.Write("Getting random number from random.org..."); WebRequest randomDotOrgRequest = WebRequest.Create(string.Format(RANDOM_ORG_URI, decimal.Round(randomMin.Value, 0), decimal.Round(randomMax.Value, 0))); using (WebResponse resp = randomDotOrgRequest.GetResponse()) { using (StreamReader sr = new StreamReader(resp.GetResponseStream())) { randomNumber = int.Parse(sr.ReadToEnd()); } } } catch (Exception ex) { displayTextBox.Write("Failed getting giveaway post", ex); return; } displayTextBox.Write("Random Number: ", randomNumber, "Getting winning comment...", "This might take a while..."); Dictionary<string, int> nums = giveawayPost.Comments.Where(c => c.Body != null && numberPost.IsMatch(c.Body)).ToDictionary(k => k.Shortlink, elementSelector: x => int.Parse(numberPost.Match(x.Body).Captures[0].Value)); string winningNumKey = null; int? winningNumVal = null; int? diff = null; for (int i = 0; (winningNumKey == null && winningNumVal == null) && (randomNumber + i < decimal.Round(randomMax.Value, 0) || randomNumber - i > 0); i++) { foreach (var x in nums) { if ((x.Value == randomNumber + i && randomNumber + i < decimal.Round(randomMax.Value, 0)) || (x.Value == randomNumber - i && randomNumber - i > decimal.Round(randomMin.Value, 0))) { winningNumKey = x.Key; winningNumVal = x.Value; diff = i; break; } //stop if we're out of range if (randomNumber + i > decimal.Round(randomMax.Value, 0) && randomNumber - i < decimal.Round(randomMin.Value, 0)) break; } } if (!string.IsNullOrEmpty(winningNumKey) && winningNumVal.HasValue) { Comment winningComment = giveawayPost.Comments.FirstOrDefault(w => w.Shortlink == winningNumKey); displayTextBox.Write("Winning comment (link): ", winningNumKey, "Winning comment (body): ", winningComment.Body, "Winning comment (commenter): ", winningComment.Author, "Diff: ", diff ); } else { displayTextBox.Write("What the heck?? not one post in the range? you sure you have that range right?"); } }