diff --git a/BuypeebApp.cs b/BuypeebApp.cs index 5e55b72..438cc70 100644 --- a/BuypeebApp.cs +++ b/BuypeebApp.cs @@ -1,5 +1,6 @@ using System; using Gtk; +using System.Text.RegularExpressions; namespace Buypeeb { class BuypeebApp { @@ -16,5 +17,16 @@ namespace Buypeeb { win.Show(); Application.Run(); } + + public static string IDFromURL(string url) { + // TODO: handle invalid URLs better, or at all really + Regex rx = new Regex(@"^([^?]+)"); + url = rx.Match(url).Groups[1].Value; // remove query params (if any) + + rx = new Regex(@".+\/(.+?)/?$"); + string id = rx.Match(url).Groups[1].Value; // extracts "k12345" from "https://buypeeb.biz/whatever/k12345/" + + return id; + } } } diff --git a/MainWindow.cs b/MainWindow.cs index 26f351b..7a03379 100755 --- a/MainWindow.cs +++ b/MainWindow.cs @@ -32,22 +32,18 @@ namespace Buypeeb { class MainWindow : Window { - private ListStore Items; - private Settings Settings; + private ListStore items; + private Settings settings; public MainWindow() : this(new Builder("main.glade")) { } private MainWindow(Builder builder) : base(builder.GetObject("wndMain").Handle) { - this.Settings = new Settings(); + this.settings = new Settings(); this.Title = "Buypeeb"; builder.Autoconnect(this); - this.Items = (ListStore)builder.GetObject("ListItems"); - this.Items.Clear(); - foreach (KeyValuePair entry in Settings.watchlist) { - string[] values = new[] { entry.Value.name, entry.Value.PriceJPY(), entry.Value.PriceAUD(), "whenever", entry.Value.id }; - this.Items.AppendValues(values); - } - foreach (object[] row in this.Items) { + this.items = (ListStore)builder.GetObject("ListItems"); + this.RenderList(); + foreach (object[] row in this.items) { Console.WriteLine(row[(int)ItemColumns.Name]); } DeleteEvent += Window_Shutdown; @@ -82,6 +78,14 @@ namespace Buypeeb { } + private void RenderList() { + this.items.Clear(); + foreach (KeyValuePair entry in settings.watchlist) { + string[] values = new[] { entry.Value.name, entry.Value.PriceJPY(), entry.Value.PriceAUD(), "whenever", entry.Value.id }; + this.items.AppendValues(values); + } + } + // button handlers private void ButtonAddClicked(object sender, EventArgs a) { @@ -96,6 +100,8 @@ namespace Buypeeb { Regex rx = new Regex(@"^http.+yahoo.+"); if (rx.IsMatch(url)) { Console.WriteLine("{0} will be added", url); + this.settings.Watch(url, name); + this.RenderList(); } else { Console.WriteLine("{0} is an invalid url", url); diff --git a/Settings.cs b/Settings.cs index 85347fc..8f3b7f3 100644 --- a/Settings.cs +++ b/Settings.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; namespace Buypeeb { @@ -6,9 +7,18 @@ namespace Buypeeb { public Settings() { this.watchlist = new Dictionary(); - string id = "k12345"; - this.watchlist.Add(id, new Listing("https://yahoo.jp", id, "my thingy")); - this.watchlist[id].Update(); + this.Watch("https://buypeeb.biz/whatever/k12345", "my thingy"); + this.watchlist["k12345"].Update(); + } + + public void Watch(string url, string name) { + string id = BuypeebApp.IDFromURL(url); + Console.WriteLine(id); + this.watchlist[id] = new Listing(url, id, name); + + foreach (KeyValuePair entry in this.watchlist) { + Console.WriteLine("{0} - {1}", entry.Value.name, entry.Value.price); + } } } }