Created
December 22, 2017 15:40
-
-
Save davlgd/266d6232d645dc17cda98f246c28baeb to your computer and use it in GitHub Desktop.
AlticeStockChecker - Version finale
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Drawing; | |
using System.Globalization; | |
using System.Net; | |
using System.Text.RegularExpressions; | |
using System.Windows.Forms; | |
namespace AlticeStockChecker | |
{ | |
public partial class Form1 : Form | |
{ | |
float lastStock = 0.0f; | |
public Form1() | |
{ | |
InitializeComponent(); | |
this.Text = Application.ProductName; | |
GetAndCleanStockValue(); | |
} | |
private void GetAndCleanStockValue() | |
{ | |
try | |
{ | |
string webPageCode = string.Empty; | |
webPageCode = GetHTMLCode("https://finance.yahoo.com/quote/ATC.AS?p=ATC.AS"); | |
float stock = ExtractFloatValue(webPageCode); | |
UpdateLabelWithColor(stock); | |
} | |
catch (System.Exception) | |
{ | |
lblStock.Text = "Erreur"; | |
} | |
} | |
private string GetHTMLCode(string url) | |
{ | |
string webPageCode = string.Empty; | |
using (WebClient wc = new WebClient()) | |
{ | |
webPageCode = wc.DownloadString(url); | |
} | |
return webPageCode; | |
} | |
private float ExtractFloatValue(string webPageCode) | |
{ | |
string rawStock = Regex.Match(webPageCode, | |
"currentPrice\":{\"raw\":(.+?),", | |
RegexOptions.Singleline).Groups[1].Value; | |
float stock = 0.0f; | |
float.TryParse(rawStock, | |
NumberStyles.Number, | |
CultureInfo.CreateSpecificCulture("en-US"), | |
out stock); | |
return stock; | |
} | |
private void UpdateLabelWithColor(float stock) | |
{ | |
if (stock > lastStock && lastStock != 0.0) | |
{ | |
lblStock.ForeColor = Color.Green; | |
} | |
else if (stock < lastStock && lastStock != 0.0) | |
{ | |
lblStock.ForeColor = Color.Red; | |
} | |
else | |
{ | |
lblStock.ForeColor = Color.Black; | |
} | |
lastStock = stock; | |
string finalStock = string.Format("{0} euros", stock.ToString()); | |
lblStock.Text = finalStock; | |
} | |
private void timer1_Tick(object sender, System.EventArgs e) | |
{ | |
GetAndCleanStockValue(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment