Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function joyAllukasGoldPrice(cahceDurationSeconds=600) {
const cacheId = "JoyAllukasSingapore";
let cache = CacheService.getDocumentCache();
var cached = cache.get(cacheId);
if (cached != null) {
let result = JSON.parse(cached);
if (result) {
Logger.log("Data from cache: " +JSON.stringify(result));
return result;
}
}
const singaporeLineCode = "[country_name] => Singapore";
const ks = ["24", "22", "18"];
const format = "rate_in_%%_carat_gold";
const url = "https://eshop.joyalukkas.com";
const expectingToFind = 3;
var html = UrlFetchApp.fetch(url).getContentText();
var splits = html.split("\n");
var singaporeFound = false;
var found = 0;
const result = {24: 0, 22: 0, 18: 0};
for (var i = 0; i < splits.length; i++) {
var line = splits[i];
if (!singaporeFound) {
if (line.includes(singaporeLineCode)) {
singaporeFound = true;
}
continue;
}
for (var j = 0; j < ks.length; j++) {
const k = ks[j];
const needle = format.replace("%%", k);
if (line.includes(needle)) {
// format = " [rate_in_22_carat_gold] => 77.30, "
const value = line.trim().split("=>")[1].trim().split(",")[0];
result[parseInt(k)] = parseFloat(value);
found++;
}
}
if (found == expectingToFind) {
break;
}
}
cache.put(cacheId, JSON.stringify(result), cahceDurationSeconds);
Logger.log(result);
return result;
}
function joyAllukasGold24kSg(cahceDurationSeconds=600) {
var result = joyAllukasGoldPrice(cahceDurationSeconds);
if (24 in result) return result[24];
return result["24"];
}
function joyAllukasGold22kSg(cahceDurationSeconds=600) {
var result = joyAllukasGoldPrice(cahceDurationSeconds);
if (22 in result) return result[22];
return result["22"];
}
function joyAllukasGold18kSg(cahceDurationSeconds=600) {
var result = joyAllukasGoldPrice(cahceDurationSeconds);
if (18 in result) return result[18];
return result["18"];
}
@trexs7

This comment has been minimized.

Copy link

@trexs7 trexs7 commented Sep 15, 2021

How can i change the country to India? I manually tried it by replacing the country name

@trexs7

This comment has been minimized.

Copy link

@trexs7 trexs7 commented Sep 15, 2021

The alignment seems to have been changed as it's giving only 22 carat price as 41.6 which is same for all the countries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment