implemented automatic updating

This commit is contained in:
Lynne Megido 2020-09-06 04:36:17 +10:00
parent 49119fc6dc
commit 987a28c747
Signed by: lynnesbian
GPG Key ID: F0A184B5213D9F90
3 changed files with 53 additions and 0 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;
}
@ -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;