Skip to content

Commit

Permalink
yfquotes@thegli: Restore auto-refresh on import (#1280)
Browse files Browse the repository at this point in the history
* Cache quotes responses for each desklet instance separately
* Restore automatic quotes data refresh on settings import
  • Loading branch information
thegli committed Sep 13, 2024
1 parent 67a7f3d commit 516b5c2
Show file tree
Hide file tree
Showing 16 changed files with 202 additions and 147 deletions.
13 changes: 13 additions & 0 deletions yfquotes@thegli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
### 0.15.1 - September 12, 2024

Features:

- update Catalan translation (courtesy of [Odyssey](https://github.com/odyssey))
- update Dutch translation (courtesy of [qadzek](https://github.com/qadzek))
- update Spanish translation (courtesy of [haggen88](https://github.com/haggen88))

Bugfixes:

- cache quotes responses for each desklet instance separately
- restore automatic quotes data refresh on settings import

### 0.15.0 - September 2, 2024

Features:
Expand Down
72 changes: 57 additions & 15 deletions yfquotes@thegli/files/yfquotes@thegli/desklet.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,15 @@ Soup.Session.prototype.add_feature.call(_httpSession, _cookieJar);

let _crumb = null;

let _lastResponse = {
// cache the last QF quotes response
const _lastResponses = new Map();
_lastResponses.set("default", {
symbolsArgument: "",
responseResult: [],
// we should never see this error message
responseError: _("No quotes data to display"),
lastUpdated: new Date()
};
});

function logDebug(msg) {
if (LOG_DEBUG) {
Expand Down Expand Up @@ -159,6 +162,15 @@ YahooFinanceQuoteUtils.prototype = {
};
},

compareSymbolsArgument: function(symbolsArgument, quoteSymbolsText) {
const argumentFromText = this.buildSymbolsArgument(quoteSymbolsText);
if (symbolsArgument.length === 0 || argumentFromText.length === 0) {
return false;
}

return symbolsArgument === argumentFromText;
},

isOkStatus: function(soupMessage) {
if (soupMessage) {
if (IS_SOUP_2) {
Expand Down Expand Up @@ -248,6 +260,7 @@ YahooFinanceQuoteReader.prototype = {
quoteUtils: new YahooFinanceQuoteUtils(),

getCookie: function(customUserAgent, callback) {
logDebug("getCookie");
const _that = this;
const message = Soup.Message.new("GET", YF_COOKIE_URL);

Expand Down Expand Up @@ -296,6 +309,7 @@ YahooFinanceQuoteReader.prototype = {
},

postConsent: function(customUserAgent, formData, callback) {
logDebug("postConsent");
const _that = this;
const message = Soup.Message.new("POST", YF_CONSENT_URL);

Expand Down Expand Up @@ -345,6 +359,7 @@ YahooFinanceQuoteReader.prototype = {
},

getCrumb: function(customUserAgent, callback) {
logDebug("getCrumb");
const _that = this;
const message = Soup.Message.new("GET", YF_CRUMB_URL);

Expand Down Expand Up @@ -393,6 +408,7 @@ YahooFinanceQuoteReader.prototype = {
},

getFinanceData: function(quoteSymbolsArg, customUserAgent, callback) {
logDebug("getFinanceData");
const _that = this;

if (quoteSymbolsArg.length === 0) {
Expand Down Expand Up @@ -710,6 +726,7 @@ StockQuoteDesklet.prototype = {
__proto__: Desklet.Desklet.prototype,

init: function(metadata, id) {
logDebug("init desklet id " + id);
this.metadata = metadata;
this.id = id;
this.updateId = 0;
Expand Down Expand Up @@ -864,22 +881,24 @@ StockQuoteDesklet.prototype = {

// called on events that change the quotes data layout (sorting, show/hide fields, text color, etc)
onRenderSettingsChanged: function() {
logDebug("onRenderSettingsChanged");
this.render();
},

// called on events that change the way YFQ data are fetched (data refresh interval)
onDataFetchSettingsChanged: function() {
logDebug("onDataFetchSettingsChanged");
this.removeUpdateTimer();
this.setUpdateTimer();
},

// called on events that change the quotes data (quotes list)
// BEWARE: DO NOT use this function as callback in settings.bindProperty() - otherwise multiple YFQ requests are fired, and multiple timers are created!
// BEWARE: DO NOT use this function as callback in settings.bind() - otherwise multiple YFQ requests are fired, and multiple timers are created!
onQuotesListChanged: function() {
logDebug("onQuotesListChanged");

if (this.updateInProgress) {
logInfo("Data refresh already in progress");
logDebug("Data refresh in progress for desklet id " + this.id);
return;
}
this.removeUpdateTimer();
Expand All @@ -900,17 +919,18 @@ StockQuoteDesklet.prototype = {
},

fetchFinanceDataAndRender: function(quoteSymbolsArg, customUserAgent) {
logDebug("fetchFinanceDataAndRender. quotes=" + quoteSymbolsArg + ", custom User-Agent: " + customUserAgent);
const _that = this;

this.quoteReader.getFinanceData(quoteSymbolsArg, customUserAgent, function(response) {
logDebug("YF query response: " + response);
let parsedResponse = JSON.parse(response);
_lastResponse =
{
_lastResponses.set(_that.id, {
symbolsArgument: quoteSymbolsArg,
responseResult: parsedResponse.quoteResponse.result,
responseError: parsedResponse.quoteResponse.error,
lastUpdated: new Date()
};
});
_that.setUpdateTimer();
_that.render();
});
Expand All @@ -920,6 +940,7 @@ StockQuoteDesklet.prototype = {
for (let cookie of _cookieJar.all_cookies()) {
let cookieName = IS_SOUP_2 ? cookie.name : cookie.get_name();
if (cookieName === name) {
logDebug("Cookie found in jar: " + name);
return true;
}
}
Expand All @@ -928,6 +949,7 @@ StockQuoteDesklet.prototype = {
},

fetchCookieAndRender: function(quoteSymbolsArg, customUserAgent) {
logDebug("fetchCookieAndRender, custom User-Agent: " + customUserAgent);
const _that = this;

this.quoteReader.getCookie(customUserAgent, function(authResponseMessage, responseBody) {
Expand All @@ -944,6 +966,7 @@ StockQuoteDesklet.prototype = {
},

processConsentAndRender: function(authResponseMessage, consentPage, quoteSymbolsArg, customUserAgent) {
logDebug("processConsentAndRender");
const _that = this;
const formElementRegex = /(<form method="post")(.*)(action="">)/;
const formInputRegex = /(<input type="hidden" name=")(.*?)(" value=")(.*?)(">)/g;
Expand Down Expand Up @@ -973,6 +996,7 @@ StockQuoteDesklet.prototype = {
},

fetchCrumbAndRender: function(quoteSymbolsArg, customUserAgent) {
logDebug("fetchCrumbAndRender");
const _that = this;

this.quoteReader.getCrumb(customUserAgent, function(crumbResponseMessage, responseBody) {
Expand All @@ -996,13 +1020,14 @@ StockQuoteDesklet.prototype = {
},

processFailedFetch: function(errorMessage) {
logDebug("processFailedFetch");
const errorResponse = JSON.parse(this.quoteReader.buildErrorResponse(errorMessage));
_lastResponse =
{
_lastResponses.set(this.id, {
symbolsArgument: "",
responseResult: errorResponse.quoteResponse.result,
responseError: errorResponse.quoteResponse.error,
lastUpdated: new Date()
};
});
this.setUpdateTimer();
this.render();
},
Expand All @@ -1011,16 +1036,30 @@ StockQuoteDesklet.prototype = {
logDebug("setUpdateTimer");
if (this.updateInProgress) {
this.updateId = Mainloop.timeout_add(this.delayMinutes * 60000, Lang.bind(this, this.onQuotesListChanged));
logDebug("new updateId " + this.updateId);
this.updateInProgress = false;
}
},

// main method to render the desklet, expects populated _lastResponse
// main method to render the desklet, expects desklet id in _lastResponses map
render: function() {
logDebug("render");
let responseResult = _lastResponse.responseResult;
const responseError = _lastResponse.responseError;
const lastUpdated = _lastResponse.lastUpdated;
let existingId = "default";
logDebug("_lastResponses size: " + _lastResponses.size);
if (_lastResponses.has(this.id)) {
logDebug("last response exists for id " + this.id);
existingId = this.id;
}

if (!this.quoteUtils.compareSymbolsArgument(_lastResponses.get(existingId).symbolsArgument, this.quoteSymbolsText)) {
logDebug("Detected changed quotes list, refreshing data for desklet id " + this.id);
this.onQuotesListChanged();
return;
}

let responseResult = _lastResponses.get(existingId).responseResult;
const responseError = _lastResponses.get(existingId).responseError;
const lastUpdated = _lastResponses.get(existingId).lastUpdated;
const symbolCustomizationMap = this.quoteUtils.buildSymbolCustomizationMap(this.quoteSymbolsText);

// destroy the current view
Expand Down Expand Up @@ -1084,8 +1123,11 @@ StockQuoteDesklet.prototype = {
},

on_desklet_removed: function() {
logDebug("on_desklet_removed for id " + this.id);
this.removeUpdateTimer();
this.unrender();
// remove cached response
_lastResponses.delete(this.id);
},

unrender: function() {
Expand All @@ -1097,7 +1139,7 @@ StockQuoteDesklet.prototype = {
},

removeUpdateTimer: function() {
logDebug("removeUpdateTimer");
logDebug("removeUpdateTimer for updateId " + this.updateId);
if (this.updateId > 0) {
Mainloop.source_remove(this.updateId);
}
Expand Down
2 changes: 1 addition & 1 deletion yfquotes@thegli/files/yfquotes@thegli/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"name": "Yahoo Finance Quotes",
"prevent-decorations": true,
"max-instances": "10",
"version": "0.15.0",
"version": "0.15.1",
"uuid": "yfquotes@thegli"
}
20 changes: 10 additions & 10 deletions yfquotes@thegli/files/yfquotes@thegli/po/ca.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgstr ""
"Project-Id-Version: yfquotes@thegli 0.13.0\n"
"Report-Msgid-Bugs-To: https://github.com/linuxmint/cinnamon-spices-desklets/"
"issues\n"
"POT-Creation-Date: 2024-09-02 17:00+0200\n"
"POT-Creation-Date: 2024-09-12 23:49+0200\n"
"PO-Revision-Date: 2024-09-10 20:15+0200\n"
"Last-Translator: Odyssey <[email protected]>\n"
"Language-Team: \n"
Expand All @@ -18,50 +18,50 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 3.4.2\n"

#. desklet.js:83
#. desklet.js:86
msgid "No quotes data to display"
msgstr "No hi ha dades de cotitzacions per mostrar"

#. desklet.js:399
#. desklet.js:415
msgid "Empty quotes list. Open settings and add some symbols."
msgstr ""
"Llista de cotitzacions buida. Obriu la configuració i afegiu-hi alguns "
"símbols."

#. desklet.js:421 desklet.js:440
#. desklet.js:437 desklet.js:456
msgid "Yahoo Finance service not available!\\nStatus: "
msgstr "El servei de Yahoo finances no està disponible!\\nEstat: "

#. desklet.js:798
#. desklet.js:815
msgid "Updated at "
msgstr "Actualitzat a les "

#. desklet.js:831
#. desklet.js:848
msgid "Error: "
msgstr "Error: "

#. desklet.js:941
#. desklet.js:963
msgid ""
"Failed to retrieve authorization parameter! Unable to fetch quotes data."
"\\nStatus: "
msgstr ""
"Error en recuperar el paràmetre d'autorització. No s'han pogut obtenir les "
"dades de les cotitzacions.\\nEstat: "

#. desklet.js:966
#. desklet.js:989
msgid "Consent processing failed! Unable to fetch quotes data.\\nStatus: "
msgstr ""
"No s'ha pogut processar el consentiment! No s'han pogut obtenir les dades de "
"les cotitzacions.\\nEstat: "

#. desklet.js:971
#. desklet.js:994
msgid ""
"Consent processing not completed! Unable to fetch quotes data.\\nStatus: "
msgstr ""
"No s'ha completat el processament del consentiment. No s'han pogut obtenir "
"les dades de les cotitzacions.\\nEstat: "

#. desklet.js:993
#. desklet.js:1017
msgid ""
"Failed to retrieve authorization crumb! Unable to fetch quotes data."
"\\nStatus: "
Expand Down
20 changes: 10 additions & 10 deletions yfquotes@thegli/files/yfquotes@thegli/po/da.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: https://github.com/linuxmint/cinnamon-spices-desklets/"
"issues\n"
"POT-Creation-Date: 2024-09-02 17:00+0200\n"
"POT-Creation-Date: 2024-09-12 23:49+0200\n"
"PO-Revision-Date: 2023-12-08 08:06+0100\n"
"Last-Translator: Alan Mortensen <[email protected]>\n"
"Language-Team: \n"
Expand All @@ -18,45 +18,45 @@ msgstr ""
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Generator: Poedit 3.0.1\n"

#. desklet.js:83
#. desklet.js:86
msgid "No quotes data to display"
msgstr ""

#. desklet.js:399
#. desklet.js:415
msgid "Empty quotes list. Open settings and add some symbols."
msgstr ""

#. desklet.js:421 desklet.js:440
#. desklet.js:437 desklet.js:456
msgid "Yahoo Finance service not available!\\nStatus: "
msgstr "Yahoo Finance er ikke tilgængelig!\\nStatus: "

#. desklet.js:798
#. desklet.js:815
msgid "Updated at "
msgstr "Opdateret "

#. desklet.js:831
#. desklet.js:848
msgid "Error: "
msgstr "Fejl: "

#. desklet.js:941
#. desklet.js:963
msgid ""
"Failed to retrieve authorization parameter! Unable to fetch quotes data."
"\\nStatus: "
msgstr ""
"Kunne ikke hente godkendelsesparameter! Kunne ikke hente kursdata.\n"
"Status: "

#. desklet.js:966
#. desklet.js:989
msgid "Consent processing failed! Unable to fetch quotes data.\\nStatus: "
msgstr "Samtykkebehandling mislykkedes! Kunne ikke hente kursdata.\\nStatus: "

#. desklet.js:971
#. desklet.js:994
msgid ""
"Consent processing not completed! Unable to fetch quotes data.\\nStatus: "
msgstr ""
"Samtykkebehandling ikke gennemført! Kunne ikke hente kursdata.\\nStatus: "

#. desklet.js:993
#. desklet.js:1017
msgid ""
"Failed to retrieve authorization crumb! Unable to fetch quotes data."
"\\nStatus: "
Expand Down
Loading

0 comments on commit 516b5c2

Please sign in to comment.