import urwid, time, sys, ast, requests, json from urllib.request import urlopen
class BiliSpider: def __init__(self): self.online_api = "https://api.bilibili.com/x/web-interface/online" self.video_api = "https://api.bilibili.com/x/web-interface/archive/stat?&aid=%s" self.newlist_api = "https://api.bilibili.com/x/web-interface/newlist?&rid=%s&pn=%s&ps=%s" self.region_api = "https://api.bilibili.com/x/web-interface/dynamic/region?&rid=%s&pn=%s&ps=%s" self.member_api = "http://space.bilibili.com/ajax/member/GetInfo" self.stat_api = "https://api.bilibili.com/x/relation/stat?vmid=%s" self.upstat_api = "https://api.bilibili.com/x/space/upstat?mid=%s" self.follower_api = "https://api.bilibili.com/x/relation/followings?vmid=%s&pn=%s&ps=%s" self.fans_api = "https://api.bilibili.com/x/relation/followers?vmid=%s&pn=%s&ps=%s" def get_api(api_url): headers = { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", "Accept-Encoding": "gzip, deflate, br", "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2", "Host": "api.bilibili.com", "Referer": "https://www.bilibili.com/", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36", } res = requests.get(api_url, headers=headers) res_dict = res.json() return res_dict def get_online(self): """ 获取在线信息 all_count: 最新投稿 web_online: 在线人数 :return: """ online_dic = BiliSpider.get_api(self.online_api) return online_dic def get_video_info(self, aid): """ 获取视频信息 :param aid: 视频id :return: """ res = BiliSpider.get_api(self.video_api %aid) return res def get_newlist_info(self, rid, pn, ps): """ 获取最新视频信息 :param rid: 二级标题的id (详见tid_info.txt) :param pn: 页数 :param ps: 每页条目数 1-50 :return: """ res = BiliSpider.get_api(self.newlist_api %(rid, pn, ps)) return res def get_region_info(self, rid, pn, ps): """ 获取最新视频信息 :param rid: 二级标题的id (详见tid_info.txt) :param pn: 页数 :param ps: 每页条目数 1-50 :return: """ res = BiliSpider.get_api(self.region_api %(rid, pn, ps)) return res def get_member_info(self, mid): """ 获取用户信息 :param mid:用户id :return: """ post_data = { "crsf": "", "mid": mid, } header = { "Host": "space.bilibili.com", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0", "Accept": "application/json, text/plain, */*", "Accept-Encoding": "gzip, deflate, br", "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2", "Referer": "https://www.bilibili.com/", } res = requests.post(self.member_api, data=post_data, headers=header) member_dic = json.dumps(res.json(), ensure_ascii=False) return member_dic def get_stat_info(self, vmid): """ 获取某用户的关注数和粉丝总数 :param vmid: 用户id :return: """ res = BiliSpider.get_api(self.stat_api % vmid) return res def get_upstat_info(self, mid): """ 获取某用户的总播放量和总阅读量 :param mid: 用户id :return: """ res = BiliSpider.get_api(self.upstat_api % mid) return res def get_follower_info(self, vmid, pn, ps): """ 获取某用户关注者信息 :param vmid:用户id :param pn: 页数 最多5页 :param ps: 每页条目数 1-50 :return: """ res = BiliSpider.get_api(self.follower_api %(vmid, pn, ps)) return res def get_fans_info(self, vmid, pn, ps): """ 获取某用户粉丝信息 :param vmid:用户id :param pn: 页数 最多5页 :param ps: 每页条目数 1-50 :return: """ res = BiliSpider.get_api(self.fans_api %(vmid, pn, ps)) return res
bili = BiliSpider()
def Bili_v(Title,ID): ID = str(ID) url = "http://api.bilibili.com/archive_stat/stat?aid=" + ID html = urlopen(url).read().decode('utf-8') d = ast.literal_eval(html) Cont = d['data'] View = str(Cont['view']) Like = str(Cont['like']) Reply = str(Cont['reply']) Coin = str(Cont['coin']) Result = "\n".join([Title,View, Like, Reply, Coin]) return Result
def exit_on_q(key): if key in ('q', 'Q'): raise urwid.ExitMainLoop()
palette = [ ('banner', 'black', 'light gray'), ('streak', 'black', 'dark red'), ('bg', 'black', 'dark blue'),] A = 'Test'
class Refresh: def keypress(self, key): if key in ('q', 'Q'): raise urwid.ExitMainLoop() def setup_view(self): mid = 393056819 AA = bili.get_member_info(mid) 粉丝 = urwid.Text(str(bili.get_stat_info(mid)['data']['follower'])) Title = urwid.Text("\n".join(["","观看量:","点赞:","回复:","硬币:" ])) Bv1 = urwid.Text(Bili_v("Python色差",86328254)) Bv2 = urwid.Text(Bili_v("汪汪洗澡",89026731)) Bv3 = urwid.Text(Bili_v("大生态缸",61040198)) Bv4 = urwid.Text(Bili_v("OneNote记",44637823)) Bv5 = urwid.Text(Bili_v("OneNote2",45117221)) self.Vedio_inf = urwid.Columns([Title,Bv1,Bv2,Bv3,Bv4,Bv5])
Fan = urwid.AttrWrap( 粉丝,'header') self.Vedio_inf = urwid.AttrWrap(self.Vedio_inf, 'body')
self.view = urwid.Padding(self.Vedio_inf, 'left') self.view = urwid.AttrMap(self.view, 'body') self.view = urwid.Filler(self.Vedio_inf, 'middle')
def main(self): self.setup_view() loop = urwid.MainLoop( self.view, palette=[('body', 'dark cyan', '')], unhandled_input=self.keypress) loop.set_alarm_in(1, self.refresh) loop.run()
def refresh(self, loop=None, data=None): self.setup_view() loop.widget = self.view loop.set_alarm_in(5, self.refresh)
if __name__ == '__main__': refresh = Refresh() sys.exit(refresh.main())
|