Untitled

 avatar
unknown
plain_text
3 months ago
2.7 kB
98
No Index
const URL = "https://apps.nutc.edu.tw/getParking/showParkingData.php";
const TARGET_AREAS = ["資訊館", "中正平面", "中技大樓", "錦平街"];

async function fetchData() {
  try {
    let req = new Request(URL);
    req.headers = { "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko)" };
    let html = await req.loadString();
    let match = html.match(/console\.log\(\s*(\[[\s\S]*?\])\s*\);/);
    if (match && match[1]) return JSON.parse(match[1]);
  } catch (e) {}
  return null;
}

const allData = await fetchData();
const widget = new ListWidget();
widget.backgroundColor = new Color("#1c1c1e");
widget.url = "scriptable:///run/" + encodeURI(Script.name()) + "?isWidgetClick=true";
widget.refreshAfterDate = new Date(Date.now() + 300000);

if (!allData) {
  let err = widget.addText("無法獲取數據");
  err.textColor = Color.red();
} else {
  const data = allData.filter(item => TARGET_AREAS.includes(item.area));
  let updateTime = data.length > 0 ? new Date(data[0].all.updateTime) : new Date();
  
  let now = new Date();
  let displayTime = updateTime;
  if (Math.abs(now - updateTime) < 120 * 1000) displayTime = now;

  let header = widget.addStack();
  header.centerAlignContent();
  let title = header.addText("中科大車位");
  title.font = Font.boldSystemFont(14);
  title.textColor = Color.white();
  header.addSpacer();
  
  let timeStr = `${updateTime.getHours().toString().padStart(2, '0')}:${updateTime.getMinutes().toString().padStart(2, '0')}`;
  let timeObj = header.addText(timeStr);
  timeObj.font = new Font("Menlo", 12);
  timeObj.textColor = Color.gray(); 
  
  widget.addSpacer(8);

  for (let item of data) {
    let row = widget.addStack();
    row.centerAlignContent();
    
    let name = row.addText(item.area);
    name.font = Font.systemFont(13);
    name.textColor = Color.lightGray();
    row.addSpacer();
    
    let count = item.all.spaceCount;
    let countObj = row.addText(count.toString());
    countObj.font = new Font("Menlo-Bold", 16);
    
    if (count == 0) countObj.textColor = Color.gray();
    else if (count <= 5) countObj.textColor = new Color("#ff453a");
    else countObj.textColor = new Color("#30d158");
    
    widget.addSpacer(2);
  }

  widget.addSpacer();

  let footerStack = widget.addStack();
  footerStack.centerAlignContent();
  
  let dynamicDate = footerStack.addDate(displayTime);
  dynamicDate.applyRelativeStyle(); 
  dynamicDate.font = Font.systemFont(10);
  dynamicDate.textColor = Color.gray();
}

Script.setWidget(widget);
Script.complete();
widget.presentMedium();

if (config.runsInApp && args.queryParameters.isWidgetClick == "true") {
  App.close();
}
Editor is loading...