Compare commits

...

2 commits

Author SHA1 Message Date
82c666296a
handle writing files better 2020-09-06 04:51:47 +10:00
987a28c747
implemented automatic updating 2020-09-06 04:36:17 +10:00
3 changed files with 60 additions and 7 deletions

View file

@ -55,6 +55,7 @@ namespace Buypeeb {
static SemaphoreSlim taskLimit = new SemaphoreSlim(6);
private Queue<string> updateQueue = new Queue<string>();
private IEnumerable<YahooAuctionsItem> filterQuery;
private IEnumerable<YahooAuctionsItem> outdatedItemQuery;
private YahooAuctionsItem selectedItem {
get {
@ -115,6 +116,14 @@ namespace Buypeeb {
this.filterChecks[name].Active = false;
}
// only returns items that meet all of the following:
// - marked as "ready", as in, they aren't in the process of updating
// - not updated since the interval
this.outdatedItemQuery =
from item in this.settings.watchlist.Values.ToList()
where item.ready && this.settings.ItemNotUpdatedSinceInterval(item)
select item;
// father forgive me for i have lynned
this.filterQuery =
from item in this.settings.watchlist.Values.ToList()
@ -155,6 +164,7 @@ namespace Buypeeb {
this.RenderList();
this.UpdateItems();
GLib.Timeout.Add(1000, new GLib.TimeoutHandler(UpdateSelectionEndTime));
GLib.Timeout.Add(10000, new GLib.TimeoutHandler(AutoUpdateItems));
DeleteEvent += WindowShutdown;
}
@ -193,11 +203,11 @@ namespace Buypeeb {
if (!Directory.Exists(this.location)) {
Directory.CreateDirectory(this.location);
}
if (!File.Exists(p)) {
var fs = File.CreateText(p);
fs.Close();
using (StreamWriter fs = File.CreateText(p)) {
fs.Write(j);
}
File.WriteAllText(System.IO.Path.Combine(this.location, "userdata.json"), j);
}
private void UpdateThread(string id) {
@ -519,10 +529,10 @@ namespace Buypeeb {
if (sd.Run() == (int)ResponseType.Accept) {
try {
if (!File.Exists(sd.Filename)) {
var fs = File.CreateText(sd.Filename);
fs.Close();
using (StreamWriter fs = File.CreateText(sd.Filename)) {
fs.Write(JsonSerializer.Serialize(this.settings, jsonOptions));
}
}
File.WriteAllText(sd.Filename, JsonSerializer.Serialize(this.settings, jsonOptions));
}
catch (Exception e) {
Console.WriteLine(e);
@ -674,6 +684,24 @@ namespace Buypeeb {
return true;
}
private bool AutoUpdateItems() {
if (this.queueActive) {
// don't autoupdate if the queue is active
return true;
}
foreach (var item in this.outdatedItemQuery) {
updateQueue.Enqueue(item.id);
}
if (updateQueue.TryPeek(out string _)) {
// there's at least one item in the queue
this.ProcessUpdateQueue();
}
return true;
}
// column renderers
private void RenderColumnFavourite(Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.ITreeModel model, Gtk.TreeIter iter) {

View file

@ -27,5 +27,29 @@ namespace Buypeeb {
this.watchlist[id] = new YahooAuctionsItem(id, name);
return this.watchlist[id];
}
// TRUE if the item hasn't been updated for at least interval seconds
// for example, if the interval is 10, and the item hasn't been updated since 20 seconds ago, this will be TRUE
public bool ItemNotUpdatedSinceInterval(YahooAuctionsItem item) {
int seconds = 1000;
if (item.favourite) {
if (item.endingSoon) {
seconds = this.favouriteUpdateIntervalCritical;
}
else {
seconds = this.favouriteUpdateInterval;
}
}
else {
if (item.endingSoon) {
seconds = this.updateIntervalCritical;
}
else {
seconds = this.updateInterval;
}
}
var later = item.lastUpdated.AddSeconds(seconds);
return DateTime.Compare(later, DateTime.UtcNow) < 0;
}
}
}

View file

@ -53,6 +53,7 @@ namespace Buypeeb {
public bool endingToday { get { return this.endDate.DayOfYear == DateTime.UtcNow.DayOfYear; } }
public bool hasWinPrice { get { return this.winPrice != 0; } }
private bool success { get; set; } // TODO: custom setter that throws an exception if set to false or something idk
public bool endingSoon { get { return DateTime.Compare(DateTime.UtcNow.AddMinutes(10), this.endDate) > 0; } }
public YahooAuctionsItem(string id, string name) {
this.id = id;