Skip to content

Instantly share code, notes, and snippets.

@alexkuang0
Last active September 27, 2022 06:49
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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
}
@alexkuang0
Copy link
Author

alexkuang0 commented Oct 4, 2020

本 Gist 已经停止维护,请前往 Gitee 代码仓库 查看后续更新。
This gist is not maintained here anymore, please go to my Gitee repo for future updates.


image

Instructions 使用说明

🧰 Install 安装

请选用以下任一方法:

  1. 手动将源代码复制/下载至 Scriptable 脚本
  2. 通过 Widget Hub 安装

🛠 Configuration 配置

‼️重要变更) 在小部件设置的 Parameter 中输入你的哔哩哔哩 UID 即可。

image

🎬 Video Instructions 视频说明

https://www.bilibili.com/video/BV1654y1178Z

Changelog 更新日志

0.1.0 09/26/2020

  • 创建

0.1.1 10/04/2020

  • 取消无用的 cookie 设置
  • 粉丝数量显示(千和百万级别)不再显示小数,以防长度过长影响观感

0.1.2 10/12/2020

  • 粉丝数量显示过长时自动缩小字号

0.2.0 03/02/2021

  • 加入每日粉丝变化量显示(若当日无新增粉丝,或第一天使用此功能将不会显示)

变化量显示

0.2.1 03/06/2021

  • 粉丝变化量更新行为由 从第一次运行开始计算更新时间 更改为 每日固定时间更新
  • 增加设置项 UPDATE_TIME ,可自行设置每日何时更新(默认:晚上 0 点)
  • 更改了粉丝变化量显示效果: +/- 与数字间没有空格

0.3.0 03/11/2021

  • 重构代码,加入更多信息(头像、昵称)
  • 粉丝数显示逻辑改变(1 万以下直接显示,1 万以上保留 1 位小数)
  • 修改了配置方式
  • 去掉显示粉丝量变化数据的功能

@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