diff --git a/MainWindow.cs b/MainWindow.cs index 4c83136..83b6f4d 100755 --- a/MainWindow.cs +++ b/MainWindow.cs @@ -24,14 +24,16 @@ using System.Text.Json; using System.Threading; using System.Threading.Tasks; using System.Net; +using System.Linq; using System.Diagnostics; +using CsvHelper; using Gtk; namespace Buypeeb { - class MainWindow : Window { private string location; + private JsonSerializerOptions jsonOptions; private ListStore items; private Settings settings; @@ -66,6 +68,11 @@ namespace Buypeeb { public MainWindow() : this(new Builder("main.glade")) { } private MainWindow(Builder builder) : base(builder.GetObject("wndMain").Handle) { + + this.jsonOptions = new JsonSerializerOptions { + Encoder = System.Text.Encodings.Web.JavaScriptEncoder.Create(System.Text.Unicode.UnicodeRanges.All) + }; + if (Environment.OSVersion.Platform == PlatformID.Win32NT) { // C:\Users\Beebus\AppData\Roaming\Lynnear Software\buypeeb this.location = System.IO.Path.Combine(Environment.ExpandEnvironmentVariables("%APPDATA%"), "Lynnear Software", "buypeeb"); @@ -153,7 +160,7 @@ namespace Buypeeb { } private void SaveSettings() { - string j = JsonSerializer.Serialize(this.settings); + string j = JsonSerializer.Serialize(this.settings, this.jsonOptions); string p = System.IO.Path.Combine(this.location, "userdata.json"); Console.WriteLine(j); if (!Directory.Exists(this.location)) { @@ -181,8 +188,9 @@ namespace Buypeeb { using (WebClient client = new WebClient()) { // TODO: download should have timeout - item.Update(client.DownloadString(item.url)); - // item.Update(File.ReadAllText("yahoo.html")); + // item.Update(client.DownloadString(item.url)); + Thread.Sleep(5000); + item.Update(File.ReadAllText("yahoo.html")); } Gtk.Application.Invoke(delegate { @@ -272,11 +280,11 @@ namespace Buypeeb { var info = new Dictionary { { "Name", item.name }, { "YahooName", item.originalName }, - { "Price", item.PriceJPY() }, - { "PriceAUD", item.PriceAUD() }, + { "Price", item.priceJPY }, + { "PriceAUD", item.priceAUD }, { "Ending", "..." }, { "Bids", $"{item.bids}" }, - { "BuyItNow", item.winPrice == 0 ? "No" : $"¥{item.PriceJPY(true)} (${item.PriceAUD(true)})" }, + { "BuyItNow", item.winPrice == 0 ? "No" : $"{item.winPriceJPY} ({item.winPriceAUD})" }, { "AutoExtension", item.autoExtension ? "Yes" : "No" }, { "LastUpdated", "Last updated: heeeenlo" } }; @@ -359,8 +367,8 @@ namespace Buypeeb { private void RenderList() { this.items.Clear(); - foreach (var item in this.settings.watchlist) { - items.AppendValues(item.Value); + foreach (var item in this.settings.watchlist.Values) { + items.AppendValues(item); } } @@ -441,7 +449,9 @@ namespace Buypeeb { } catch (Exception e) { Console.WriteLine(e); - MsgBox($"Failed to load {od.Filename}!\n{e.Message}"); + var md = MsgBox($"Failed to load {od.Filename}!\n{e.Message}", ButtonsType.Ok); + md.Run(); + md.Dispose(); } } od.Dispose(); @@ -472,7 +482,52 @@ namespace Buypeeb { var fs = File.CreateText(sd.Filename); fs.Close(); } - File.WriteAllText(sd.Filename, JsonSerializer.Serialize(this.settings)); + File.WriteAllText(sd.Filename, JsonSerializer.Serialize(this.settings, jsonOptions)); + } + catch (Exception e) { + Console.WriteLine(e); + var md = MsgBox($"Failed to write {sd.Filename}!\n{e.Message}.", ButtonsType.Ok); + md.Run(); + md.Dispose(); + } + } + + sd.Dispose(); + } + + private void ButtonExportClicked(object sender, EventArgs a) { + var readyQuery = from item in this.settings.watchlist.Values.ToList() where !item.ready select item; + foreach (var item in readyQuery) { + Console.WriteLine(item.name); + } + + if (readyQuery.Count() != 0) { + var md = MsgBox("Please wait for all items to update before exporting a CSV.", ButtonsType.Ok); + md.Run(); + md.Dispose(); + return; + } + + var sd = new FileChooserDialog( + title: "Export watchlist as CSV", + parent: this, + action: FileChooserAction.Save, + "Cancel", ResponseType.Cancel, "Save", ResponseType.Accept + ); + sd.CurrentName = "buypeeb.csv"; + + var sdf = new FileFilter(); + sdf.Name = "CSV files"; + sdf.AddMimeType("text/csv"); + sdf.AddPattern("*.csv"); + sd.AddFilter(sdf); + + if (sd.Run() == (int)ResponseType.Accept) { + try { + using (var writer = new StreamWriter(sd.Filename)) + using (var csv = new CsvWriter(writer, System.Globalization.CultureInfo.InvariantCulture)) { + csv.WriteRecords(this.settings.watchlist); + } } catch (Exception e) { Console.WriteLine(e); @@ -594,12 +649,12 @@ namespace Buypeeb { private void RenderColumnPriceYen(Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.ITreeModel model, Gtk.TreeIter iter) { YahooAuctionsItem item = (YahooAuctionsItem)model.GetValue(iter, 0); - (cell as Gtk.CellRendererText).Text = item.ready ? item.PriceJPY() : "..."; + (cell as Gtk.CellRendererText).Text = item.ready ? item.priceJPY : "..."; } private void RenderColumnPriceAUD(Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.ITreeModel model, Gtk.TreeIter iter) { YahooAuctionsItem item = (YahooAuctionsItem)model.GetValue(iter, 0); - (cell as Gtk.CellRendererText).Text = item.ready ? item.PriceAUD() : "..."; + (cell as Gtk.CellRendererText).Text = item.ready ? item.priceAUD : "..."; } private void RenderColumnEnding(Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.ITreeModel model, Gtk.TreeIter iter) { diff --git a/Settings.cs b/Settings.cs index 1772f76..ba9933f 100644 --- a/Settings.cs +++ b/Settings.cs @@ -18,15 +18,7 @@ namespace Buypeeb { if (this.watchlist == null) { // either this is the first time the program has been run, or there's something wrong with userdata.json this.watchlist = new Dictionary(); - // this.Watch("https://buypeeb.biz/whatever/k12345", "my thingy"); - // this.Watch("https://buypeeb.biz/whatever/z09876", "your thingy"); - // this.Watch("https://buypeeb.biz/whatever/h55555", "our thingy"); - // for (int i = 0; i < 10; i++) { - // this.Watch($"https://buypeeb.biz/whatever/x{i * 123}", $"filler {i}"); - // } - // this.watchlist["k12345"].Update(); } - } public YahooAuctionsItem Watch(string url, string name) { diff --git a/YahooAuctionsItem.cs b/YahooAuctionsItem.cs index 45c7fcc..008b28d 100644 --- a/YahooAuctionsItem.cs +++ b/YahooAuctionsItem.cs @@ -25,7 +25,7 @@ namespace Buypeeb { // there's not really a need for it i guess but it's my program and i can do what i want public string id { get; set; } public string name { get; set; } - public int price = 0; + private int price = 0; public int winPrice; public string originalName { get; set; } public string notes { get; set; } @@ -45,6 +45,32 @@ namespace Buypeeb { } } + public string priceJPY { + get { + return $"¥{this.price}"; + } + } + + public string winPriceJPY { + get { + return $"¥{this.winPrice}"; + } + } + + public string priceAUD { + get { + double aud = this.price / 75.0; + return $"${aud:f2}"; + } + } + + public string winPriceAUD { + get { + double aud = this.winPrice / 75.0; + return $"${aud:f2}"; + } + } + private bool success { get; set; } // TODO: custom setter that throws an exception if set to false or something idk public YahooAuctionsItem(string id, string name) { @@ -103,14 +129,5 @@ namespace Buypeeb { this.autoExtension = (m.Groups[1].Value == "あり"); } } - - public string PriceAUD(bool win = false) { - double aud = win ? this.winPrice / 75.0 : this.price / 75.0; - return $"${aud:f2}"; - } - - public string PriceJPY(bool win = false) { - return win ? $"¥{this.winPrice}" : $"¥{this.price}"; - } } } diff --git a/buypeeb.csproj b/buypeeb.csproj index b2e8dae..a19cfc3 100755 --- a/buypeeb.csproj +++ b/buypeeb.csproj @@ -14,6 +14,7 @@ + diff --git a/ui/main.glade b/ui/main.glade index 6c68989..8195dcd 100644 --- a/ui/main.glade +++ b/ui/main.glade @@ -185,11 +185,27 @@ True False - Export as... - Export as... + Save as... + Save as... True gtk-save-as + + + + False + True + + + + + True + False + Export + Export + True + gtk-convert +