# 台大體育館人數 Scriptable
### 安裝步驟
1. 安裝Scriptable (iOS)
2. 新增Script,複製貼上以下程式碼
3. iPhone home screen新增widget,選擇最小的
4. 放到Home screen 後選擇剛剛新增的script
5. 第二欄When interacting選 Run script
### Script
```javascript=
url = "https://ntusportscenter.ntu.edu.tw/counter.txt"
let r = new Request(url)
let html = await r.loadJSON()
gym = html["CounterData"][0]["innerCount"].split(';')[0]
pool = html["CounterData"][0]["innerCount"].split(';')[1]
const width=125
const h=7
const w = new ListWidget()
REFRESH_INTERVAL = 1 //mins
w.backgroundColor=new Color("#222222")
var refreshDate = Date.now() + 1000*60*REFRESH_INTERVAL
w.refreshAfterDate = new Date(refreshDate)
function getwidget(total, haveGone, str) {
const titlew = w.addText(str)
titlew.textColor = new Color("#808080")
titlew.font = Font.boldSystemFont(15)
w.addSpacer(6)
const imgw = w.addImage(creatProgress(total,haveGone))
imgw.imageSize=new Size(width, h)
w.addSpacer(14)
}
function creatProgress(total,count){
let colorCodeCounter = ''
if (count/total >= 0.0) { colorCodeCounter = '65c64c'}
if (count/total >= 0.54) { colorCodeCounter = 'dfce60'}
if (count/total >= 0.9) { colorCodeCounter = 'b74d34'}
const context =new DrawContext()
context.size=new Size(width, h)
context.opaque=false
context.respectScreenScale=true
context.setFillColor(new Color("#48484b"))
const path = new Path()
path.addRoundedRect(new Rect(0, 0, width, h), 3, 2)
context.addPath(path)
context.fillPath()
context.setFillColor(new Color("#"+colorCodeCounter))
const path1 = new Path()
path1.addRoundedRect(new Rect(0, 0, width*count/total, h), 3, 2)
context.addPath(path1)
context.fillPath()
return context.getImage()
}
w.addSpacer(8)
let provider = w.addText("台大體育館")
provider.font = Font.mediumSystemFont(19)
provider.textColor = new Color("#808080")
w.addSpacer(16)
if (gym > 9) {getwidget(80, gym, "重訓室\t" + gym.toString() + " / 80")}
else {getwidget(80, gym, "重訓室\t " + gym.toString() + " / 80")}
if (pool > 9) {getwidget(60, pool, "游泳池\t"+ pool.toString() + " / 60")}
else{getwidget(60, pool, "游泳池\t "+ pool.toString() + " / 60")}
Script.setWidget(w)
Script.complete()
w.presentSmall()
```
### 參考圖
