# @name BilibiliFans # @desc 显示Bilibili粉丝数 # @author zemo # @version 1.1 # @config uid text "B站UID" default="2125744484" maxlen=20 # @config icon_id text "图标ID" default="71441" maxlen=32 # @config every number "刷新间隔" default=10 min=1 max=120 unit=min # @config tint color "数字颜色" default=#00AEEC class BilibiliFans var url, referer, period var icon_id, tint var count, label, xpos var ticks, busy, err def init() var uid = store.get("uid") self.url = "https://api.bilibili.com/x/relation/stat?vmid=" + uid self.referer = "https://space.bilibili.com/" + uid self.period = store.get("every") * 60 self.icon_id = store.get("icon_id") self.tint = store.get("tint") self.count = store.get("count") self.label = nil self.xpos = 9 if self.count != nil self.label = self.format_count(self.count) self.xpos = 9 + (width() - 9 - text_ink_width(self.label)) / 2 end self.ticks = 15 self.busy = false self.err = false end def format_count(n) var full = str(n) var available = width() - 9 if text_ink_width(full) <= available return full end var divisor = 10000 var suffix = "W" if n >= 100000000 divisor = 100000000 suffix = "Y" end var rounded = round((n / (divisor + 0.0)) * 10) return str(int(rounded / 10)) + "." + str(rounded % 10) + suffix end def on_body(body, status) self.busy = false if body == nil self.err = true log("BilibiliFans empty, HTTP " + str(status)) return end # 确认接口返回成功 if re.search("\"code\":0", body) == nil self.err = true log("BilibiliFans API error, HTTP " + str(status)) log(body) return end # 兼容冒号附近可能出现空格 var match = re.search("follower[^0-9]*([0-9]+)", body) if match == nil self.err = true log("BilibiliFans follower missing") log(body) return end var followers = num(match[1]) if followers == nil self.err = true return end self.count = followers self.label = self.format_count(followers) self.xpos = 9 + (width() - 9 - text_ink_width(self.label)) / 2 self.err = false store.set("count", followers) log("Bilibili followers: " + str(followers)) end def loop() if self.ticks <= 0 && !self.busy self.busy = true http.get( self.url, / body, status -> self.on_body(body, status), { 'headers': { 'Accept': "application/json,text/plain,*/*", 'Accept-Encoding': "identity", 'User-Agent': "Mozilla/5.0", 'Referer': self.referer, 'Origin': "https://space.bilibili.com" } } ) self.ticks = self.period end self.ticks -= 1 end def draw() clear() icon(self.icon_id, 0, 0) if self.label != nil text(self.xpos, 6, self.label, self.tint) elif self.err text(9, 6, "ERR", 0xCC7722) else text(9, 6, "...", 0x555555) end end end return BilibiliFans()