Skip to content

Instantly share code, notes, and snippets.

@alexkuang0
Last active September 27, 2022 06:49
Show Gist options
  • Save alexkuang0/a933a543324195a10ace72bda2b9d282 to your computer and use it in GitHub Desktop.
Save alexkuang0/a933a543324195a10ace72bda2b9d282 to your computer and use it in GitHub Desktop.
Scriptable iOS 14 Widget - Bilibili followers count monitor 哔哩哔哩粉丝数监控
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
}
@janedut
Copy link

janedut commented Mar 2, 2021

能考虑加入每天粉丝增量和粉丝数量曲线图吗

@alexkuang0
Copy link
Author

alexkuang0 commented Mar 2, 2021

能考虑加入每天粉丝增量和粉丝数量曲线图吗

@janedut 已加入显示每日粉丝增量功能,详见上一条更新日志。写得匆忙,如有 bug 请反馈。

p.s. 曲线图功能不打算加入, Scriptable 提供的 API 和数据存储功能都很有限,实现图表过于复杂。

@ThisIsBenny
Copy link

Hi @alexkuang0,
Benny here, from Widget Hub. You had uploaded your widget to Widget Hub. Unfortunately, the image URL does not work and the description of the widget was not correct. I have corrected the issue now and would unlock the widget now.

Regards Benny

@alexkuang0
Copy link
Author

@ThisIsBenny Thank you Benny. It's probably someone else who uploaded the widget cuz I don't remember doing that. Do you have a link to it?

@ThisIsBenny
Copy link

Oh ok... I didn‘t check the User and I have assume that the user is the author. If you like, i can remove it: https://widget-hub.app/widget/6040901ec1909a00094fa314/bilibili-followers

@alexkuang0
Copy link
Author

@ThisIsBenny I just registered as a developer on Widget Hub with my GitHub account. If a transfer of ownership is possible please do; if not I can upload one myself. Btw thank you for your great work!

@ThisIsBenny
Copy link

Transferring the ownership isn‘t possible. In will remove the Script which was uploaded without permission

@janedut
Copy link

janedut commented Mar 5, 2021

能考虑加入每天粉丝增量和粉丝数量曲线图吗

@janedut 已加入显示每日粉丝增量功能,详见上一条更新日志。写得匆忙,如有 bug 请反馈。

p.s. 曲线图功能不打算加入, Scriptable 提供的 API 和数据存储功能都很有限,实现图表过于复杂。

进过测试,粉丝增量统计从添加脚本时刻开始计算,能不能改成晚上12点呢

@alexkuang0
Copy link
Author

@janedut 用这个 时间戳转换工具 找到最近的一天的晚上 12 点时间戳(单位注意选成毫秒),然后在 Scriptable 中新建一个脚本并运行即可手动重置:

const UPDATE_KEY = 'bilibiliFollower.update' // keyname: 上次更新时间
Keychain.set(UPDATE_KEY, '<这里填写生成的时间戳>')

@alexkuang0
Copy link
Author

alexkuang0 commented Mar 6, 2021

@janedut 已更新,现在默认晚上 12 点更新

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment