Skip to content

Instantly share code, notes, and snippets.

@yusufozgul
Created April 8, 2021 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yusufozgul/0334e1aada7c13d258f7a06f5583b784 to your computer and use it in GitHub Desktop.
Save yusufozgul/0334e1aada7c13d258f7a06f5583b784 to your computer and use it in GitHub Desktop.
Scriptable widget for fund info
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: red; icon-glyph: chart-line;
let widget = new ListWidget()
let mainColor = Color.dynamic(Color.black(), Color.white())
var allTotal = 0
var teknoloji = {
'daily': "",
'monthly': "",
'current': "",
'change' : "",
'title': "Teknoloji",
'code': "GUH",
'mainStock': 1,
'totalFonCount': 1
}
let fonArray = [teknoloji, temizEnerji, smartTemk, borclanma]
await Promise.all(fonArray.map(getFon))
fonArray.forEach(setFonData)
widget.setPadding(0, 5, 3, 5)
setInfoData()
widget.presentLarge()
Script.setWidget(widget)
Script.complete()
async function getFon(fon) {
let url = "https://www.tefas.gov.tr/api/DB/GetAllFundAnalyzeData"
let request = new Request(url)
request.method = "POST"
request.headers["Content-Type"] = "application/x-www-form-urlencoded; charset=utf-8"
request.body = "dil=TR&fonkod=" + fon.code
let response = await request.loadJSON()
let currentStatus = response.fundInfo[0].SONFIYAT * fon.totalFonCount - fon.mainStock
if (response.fundInfo[0].GUNLUKGETIRI != -100) {
let yesterdayStatus = response.fundPrices1A[response.fundPrices1A.length - 2].FIYAT * fon.totalFonCount - fon.mainStock
let yesterday = currentStatus - yesterdayStatus
allTotal += currentStatus
fon.current = currentStatus.toFixed(2)
fon.daily = "%" + response.fundInfo[0].GUNLUKGETIRI.toFixed(2)
fon.monthly = "%" + response.fundReturn[0].GETIRI1A.toFixed(2)
fon.change = yesterday.toFixed(2)
} else {
fon.current = "-•-"
fon.daily = "-•-"
fon.monthly = "-•-"
}
}
function setFonData(fon) {
let titleText = widget.addText(fon.title)
titleText.textColor = mainColor
titleText.font = Font.semiboldSystemFont(15)
widget.addSpacer(3)
let layoutStack = widget.addStack()
layoutStack.setPadding(3, 0, 0, 10)
layoutStack.centerAlignContent()
let dailyChangeText = layoutStack.addText("Günlük: ")
dailyChangeText.textColor = mainColor
dailyChangeText.font = Font.systemFont(12)
let dailyChangeAmountText = layoutStack.addText(fon.daily)
dailyChangeAmountText.textColor = mainColor
dailyChangeAmountText.font = Font.semiboldSystemFont(13)
if (fon.daily.charAt(1) == "-") {
dailyChangeAmountText.textColor = Color.red()
} else if (fon.daily.charAt(0) == "%") {
dailyChangeAmountText.textColor = Color.green()
}
layoutStack.addSpacer(3)
let change = layoutStack.addText("(" + String(fon.change) + ")")
change.textColor = mainColor
change.font = Font.semiboldSystemFont(8)
if (fon.change < 0) {
change.textColor = Color.red()
} else {
change.textColor = Color.green()
}
layoutStack.addSpacer()
let monthlyText = layoutStack.addText("Aylık: ")
monthlyText.textColor = mainColor
monthlyText.font = Font.systemFont(12)
let monthlyAmountText = layoutStack.addText(fon.monthly)
monthlyAmountText.textColor = mainColor
monthlyAmountText.font = Font.semiboldSystemFont(12)
if (fon.monthly.charAt(1) == "-") {
monthlyAmountText.textColor = Color.red()
} else if (fon.monthly.charAt(0) == "%") {
monthlyAmountText.textColor = Color.green()
}
layoutStack.addSpacer()
let currentText = layoutStack.addText("Durum: ")
currentText.textColor = mainColor
currentText.font = Font.systemFont(12)
let currentAmountText = layoutStack.addText(fon.current)
currentAmountText.textColor = mainColor
currentAmountText.font = Font.semiboldSystemFont(12)
if (fon.current < 0) {
currentAmountText.textColor = Color.red()
} else if (fon.current != "-•-"){
currentAmountText.textColor = Color.green()
}
widget.addSpacer(5)
let bg = widget.addStack()
bg.backgroundColor = Color.gray()
bg.size = new Size(320, 0.3)
widget.addSpacer(15)
}
function setInfoData() {
let formatter = new DateFormatter()
formatter.useMediumTimeStyle()
let layoutStack = widget.addStack()
layoutStack.setPadding(3, 0, 0, 10)
layoutStack.centerAlignContent()
let date = formatter.string(new Date)
let titleText = layoutStack.addText("Updated: " + date)
titleText.textColor = mainColor
titleText.textOpacity = 0.8
titleText.font = Font.systemFont(13)
layoutStack.addSpacer()
let totalText = layoutStack.addText("Total: ")
totalText.textColor = mainColor
totalText.textOpacity = 0.8
totalText.font = Font.systemFont(13)
let totalAmountText = layoutStack.addText(allTotal.toFixed(2))
totalAmountText.font = Font.semiboldSystemFont(13)
if (allTotal < 0) {
totalAmountText.textColor = Color.red()
} else {
totalAmountText.textColor = Color.green()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment