public static Decimal TickerFromAPI(FinProvider finProvider, string name) { Decimal current = 0; try { if (name != null && name.Length > 0) { var url = finProvider.Url.Replace( finProvider.Replace, name); ServicePointManager.Expect100Continue = true; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); WebResponse response = request.GetResponse(); using (Stream dataStream = response.GetResponseStream()) { StreamReader reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); //Match @"<meta name=""price"" content=""(\d+,?\d+\.\d+)"">" //@"lastsale"">(\d+,?\d+\.\d+).*" Match match = Regex.Match(responseFromServer, @"lastsale"" .*>(\d+,?\d+\.\d+)", RegexOptions.IgnoreCase); if (match.Success) { Decimal.TryParse(match.Groups[1].Value, out current); } } response.Close(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } return(current); }
static void Main(string[] args) { TwilioConf tw = new TwilioConf() { Token = ConfigurationManager.AppSettings["SMSKeyToken"], From = ConfigurationManager.AppSettings["SMSFrom"], Account = ConfigurationManager.AppSettings["SMSAccount"] }; FinProvider fin = new FinProvider() { Name = ConfigurationManager.AppSettings["FinProviderName"], Match = ConfigurationManager.AppSettings["FinProviderMatch"], Replace = ConfigurationManager.AppSettings["FinProviderReplace"], Url = ConfigurationManager.AppSettings["FinProviderURL"] }; Console.WriteLine("Starting TickerUpdater cycle"); TickerContext _db = new TickerContext(ConfigurationManager.ConnectionStrings["TickerSQLConnection"].ConnectionString); // Initialize the product database. Database.SetInitializer(new TickerDatabaseInit()); while (true) { try { foreach (Ticker ticker in _db.Tickers) { ticker.LastRead = DateTime.UtcNow; var currRead = HTTPhandy.TickerFromAPI(fin, ticker.TickerName); ticker.Current = currRead > 0 ? currRead: ticker.Current; Console.WriteLine(ticker.TickerName + ": " + ticker.Current); } _db.SaveChanges(); } catch (Exception ex) { Console.WriteLine(ex.Message); } try { var results = from t1 in _db.Tickers join t2 in _db.TickerSMSSettings on t1.TickerName equals t2.TickerName where (t1.Current <= t2.Low) || (t1.Current >= t2.High) select new { t1.TickerName, t1.Current, t2.Mobile, t2.Low, t2.High, t1.LastRead }; TickerSMSSettings found; foreach (var smsSettings in results.ToList()) { Console.WriteLine(smsSettings.TickerName + " " + smsSettings.Mobile + " " + smsSettings.Current); found = _db.TickerSMSSettings.Where(s => s.Mobile == smsSettings.Mobile) .FirstOrDefault <TickerSMSSettings>(); found.TickerDate = smsSettings.LastRead; HTTPhandy.SendSMS(tw, found, smsSettings.Current); _db.TickerSMSSettings.Remove(found); } _db.SaveChanges(); } catch (Exception ex) { Console.WriteLine(ex.Message); } Thread.Sleep(5000); } }