Compare commits

...

11 commits

6 changed files with 292 additions and 134 deletions

1
.gitignore vendored
View file

@ -4,3 +4,4 @@ BuypeebApp.exe
bin/ bin/
obj/ obj/
out/ out/
yahoo.html

4
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,4 @@
{
"editor.tabSize": 2,
"editor.insertSpaces": false
}

View file

@ -1,32 +1,75 @@
using System; using System;
using System.Globalization; using System.Globalization;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Text.Json;
namespace Buypeeb { namespace Buypeeb {
class Listing { class Listing {
public string url { get; set; } public string url {
get {
return $"https://page.auctions.yahoo.co.jp/jp/auction/{this.id}";
}
}
public string id { get; set; } public string id { get; set; }
public string name { get; set; } public string name { get; set; }
public int price; public int price = 0;
public int win_price; public int win_price;
public string original_name; public string original_name;
public bool favourite { get; set; } = false; public bool favourite { get; set; } = false;
// start_date, end_date // start_date, end_date
public int bids; public int bids;
public bool auto_extension; public bool auto_extension;
public bool ready;
public Listing(string url, string id, string name) { private bool success { get; set; } // TODO: custom setter that throws an exception if set to false or something idk
this.url = url;
public Listing(string id, string name) {
this.id = id; this.id = id;
this.name = name; this.name = name;
this.ready = false;
} }
public void Update() { public Listing() {
// use fake values for now // parameterless constructor for deserialisation
var rnd = new Random(); }
this.price = rnd.Next(100, 5000);
this.bids = rnd.Next(0, 15); public void Update(string html) {
this.name = "testing"; var rx = new Regex(@"var pageData ?= ?(\{.+?\});", RegexOptions.Singleline); // TODO: maybe compile and match the regex in another thread
this.original_name = "testing"; var m = rx.Match(html);
if (m == null) {
Console.WriteLine("no sir i don't like it");
return;
}
Dictionary<string, Dictionary<string, string>> j_full;
try {
// master forgive me, but i must go all out, just this once...
j_full = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, string>>>(m.Groups[1].Value);
}
catch (Exception e) {
throw e;
}
var j = j_full["items"];
this.original_name = j["productName"];
this.success = int.TryParse(j["price"], out this.price);
this.success = int.TryParse(j["winPrice"], out this.win_price);
this.success = int.TryParse(j["bids"], out this.bids);
if (String.IsNullOrWhiteSpace(this.name)) {
this.name = this.original_name;
}
// as far as i can tell, neither the `pageData` nor the `conf` variables in the html seem to store whether or not the auction uses automatic extension
// the `conf` variable *does* store whether or not the auction has the "early end" feature enabled, in the key `earlyed`.
// unfortunately, it seems like the only way to get the auto extension info is to scrape the page for the info column that displays the auto ext status
// and check whether or not it's equal to "ari" (japanese for "yes").
var autoExtensionCheck = new Regex(@"自動延長.+\n.+>(.+)<");
m = rx.Match(html);
if (m.Groups[1].Value != null) {
this.auto_extension = (m.Groups[1].Value == "あり");
}
} }
public string PriceAUD() { public string PriceAUD() {

View file

@ -18,44 +18,184 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using System.Net;
using Gtk; using Gtk;
namespace Buypeeb { namespace Buypeeb {
enum ItemColumns { public struct ItemColumns {
Name, public const int Name = 0;
PriceYen, public const int PriceYen = 1;
PriceAUD, public const int PriceAUD = 2;
Ending, public const int Ending = 3;
Id public const int Id = 4;
} }
class MainWindow : Window { class MainWindow : Window {
private string location;
// private Queue<string> statuses;
private ListStore items; private ListStore items;
private Settings settings; private Settings settings;
private TreeView itemTreeView;
private Label statusLabel;
static SemaphoreSlim tasklimit = new SemaphoreSlim(4);
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(); if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
this.settings.Save(); // C:\Users\Beebus\AppData\Roaming\Lynnear Software\buypeeb
this.Title = "Buypeeb"; this.location = System.IO.Path.Combine(Environment.ExpandEnvironmentVariables("%APPDATA%"), "Lynnear Software", "buypeeb");
builder.Autoconnect(this);
this.items = (ListStore)builder.GetObject("ListItems");
this.RenderList();
foreach (object[] row in this.items) {
Console.WriteLine(row[(int)ItemColumns.Name]);
} }
else {
// ~/.config/Lynnear Software/buypeeb
this.location = System.IO.Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".config", "Lynnear Software", "buypeeb");
}
string userdata = System.IO.Path.Combine(location, "userdata.json");
if (File.Exists(userdata)) {
try {
string j = File.ReadAllText(userdata);
this.settings = JsonSerializer.Deserialize<Settings>(j);
}
catch {
// ???
Console.WriteLine("oops");
Application.Quit();
}
}
else {
this.settings = new Settings();
}
this.SaveSettings();
this.Title = "Buypeeb";
builder.Autoconnect(this);
this.statusLabel = (Label)builder.GetObject("LabelStatus");
// bind treeview columns to watchlist instead of needing to manually sync its liststore
this.itemTreeView = (TreeView)builder.GetObject("TreeViewItems");
this.items = new ListStore(typeof(Listing));
this.RenderList();
this.itemTreeView.Model = this.items;
TreeCellDataFunc[] funcs = {
new TreeCellDataFunc(this.RenderColumnName),
new TreeCellDataFunc(this.RenderColumnPriceYen),
new TreeCellDataFunc(this.RenderColumnPriceAUD),
new TreeCellDataFunc(this.RenderColumnEnding)
};
for (int i = 0; i < this.itemTreeView.Columns.Length; i++) {
var c = this.itemTreeView.Columns[i];
c.SetCellDataFunc(c.Cells[0], funcs[i]);
}
this.UpdateItems();
DeleteEvent += Window_Shutdown; DeleteEvent += Window_Shutdown;
} }
private void Window_Shutdown(object sender, DeleteEventArgs args) { private void Window_Shutdown(object sender, DeleteEventArgs args) {
SaveSettings();
Application.Quit(); Application.Quit();
} }
// general behaviour // general behaviour
private void SetStatus(string status) {
this.statusLabel.Text = status ?? "Buypeeb";
}
private (TreePath path, TreeIter iter) GetRow(string id) {
// TODO: surely there's a better way to do this
TreeIter iter;
this.itemTreeView.Model.GetIterFirst(out iter);
for (int i = 0; i < this.itemTreeView.Model.IterNChildren(); i++) {
var x = (Listing)this.itemTreeView.Model.GetValue(iter, 0);
if (x.id == id) {
return (this.itemTreeView.Model.GetPath(iter), iter);
}
else {
this.itemTreeView.Model.IterNext(ref iter);
}
}
Console.WriteLine($"Couldn't find {id}!");
return (null, iter);
}
private void SaveSettings() {
string j = JsonSerializer.Serialize(this.settings);
string p = System.IO.Path.Combine(this.location, "userdata.json");
Console.WriteLine(j);
if (!Directory.Exists(this.location)) {
Directory.CreateDirectory(this.location);
}
if (!File.Exists(p)) {
File.CreateText(p);
}
File.WriteAllText(System.IO.Path.Combine(this.location, "userdata.json"), j);
}
private void UpdateThread(string id) {
var item = this.settings.watchlist[id];
Console.WriteLine($"Updating {id}...");
// set item.ready to false to show that it's still being updated
// this changes a few behaviours, such as displaying the price as "..." instead of whatever's currently stored
item.ready = false;
Gtk.Application.Invoke(delegate {
// TODO: find a way to not have to do this. i think we need to avoid actually modifying the items outside of the main thread :/
var pathAndIter = this.GetRow(id);
this.items.EmitRowChanged(pathAndIter.path, pathAndIter.iter);
});
using (WebClient client = new WebClient()) {
// TODO: download should have timeout
item.Update(client.DownloadString(item.url));
// item.Update(File.ReadAllText("yahoo.html"));
}
Gtk.Application.Invoke(delegate {
var pathAndIter = this.GetRow(id);
this.items.EmitRowChanged(pathAndIter.path, pathAndIter.iter);
});
item.ready = true;
Console.WriteLine($"{id} updated.");
}
private void UpdateItem(string id) {
// don't start a new task if there are more than [tasklimit] tasks currently running
// this makes sure we don't make 1000 simultaneous requests to yahoo auctions if there are 1000 items on the watchlist
this.settings.watchlist[id].ready = false;
tasklimit.Wait();
var t = Task.Factory.StartNew(() => {
this.UpdateThread(id);
}).ContinueWith(task => { tasklimit.Release(); });
}
private void UpdateItems() {
var t = Task.Factory.StartNew(() => {
foreach (var item in this.settings.watchlist) {
this.UpdateItem(item.Key);
}
});
}
// show a simple entry dialogue that allows the user to enter text and either cancel or submit it
private (Boolean accepted, string response) EntryDialogue(string title = "Buypeeb", string message = "Hi there!") { private (Boolean accepted, string response) EntryDialogue(string title = "Buypeeb", string message = "Hi there!") {
Dialog ed = new Dialog(title, null, Gtk.DialogFlags.DestroyWithParent, "Cancel", ResponseType.Cancel, "OK", ResponseType.Ok); Dialog ed = new Dialog(title, null, Gtk.DialogFlags.DestroyWithParent, "Cancel", ResponseType.Cancel, "OK", ResponseType.Ok);
ed.DefaultResponse = ResponseType.Ok; ed.DefaultResponse = ResponseType.Ok;
@ -75,15 +215,10 @@ namespace Buypeeb {
return (accepted == ResponseType.Ok, response); return (accepted == ResponseType.Ok, response);
} }
private void UpdateItems() {
}
private void RenderList() { private void RenderList() {
this.items.Clear(); this.items.Clear();
foreach (KeyValuePair<string, Listing> entry in settings.watchlist) { foreach (var item in this.settings.watchlist) {
string[] values = new[] { entry.Value.name, entry.Value.PriceJPY(), entry.Value.PriceAUD(), "whenever", entry.Value.id }; items.AppendValues(item.Value);
this.items.AppendValues(values);
} }
} }
@ -110,7 +245,7 @@ namespace Buypeeb {
} }
private void ButtonUpdateAllClicked(object sender, EventArgs a) { private void ButtonUpdateAllClicked(object sender, EventArgs a) {
Console.WriteLine("ButtonUpdateAllClicked"); this.UpdateItems();
} }
private void ButtonClearEndedClicked(object sender, EventArgs a) { private void ButtonClearEndedClicked(object sender, EventArgs a) {
@ -122,7 +257,7 @@ namespace Buypeeb {
} }
private void ButtonSaveClicked(object sender, EventArgs a) { private void ButtonSaveClicked(object sender, EventArgs a) {
Console.WriteLine("ButtonSaveClicked"); this.SaveSettings();
} }
private void ButtonQuitClicked(object sender, EventArgs a) { private void ButtonQuitClicked(object sender, EventArgs a) {
@ -130,6 +265,7 @@ namespace Buypeeb {
ResponseType response = (ResponseType)md.Run(); ResponseType response = (ResponseType)md.Run();
md.Dispose(); md.Dispose();
if (response == ResponseType.Ok) { if (response == ResponseType.Ok) {
this.SaveSettings();
Application.Quit(); Application.Quit();
} }
} }
@ -154,5 +290,27 @@ namespace Buypeeb {
Console.WriteLine("ButtonSelectedRenameClicked"); Console.WriteLine("ButtonSelectedRenameClicked");
} }
// column renderers
private void RenderColumnName(Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.ITreeModel model, Gtk.TreeIter iter) {
Listing item = (Listing)model.GetValue(iter, 0);
(cell as Gtk.CellRendererText).Text = item.name ?? "Loading...";
}
private void RenderColumnPriceYen(Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.ITreeModel model, Gtk.TreeIter iter) {
Listing item = (Listing)model.GetValue(iter, 0);
(cell as Gtk.CellRendererText).Text = item.ready ? item.PriceJPY() : "...";
}
private void RenderColumnPriceAUD(Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.ITreeModel model, Gtk.TreeIter iter) {
Listing item = (Listing)model.GetValue(iter, 0);
(cell as Gtk.CellRendererText).Text = item.ready ? item.PriceAUD() : "...";
}
private void RenderColumnEnding(Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.ITreeModel model, Gtk.TreeIter iter) {
Listing item = (Listing)model.GetValue(iter, 0);
(cell as Gtk.CellRendererText).Text = item.ready ? "whatever" : "...";
}
} }
} }

View file

@ -1,13 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Text.Json;
namespace Buypeeb { namespace Buypeeb {
class Settings { class Settings {
private string location;
public int updateInterval { get; set; } = 10 * 60; public int updateInterval { get; set; } = 10 * 60;
public int favouriteUpdateInterval { get; set; } = 5 * 60; public int favouriteUpdateInterval { get; set; } = 5 * 60;
public int updateIntervalCritical { get; set; } = 60; public int updateIntervalCritical { get; set; } = 60;
@ -18,36 +14,28 @@ namespace Buypeeb {
} }
public Settings() { public Settings() {
if (Environment.OSVersion.Platform == PlatformID.Win32NT) { if (this.watchlist == null) {
// C:\Users\Beebus\AppData\Roaming\Lynnear Software\buypeeb // either this is the first time the program has been run, or there's something wrong with userdata.json
this.location = Path.Combine(Environment.ExpandEnvironmentVariables("%APPDATA%"), "Lynnear Software", "buypeeb");
}
else {
// ~/.config/Lynnear Software/buypeeb
this.location = Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".config", "Lynnear Software", "buypeeb");
}
this.watchlist = new Dictionary<string, Listing>(); this.watchlist = new Dictionary<string, Listing>();
this.Watch("https://buypeeb.biz/whatever/k12345", "my thingy"); // this.Watch("https://buypeeb.biz/whatever/k12345", "my thingy");
this.watchlist["k12345"].Update(); // 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 void Watch(string url, string name) { public void Watch(string url, string name) {
string id = BuypeebApp.IDFromURL(url); string id = BuypeebApp.IDFromURL(url);
Console.WriteLine(id); Console.WriteLine(id);
this.watchlist[id] = new Listing(url, id, name); this.watchlist[id] = new Listing(id, name);
foreach (KeyValuePair<string, Listing> entry in this.watchlist) { foreach (KeyValuePair<string, Listing> entry in this.watchlist) {
Console.WriteLine("{0} - {1}", entry.Value.name, entry.Value.price); Console.WriteLine("{0} - {1}", entry.Value.name, entry.Value.price);
} }
} }
public void Save() {
string j = JsonSerializer.Serialize(this);
Console.WriteLine(j);
}
// public void Load() {
// }
} }
} }

View file

@ -1,33 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.36.0 --> <!-- Generated with glade 3.22.2 -->
<interface> <interface>
<requires lib="gtk+" version="3.22"/> <requires lib="gtk+" version="3.22"/>
<object class="GtkListStore" id="ListItems">
<columns>
<!-- column-name Name -->
<column type="gchararray" />
<!-- column-name PriceYen -->
<column type="gchararray" />
<!-- column-name PriceAUD -->
<column type="gchararray" />
<!-- column-name Ending -->
<column type="gchararray" />
<!-- column-name id -->
<column type="gchararray" />
</columns>
<data>
<row>
<col id="0" translatable="yes">The Stinchinator</col>
<col id="1" translatable="yes">¥599</col>
<col id="2" translatable="yes">$5.99</col>
<col id="3" translatable="yes">7 hours</col>
<col id="4" translatable="yes">12345</col>
</row>
</data>
</object>
<object class="GtkWindow" id="wndMain"> <object class="GtkWindow" id="wndMain">
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="default_width">700</property> <property name="default_width">700</property>
<child type="titlebar">
<placeholder/>
</child>
<child> <child>
<object class="GtkBox"> <object class="GtkBox">
<property name="visible">True</property> <property name="visible">True</property>
@ -287,10 +267,9 @@
<property name="shadow_type">in</property> <property name="shadow_type">in</property>
<property name="min_content_width">200</property> <property name="min_content_width">200</property>
<child> <child>
<object class="GtkTreeView"> <object class="GtkTreeView" id="TreeViewItems">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="model">ListItems</property>
<property name="rules_hint">True</property> <property name="rules_hint">True</property>
<property name="search_column">1</property> <property name="search_column">1</property>
<property name="activate_on_single_click">True</property> <property name="activate_on_single_click">True</property>
@ -309,9 +288,6 @@
<property name="clickable">True</property> <property name="clickable">True</property>
<child> <child>
<object class="GtkCellRendererText"/> <object class="GtkCellRendererText"/>
<attributes>
<attribute name="text">0</attribute>
</attributes>
</child> </child>
</object> </object>
</child> </child>
@ -321,9 +297,6 @@
<property name="title" translatable="yes">Price (¥)</property> <property name="title" translatable="yes">Price (¥)</property>
<child> <child>
<object class="GtkCellRendererText"/> <object class="GtkCellRendererText"/>
<attributes>
<attribute name="text">1</attribute>
</attributes>
</child> </child>
</object> </object>
</child> </child>
@ -333,9 +306,6 @@
<property name="title" translatable="yes">Price (AUD)</property> <property name="title" translatable="yes">Price (AUD)</property>
<child> <child>
<object class="GtkCellRendererText"/> <object class="GtkCellRendererText"/>
<attributes>
<attribute name="text">2</attribute>
</attributes>
</child> </child>
</object> </object>
</child> </child>
@ -346,9 +316,6 @@
<property name="clickable">True</property> <property name="clickable">True</property>
<child> <child>
<object class="GtkCellRendererText"/> <object class="GtkCellRendererText"/>
<attributes>
<attribute name="text">3</attribute>
</attributes>
</child> </child>
</object> </object>
</child> </child>
@ -795,7 +762,7 @@
<property name="margin_top">3</property> <property name="margin_top">3</property>
<property name="margin_bottom">3</property> <property name="margin_bottom">3</property>
<child> <child>
<object class="GtkLabel"> <object class="GtkLabel" id="LabelStatus">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="halign">start</property> <property name="halign">start</property>
@ -828,8 +795,5 @@
</child> </child>
</object> </object>
</child> </child>
<child type="titlebar">
<placeholder />
</child>
</object> </object>
</interface> </interface>