/// <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); }
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> /// 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); } } }