Skip to content

Instantly share code, notes, and snippets.

@blanboom
Forked from saiteja09/StockWidget.js
Last active December 9, 2020 20:45
Show Gist options
  • Save blanboom/6ff96e27494913c1ca6315b900f555f0 to your computer and use it in GitHub Desktop.
Save blanboom/6ff96e27494913c1ca6315b900f555f0 to your computer and use it in GitHub Desktop.
用于 Scriptable 的股票 widget,能够密集显示更多的股票信息
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: light-gray; icon-glyph: magic;
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-blue; icon-glyph: book; share-sheet-inputs: plain-text;
// Stock Ticker Widget
let stocksInfo = await getStockData()
let widget = await createWidget()
if (config.runsInWidget) {
// The script runs inside a widget, so we pass our instance of ListWidget to be shown inside the widget on the Home Screen.
Script.setWidget(widget)
} else {
// The script runs inside the app, so we preview the widget.
widget.presentSmall()
}
// Calling Script.complete() signals to Scriptable that the script have finished running.
// This can speed up the execution, in particular when running the script from Shortcuts or using Siri.
Script.complete()
async function createWidget(api) {
let upticker = SFSymbol.named("chevron.up");
let downticker = SFSymbol.named("chevron.down");
let widget = new ListWidget()
// Add background gradient
let gradient = new LinearGradient()
gradient.locations = [0, 1]
gradient.colors = [
new Color("141414"),
new Color("13233F")
]
widget.backgroundGradient = gradient
for(j=0; j<stocksInfo.length; j++)
{
let currentStock = stocksInfo[j];
let row1 = widget.addStack();
// Add Stock Symbol
let stockSymbol = row1.addText(currentStock.symbol);
stockSymbol.textColor = Color.white();
stockSymbol.font = Font.boldMonospacedSystemFont(9);
//Add Today's change in price
row1.addSpacer();
let changeValue = row1.addText(currentStock.changepercent+"%");
if(currentStock.changevalue < 0) {
changeValue.textColor = Color.green();
} else {
changeValue.textColor = Color.red();
}
changeValue.font = Font.boldMonospacedSystemFont(9);
// Add Ticker icon
row1.addSpacer(2);
let ticker = null;
if(currentStock.changevalue < 0){
ticker = row1.addImage(downticker.image);
ticker.tintColor = Color.green();
} else {
ticker = row1.addImage(upticker.image);
ticker.tintColor = Color.red();
}
ticker.imageSize = new Size(8,8);
widget.addSpacer(1);
}
// Add last updated time
let row1 = widget.addStack();
row1.addSpacer();
let date = new Date();
let timeString = date.toLocaleTimeString('en-GB');
let updatedTimeText = row1.addText("Updated at "+timeString);
updatedTimeText.textColor = Color.lightGray();
updatedTimeText.font = Font.boldMonospacedSystemFont(9);
row1.addSpacer();
// widget.addSpacer(1);
return widget
}
async function getStockData() {
let stocks = null;
// Read from WidgetParameter if present or use hardcoded values
// Provide values in Widget Parameter as comma seperated list
if(args.widgetParameter == null) {
stocks = ["000001.ss", "399001.sz", "399006.sz", "000300.ss", "%5ehsi", "%5espx", "%5endx", "%5edax", "btc-usd", "eth-usd"];
} else {
stocks = args.widgetParameter.split(",");
}
let stocksdata = [];
for(i=0; i< stocks.length; i++)
{
let stkdata = await queryStockData(stocks[i].trim());
let price = stkdata.quoteSummary.result[0].price;
let priceKeys = Object.keys(price);
let data = {};
data.symbol = price.symbol;
data.changepercent = (price.regularMarketChangePercent.raw * 100).toFixed(2);
data.changevalue = price.regularMarketChange.raw.toFixed(2);
data.price = price.regularMarketPrice.raw.toFixed(2);
data.high = price.regularMarketDayHigh.raw.toFixed(2);
data.low = price.regularMarketDayLow.raw.toFixed(2);
data.prevclose = price.regularMarketPreviousClose.raw.toFixed(2);
data.name = price.shortName;
stocksdata.push(data);
}
return stocksdata;
}
async function queryStockData(symbol) {
let url = "https://query1.finance.yahoo.com/v10/finance/quoteSummary/" + symbol + "?modules=price"
let req = new Request(url)
return await req.loadJSON()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment