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 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;
}
}
}

View file

@ -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<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);
}
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<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
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);

View file

@ -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, Listing>();
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<string, Listing> entry in this.watchlist) {
Console.WriteLine("{0} - {1}", entry.Value.name, entry.Value.price);
}
}
}
}