Created
September 1, 2021 14:55
-
-
Save mebjas/c5f7b8b3ad265af3a89405ebc548aa1e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.