Skip to content

Commit

Permalink
Avoid caching for morningstar-uk
Browse files Browse the repository at this point in the history
  • Loading branch information
joseballester committed Jul 4, 2022
1 parent 1dd0852 commit ed005e9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
17 changes: 12 additions & 5 deletions helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,26 @@ function processSource(source) {
}

/* -------- Fetching cached/non-cached pages -------- */
function fetchURL(url, cacheid) {
function fetchURL(url, cacheid = null) {
const cache = CacheService.getScriptCache();
const cached = cache.get(cacheid);
if (cached != null) {
return cached;

if (cacheid != null) {
const cached = cache.get(cacheid);
if (cached != null) {
return cached;
}
}

const fetch = UrlFetchApp.fetch(url);
if (fetch.getResponseCode() == 200 && fetch.getContent().length > 0) {
const body = fetch.getContentText();
const $ = Cheerio.load(body);
const trimmed = $("body").html();
cache.put(cacheid, trimmed, 7200);

if (cacheid != null) {
cache.put(cacheid, trimmed, 7200);
}

return trimmed;
} else {
throw new Error("Wrong combination of asset identifier and source. Please check the accepted ones at the documentation.");
Expand Down
8 changes: 7 additions & 1 deletion sources/morningstar.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,13 @@ function getCategoryFromMorningstar(doc, country) {

function fetchMorningstar(id, country) {
const url = getMorningstarLink(country, id);
const doc = fetchURL(url, "morningstar-" + country + "-" + id);

const cacheid = !['gb', 'uk'].includes(country)
? "morningstar-" + country + "-" + id
: null;

const doc = fetchURL(url, cacheid);

return doc;
}

Expand Down

0 comments on commit ed005e9

Please sign in to comment.