static private void ChangeListener() { while (true) { if (HttpParser.ResponseFailed == false) { using (ispend_icryEntities db = new ispend_icryEntities()) { int upd_count = db.balances.Count(x => x.requires_notification == true); if (upd_count > 0) { Mailer.SendEmail(); foreach (balance b in db.balances) { b.requires_notification = false; db.Entry(b).State = System.Data.Entity.EntityState.Modified; } db.SaveChanges(); } } } else { Mailer.SendEmail(true); } Thread.Sleep(600000); } }
public static void SendEmail(bool?failed = false) { MailAddress fromAddress = new MailAddress("*****@*****.**", "iSpendiCry Administrator"); MailAddress toAddress = new MailAddress("*****@*****.**", "to iSpend_iCry User"); string fromPass = ConfigurationManager.AppSettings["GmailPass"]; string subject = "Accounts Updated"; string body_part = System.IO.File.ReadAllText("Html\\body.html"); if (failed == false) { using (ispend_icryEntities db = new ispend_icryEntities()) { string update_Date = db.accounts.First().update_timestamp.ToString(); body_part = body_part.Replace("<<**DATE**>>", update_Date); var joined = from b in db.balances join a in db.accounts on b.account_id equals a.account_id select new { a.display_name, b.available, b.current }; string accs_html = ""; foreach (var detail in joined) { accs_html += System.IO.File.ReadAllText("Html\\account.html"); // body += "\n - " + detail.display_name + "Current: " + detail.current + ", Available : " + "<bold>" + detail.available + "</bold>"; accs_html = accs_html.Replace("<<**ACCOUNT_NAME**>>", detail.display_name) .Replace("<<**CURRENT**>>", detail.current.ToString()) .Replace("<<**AVAILABLE**>>", detail.available.ToString()); accs_html = accs_html.Replace("<<**BG_COLOR**>>", (detail.available > 0M ? "#009900" : "#9C010F")); } accs_html += System.IO.File.ReadAllText("Html\\total.html"); accs_html = accs_html.Replace("<<**TOTAL_AVAILABLE**>>", joined.Sum(x => x.available).ToString()); body_part = body_part.Replace("<<**ACCOUNTS_HTML**>>", accs_html); } } else { subject = "Update Failed"; body_part = "UPDATE FAILED"; }; var smtp = new SmtpClient { Host = "smtp.gmail.com", Port = 587, EnableSsl = true, DeliveryMethod = SmtpDeliveryMethod.Network, Credentials = new NetworkCredential(fromAddress.Address, fromPass), Timeout = 20000 }; using (var message = new MailMessage(fromAddress, toAddress) { Subject = subject, Body = body_part, IsBodyHtml = true }) { smtp.Send(message); } }
private static void Main(string[] args) { while (true) { string param_from = ""; string param_to = ""; if (ListenerThread == null) { ListenerThread = new Thread(ChangeListener); ListenerThread.Start(); } if (args.Length < 2) { using (ispend_icryEntities db = new ispend_icryEntities()) { param_from = db.transactions.Max(x => x.timestamp).ToString("yyyy-MM-dd"); }; param_to = DateTime.Now.ToString("yyyy-MM-dd"); } else if (args.Length == 2) { param_from = args[0]; param_to = args[1]; } Accounts Acc = new Accounts(); Balances Bal = new Balances(); Acc = (Accounts)HttpParser.GetAPIResponse("accounts", null, HttpParser.ResponseType.Account); foreach (AccountsResult ac in Acc.Results) { using (ispend_icryEntities db = new ispend_icryEntities()) { if (db.accounts.Where(x => x.account_id == ac.AccountId).ToList().Count == 0) { db.accounts.Add(new account() { account_id = ac.AccountId, account_number_iban = ac.AccountNumber.Iban, account_number_number = ac.AccountNumber.Number, account_number_sort_code = ac.AccountNumber.SortCode, account_number_swift_bic = ac.AccountNumber.SwiftBic, account_type = ac.AccountType, currency = ac.Currency, display_name = ac.DisplayName, logo_uri = ac.Provider.LogoUri, provider_display_name = ac.Provider.DisplayName, provider_id = ac.Provider.ProviderId, update_timestamp = ac.UpdateTimestamp }); } else { account tmp_acc = db.accounts.Where(x => x.account_id == ac.AccountId).FirstOrDefault(); tmp_acc.update_timestamp = ac.UpdateTimestamp; db.Entry(tmp_acc).State = System.Data.Entity.EntityState.Modified; } db.SaveChanges(); } } using (ispend_icryEntities db = new ispend_icryEntities()) { foreach (account ac in db.accounts) { Bal = (Balances)HttpParser.GetAPIResponse("balance", new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("acc_id", ac.account_id) }, HttpParser.ResponseType.Balance); foreach (BalancesResult b in Bal.Results) { if (db.balances.Where(x => x.account_id == ac.account_id).ToList().Count == 0) { db.balances.Add(new balance { account_id = ac.account_id, available = Convert.ToDecimal(b.Available), curency = b.Currency, current = Convert.ToDecimal(b.Current), update_timestamp = b.UpdateTimestamp }); } else { balance tmp_bal = db.balances.Where(x => x.account_id == ac.account_id).FirstOrDefault(); tmp_bal.requires_notification = (Convert.ToDecimal(b.Available) == tmp_bal.available ? false : true); tmp_bal.available = Convert.ToDecimal(b.Available); tmp_bal.current = Convert.ToDecimal(b.Current); tmp_bal.update_timestamp = b.UpdateTimestamp; db.Entry(tmp_bal).State = System.Data.Entity.EntityState.Modified; } } } db.SaveChanges(); } using (ispend_icryEntities db = new ispend_icryEntities()) { foreach (account ac in db.accounts) { Transactions Transacts = (Transactions)HttpParser.GetAPIResponse("transactions", new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("acc_id", ac.account_id), new KeyValuePair <string, string>("from", param_from), new KeyValuePair <string, string>("to", param_to) }, HttpParser.ResponseType.Transaction); foreach (TransactionsResult T in Transacts.Results) { if (db.transactions.Where(x => x.transaction_id == T.TransactionId && x.timestamp == T.Timestamp).ToList().Count == 0) { db.transactions.Add(new transaction { transaction_id = T.TransactionId, account_id = ac.account_id, timestamp = T.Timestamp, description = T.Description, transaction_type = T.TransactionType, transaction_category = T.TransactionCategory, amount = Convert.ToDecimal(T.Amount), currency = T.Currency, meta_provider_transaction_category = (T.Meta != null ? T.Meta.ProviderTransactionCategory : null), meta_transaction_reference = (T.Meta != null ? T.Meta.TransactionReference : null) }); } } } db.SaveChanges(); } Thread.Sleep(4680000); } }