/// <summary> /// Saves status of toast notifications to JSON object. /// </summary> private void tbtnToggleToast_Click(object sender, RoutedEventArgs e) { //Initialize helper class rcHelper rc = new rcHelper(); //Handles toggling of toast notifications through rcHelper ToggleToast method. try { if ((sender as ToggleButton).IsChecked ?? false) { rc.ToggleToast("yes"); MessageBox.Show("Toast notifications are now enabled"); } else { rc.ToggleToast("no"); MessageBox.Show("Toast notifications are now disabled"); } } //Generic exception handling catch (Exception ex) { rc.DebugLog(ex); Environment.Exit(1); } }
/// <summary> /// Confirms contents of email and email password boxes and encrypts/writes contents to JSON object using <see cref="NewEmail(string, string)"/> if valid. /// </summary> private void btnSubmitEmail_click(object sender, RoutedEventArgs e) { //Initialize helper class rcHelper rc = new rcHelper(); try { //Initialize all variables from email and ePass fields. #region variable_Initialization //Initialize email and password variables string email = null; string ePass = null; //Check for valid input in txtEmail textbox if (txtEmail.Text != null && rc.IsEmailValid(txtEmail.Text) == true) { email = txtEmail.Text; } //If invalid, display error & return else { MessageBox.Show("Please enter a valid email address"); return; } //Ensure password exists if (pwdEmail.Password.Count() > 0) { ePass = pwdEmail.Password; } //If null or empty, display error & return else { MessageBox.Show("Please input your email password."); return; } #endregion //Tests email credentials & writes them to JSON object if valid. if (email != null && ePass != null) { bool eLog = rc.NewEmail(email, ePass); if (eLog == false) { MessageBox.Show("Your email address or password was incorrect."); return; } MessageBox.Show("Email credentials accepted & saved"); if (rc.ApplicationReady() == true) { MessageBox.Show("You may now run RedditCrawler!"); } } } //Generic exception handling catch (Exception ex) { rc.DebugLog(ex); } }
public void TestInvalidEmailFormat() { rcHelper rc = new rcHelper(); string email = "cd2"; bool b = rc.IsEmailValid(email); Assert.AreEqual(b, false); }
public void TestValidEmailFormat() { rcHelper rc = new rcHelper(); string email = "*****@*****.**"; bool b = rc.IsEmailValid(email); Assert.AreEqual(b, true); }
public void TestPasswordEncoding() { rcHelper rc = new rcHelper(); string pass = "******"; string ePass = rc.EncodePassword(pass); ePass = rc.DecodePassword(ePass); Assert.AreEqual(ePass, pass); }
/// <summary> /// Removes email from JSON object to disable email notifications /// </summary> private void btnDisableEmail_Click(object sender, RoutedEventArgs e) { rcHelper rc = new rcHelper(); RCDetails json = rc.GetJson(jsonFilePath); json.email = null; json.ePass = null; rc.WriteToFile(json); MessageBox.Show("Your email has been removed. \nYou will only receive toast notifications for new posts (if enabled)"); }
public void TestJsonCreationAndExistence() { rcHelper rc = new rcHelper(); List <string> sc = new List <string>(); RCDetails rcd = new RCDetails("rLog", "rPass", "email", "ePass", "sub", "toast", sc, sc); rc.WriteToFile(rcd); bool b = File.Exists(Directory.GetCurrentDirectory().ToString() + "/rcData.json"); File.Delete(Directory.GetCurrentDirectory().ToString() + "/rcData.json"); Assert.AreEqual(b, true); }
//Region containing all button events #region buttonClicks /// <summary> /// Confirms contents of subreddit textbox and writes contents to JSON object using <see cref="NewSub(string)"/> if valid. /// </summary> private void btnSubmitSub_click(object sender, RoutedEventArgs e) { rcHelper rc = new rcHelper(); try { //Initialize all variables from subreddit field #region variable_Initialization //Initialize subreddit variable string sub = null; //Check for valid input in txtSubreddit textbox if (txtSubreddit.Text != null && txtSubreddit.Text.Count() > 3 && txtSubreddit.Text.StartsWith("/r/")) { sub = txtSubreddit.Text; } //Display error message if subreddit textbox is empty or contains improperly formatted text. else { MessageBox.Show("Please input the subreddit you'd like to monitor. \nMake sure it begins with \"/r/\""); return; } #endregion //Write details of text boxes to JSON object, displaying message if it was accepted. if (sub != null) { rc.NewSub(sub); MessageBox.Show("Subreddit accepted"); if (rc.ApplicationReady() == true) { MessageBox.Show("You may now run RedditCrawler!"); } } } //Generic exception handling. catch (Exception ex) { rc.DebugLog(ex); } }
static void Main(string[] args) { //Initialize helper classes rcHelper rc = new rcHelper(); Program p = new Program(); //Set current directory to match up with rcConfig's string s = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\RedditCrawler"; if (!Directory.Exists(s)) { Directory.CreateDirectory(s); } Directory.SetCurrentDirectory(s); //Create shortcut for application for toast notification's access to Windows try { ShortCutCreator.TryCreateShortcut(appID, "RedditCrawler"); } catch (Exception ex) { rc.DebugLog(ex); } //Validates login, logs failures. RCDetails json = new RCDetails(); if (File.Exists(Path.Combine(Directory.GetCurrentDirectory().ToString() + jsonFilePath))) { json = rc.GetJson(jsonFilePath); } try { p.Listen().Wait(); } catch (Exception ex) { rc.DebugLog(ex); } }
public MainWindow() { InitializeComponent(); //Set working directory to match RedditCrawler's string s = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\RedditCrawler"); if (!Directory.Exists(s)) { Directory.CreateDirectory(s); } Directory.SetCurrentDirectory(s); //Initialize helper classes rcHelper rc = new rcHelper(); RCDetails data = new RCDetails(); //Initial code for populating textboxes with existing criteria #region populate fields try { //Toggle toast toggle button and field auto-fill based on existing criteria if (File.Exists(System.IO.Path.Combine(Directory.GetCurrentDirectory().ToString() + jsonFilePath))) { var datRaw = File.ReadAllText(System.IO.Path.Combine(Directory.GetCurrentDirectory().ToString() + jsonFilePath)); data = JsonConvert.DeserializeObject <RCDetails>(datRaw); if (data.toast == "yes") { tbtnToast.IsChecked = true; } if (data.sub != null) { txtSubreddit.Text = data.sub; } if (data.email != null) { txtEmail.Text = rc.DecodePassword(data.email); } if (data.ePass != null) { pwdEmail.Password = rc.DecodePassword(data.ePass); } if (data.searchCriteria.Count > 0) { foreach (string s2 in data.searchCriteria) { //Ensures no new line is created at end of list if (data.searchCriteria.IndexOf(s2) != data.searchCriteria.Count - 1) { rtfSearchTerms.AppendText(s2 + "\n"); } else { rtfSearchTerms.AppendText(s2); } } } } } //Generic exception handling catch (Exception ex) { rc.DebugLog(ex); } #endregion }
/// <summary> /// Confirms contents of search criteria box and writes contents /// to JSON object after conversion to a list /// </summary> private void btnSubmitSearch_Click(object sender, RoutedEventArgs e) { //Initialize helper class rcHelper rc = new rcHelper(); try { //Initialize all search-based variables. #region variable_Initialization //Initialize TextRange TextRange searchCriteria = null; //analyzes entries in search terms rich textbox, confirms there is input present. var start = rtfSearchTerms.Document.ContentStart; var end = rtfSearchTerms.Document.ContentEnd; int difference = start.GetOffsetToPosition(end); if (difference > 1) { searchCriteria = new TextRange(start, end); } //If input is not present or is too small, user is directed to input terms in the RTF box else { MessageBox.Show("Please input the terms you'd like to listen for. \nBe sure to put each term on a new line"); return; } #endregion //Only continues if searchCriteria is not null if (searchCriteria != null) { try { //Retrieves text from searchCriteria TextRange as a string string sl = searchCriteria.Text.ToString(); //Separates searchCriteria string sl into a list of strings separated by line breaks. List <string> scList = sl.Split(new[] { Environment.NewLine }, StringSplitOptions.None).ToList(); //Removes empty and NewLine entries from list for (int i = 0; i < scList.Count; i++) { if (scList[i] == "" || scList[i] == null || scList[i] == Environment.NewLine) { scList.RemoveAt(i); } } //Runs filtered list into NewSearchCriteria method. rc.NewSearchCriteria(scList); } //Generic exception handling. catch (Exception ex) { MessageBox.Show(ex.Message); rc.DebugLog(ex); Environment.Exit(1); } MessageBox.Show("Criteria successfully updated."); if (rc.ApplicationReady() == true) { MessageBox.Show("You may now run RedditCrawler!"); } } } catch (Exception ex) { MessageBox.Show(ex.Message); rc.DebugLog(ex); Environment.Exit(1); } }
/// <summary> /// Takes user reddit user name and password as input, confirms existence of all relevant JSON contents, then retrieves 15 posts using <see cref="GetPosts(string)"/> /// Once these posts have been retrieved, the method checks them against duplicates and matches from the JSON as well, /// then uses <see cref="NotifyUser(string)"/> to notify the user of any matches. After this, all variables are cleared, the thread sleeps for /// 180,000 ticks, and runs again. /// </summary> private async Task Listen() { //Initialize helper classes rcHelper rc = new rcHelper(); rcConnectivity rcon = new rcConnectivity(); RCDetails json = new RCDetails(); //Retrieve JSON object from local storage if (File.Exists(Path.Combine(Directory.GetCurrentDirectory().ToString() + jsonFilePath))) { json = rc.GetJson(jsonFilePath); } //If JSON is not found, throw exception indicating it needs to be initialized. else { Exception ex = new Exception("Please ensure you have properly configured your information in rcConfig."); rc.DebugLog(ex); Environment.Exit(0); } //Run remainder of method in a loop every 180,000 ticks.. while (true) { try { //Checks for valid entries in relevant text files, performs DebugLog() and exits environment on failure. #region criteriaCheck List <string> lstSearchInput = json.searchCriteria; List <string> lstDuplicateList = new List <string>(); if (json.dupResults != null) { lstDuplicateList = json.dupResults; } //Throw exception indicating that user needs to input search terms into rcConfig if (lstSearchInput.Count < 1) { Exception ex = new Exception("You must ensure search terms have been set up in rcConfig."); rc.DebugLog(ex); System.Environment.Exit(0); } //Retrieve status on all variables bool toastStatus = false; string sub = null; bool subEx, emailEx; subEx = emailEx = false; if (json.toast == "yes") { toastStatus = true; } if (json.sub != null && json.sub.Count() > 3) { subEx = true; sub = json.sub; } if (json.email != null && json.ePass != null) { emailEx = true; } //Exit environment and log error if any variable check fails if (subEx == false) { rc.DebugLog(new Exception("Please check your subreddit existence & formatting in rcConfig.")); Environment.Exit(0); } #endregion //gets list of 15 most recent posts/URLs from designated sub. var getLists = rcon.GetPosts(sub); List <string> lstResultList = getLists.Item1; List <string> lstUrl = getLists.Item2; //Run lists through the NotificationList method, trimming duplicates and non-matches. var passedLists = rc.NotificationList(lstResultList, lstUrl, lstSearchInput, lstDuplicateList); List <string> lstPassedList = passedLists.Item1; List <string> lstPassedUrls = passedLists.Item2; //Now that the list has been trimmed, notify user of all results if (lstPassedList.Count > 0) { if (lstPassedList.Count() == 0) { Console.WriteLine("No new posts were retrieved."); } else { foreach (string s in lstPassedList) { Console.WriteLine("Sent: " + s); lstDuplicateList.Add(s); } if (emailEx == true) { rcon.NotifyUser(lstPassedList, lstPassedUrls); } } for (int i = 0; i < lstPassedList.Count(); i++) { if (toastStatus == true) { ShowTextToast(appID, "New Reddit Post!" + lstPassedList[i], lstPassedUrls[i]); await(Task.Delay(10000)); } } json.dupResults = lstDuplicateList; rc.WriteToFile(json); } //Clear all lists, sleep, and repeat lstResultList.Clear(); lstPassedList.Clear(); lstDuplicateList.Clear(); lstSearchInput.Clear(); await Task.Delay(180000); } //Generic unhandled exception catch catch (Exception ex) { rc.DebugLog(ex); System.Environment.Exit(0); } } }