Last active
September 27, 2022 06:49
-
-
Save alexkuang0/a933a543324195a10ace72bda2b9d282 to your computer and use it in GitHub Desktop.
Scriptable iOS 14 Widget - Bilibili followers count monitor 哔哩哔哩粉丝数监控
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
const UID = args.widgetParameter | |
if (!/^\d+$/.test(UID)) throw new Error('请提供正确的 UID') | |
let widget = createWidget(await getData(UID)) | |
if (!config.runsInWidget) { | |
await widget.presentSmall() | |
} | |
Script.setWidget(widget) | |
Script.complete() | |
async function getData(uid) { | |
const result = { | |
uid, | |
avatar: null, | |
follower: null, | |
nickname: '未知用户' | |
} | |
try { | |
const r1 = new Request(`https://api.bilibili.com/x/relation/stat?vmid=${uid}`) | |
const res1 = await r1.loadJSON() | |
const r2 = new Request(`https://api.bilibili.com/x/space/acc/info?mid=${uid}`) | |
const res2 = await r2.loadJSON() | |
if (res1.data && res1.data.follower && res2.data && res2.data.name && res2.data.face) { | |
result.follower = res1.data.follower | |
result.nickname = res2.data.name | |
result.avatar = await (new Request(res2.data.face)).loadImage() | |
} else { | |
throw new Error('no sufficient data.') | |
} | |
} catch (e) { | |
console.log(e.message) | |
const isCloud = module.filename.includes('Documents/iCloud~') | |
const fileMgr = FileManager[isCloud ? 'iCloud' : 'local']() | |
const placeholderPath = fileMgr.documentsDirectory() + '/biliFollower-avatarPlaceholder.jpeg' | |
result.avatar = Image.fromFile(placeholderPath) | |
} | |
return result | |
} | |
function formatFollower(num) { | |
if (typeof num !== 'number') return 'N/A' | |
if (num >= 10000) return (num / 10000).toFixed(1) + 'w' | |
return num.toString() | |
} | |
function createWidget(options) { | |
const w = new ListWidget() | |
const { avatar, nickname, uid, follower } = options | |
// background gradient | |
const gr1 = new LinearGradient() | |
gr1.locations = [0, 1] | |
gr1.colors = [ | |
new Color('#F68EBC'), | |
new Color('#F45A8D'), | |
] | |
w.backgroundGradient = gr1 | |
w.url = `https://space.bilibili.com/${uid}` | |
// top spacer | |
w.addSpacer() | |
// stack | |
const st = w.addStack() | |
// avatar image | |
const ava = st.addImage(avatar) | |
ava.imageSize = new Size(20, 20) | |
ava.cornerRadius = 10 | |
ava.borderWidth = 3 | |
ava.borderColor = new Color('#FFF') | |
st.addSpacer(5) | |
// nickname | |
const cap = st.addText(nickname) | |
cap.font = Font.heavySystemFont(14) | |
cap.textColor = new Color('#FFF') | |
cap.lineLimit = 1 | |
cap.minimumScaleFactor = 0.8 | |
w.addSpacer(2) | |
// follower | |
const fo = w.addText(formatFollower(follower)) | |
fo.font = Font.thinSystemFont(45) | |
fo.textColor = new Color('#FFF') | |
fo.lineLimit = 1 | |
fo.minimumScaleFactor = 0.5 | |
// bottom spacer | |
w.addSpacer() | |
return w | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
进过测试,粉丝增量统计从添加脚本时刻开始计算,能不能改成晚上12点呢