adding urls works, wahooooo

This commit is contained in:
Lynne Megido 2020-09-02 13:21:32 +10:00
parent 8d447109ad
commit 80488d7c71
Signed by: lynnesbian
GPG key ID: F0A184B5213D9F90
3 changed files with 41 additions and 13 deletions

View file

@ -1,5 +1,6 @@
using System; using System;
using Gtk; using Gtk;
using System.Text.RegularExpressions;
namespace Buypeeb { namespace Buypeeb {
class BuypeebApp { class BuypeebApp {
@ -16,5 +17,16 @@ namespace Buypeeb {
win.Show(); win.Show();
Application.Run(); 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;
}
} }
} }

View file

@ -32,22 +32,18 @@ namespace Buypeeb {
class MainWindow : Window { class MainWindow : Window {
private ListStore Items; private ListStore items;
private Settings Settings; private Settings settings;
public MainWindow() : this(new Builder("main.glade")) { } public MainWindow() : this(new Builder("main.glade")) { }
private MainWindow(Builder builder) : base(builder.GetObject("wndMain").Handle) { private MainWindow(Builder builder) : base(builder.GetObject("wndMain").Handle) {
this.Settings = new Settings(); this.settings = new Settings();
this.Title = "Buypeeb"; this.Title = "Buypeeb";
builder.Autoconnect(this); builder.Autoconnect(this);
this.Items = (ListStore)builder.GetObject("ListItems"); this.items = (ListStore)builder.GetObject("ListItems");
this.Items.Clear(); this.RenderList();
foreach (KeyValuePair<string, Listing> entry in Settings.watchlist) { foreach (object[] row in this.items) {
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) {
Console.WriteLine(row[(int)ItemColumns.Name]); Console.WriteLine(row[(int)ItemColumns.Name]);
} }
DeleteEvent += Window_Shutdown; DeleteEvent += Window_Shutdown;
@ -82,6 +78,14 @@ namespace Buypeeb {
} }
private void RenderList() {
this.items.Clear();
foreach (KeyValuePair<string, Listing> 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 // button handlers
private void ButtonAddClicked(object sender, EventArgs a) { private void ButtonAddClicked(object sender, EventArgs a) {
@ -96,6 +100,8 @@ namespace Buypeeb {
Regex rx = new Regex(@"^http.+yahoo.+"); Regex rx = new Regex(@"^http.+yahoo.+");
if (rx.IsMatch(url)) { if (rx.IsMatch(url)) {
Console.WriteLine("{0} will be added", url); Console.WriteLine("{0} will be added", url);
this.settings.Watch(url, name);
this.RenderList();
} }
else { else {
Console.WriteLine("{0} is an invalid url", url); Console.WriteLine("{0} is an invalid url", url);

View file

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
namespace Buypeeb { namespace Buypeeb {
@ -6,9 +7,18 @@ namespace Buypeeb {
public Settings() { public Settings() {
this.watchlist = new Dictionary<string, Listing>(); this.watchlist = new Dictionary<string, Listing>();
string id = "k12345"; this.Watch("https://buypeeb.biz/whatever/k12345", "my thingy");
this.watchlist.Add(id, new Listing("https://yahoo.jp", id, "my thingy")); this.watchlist["k12345"].Update();
this.watchlist[id].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<string, Listing> entry in this.watchlist) {
Console.WriteLine("{0} - {1}", entry.Value.name, entry.Value.price);
}
} }
} }
} }