UMNK星专用抽奖工具

本程序用于实现B站23198878直播间内舰长抽奖功能

import urllib.request
import re
import os
import random
#from tqdm import tqdm
from time import sleep
from datetime import datetime, timedelta
import time


class marine:
    uid = ''
    ruid = ''
    user_space = ''
    all_page = 0  # 舰长列表页数
    crew_list = './crew_list/'  # 存放舰长列表的文件夹名称
    s_crew_list = []
    header = {
        'User-Agent': 'Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
        'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8'
    }

    def __init__(self):
        self.uid = 1736210
        self.space = 'https://api.bilibili.com/x/space/acc/info?mid=1736210&jsonp=jsonp'  # 用户信息

    def ruid(self):  # 根据uid获取直播间号
        space_request = urllib.request.Request(self.space, headers=self.header)
        space_html = urllib.request.urlopen(space_request).read().decode('utf-8')
        self.ruid = re.findall(r'live.bilibili.com/(.*?)"', space_html)[0]
        #self.caplist = 'https://api.live.bilibili.com/xlive/app-room/v2/guardTab/topList?roomid=' + self.ruid + '&page=1&ruid=' + self.uid + '&page_size=29'  # 直播间信息
        self.caplist = 'https://api.live.bilibili.com/xlive/app-room/v2/guardTab/topList?roomid=23198878&page=1&ruid=1736210&page_size=29'
    def Snapshot(self):  # 下载舰长列表
        if not os.path.exists(self.crew_list):
            os.makedirs(self.crew_list)

        request = urllib.request.Request(self.caplist, headers=self.header)
        reponse = urllib.request.urlopen(request).read()

        path = self.crew_list + 'page1.html'
        fh = open(path, "wb")
        fh.write(reponse)
        fh.close()

        reponse = reponse.decode('utf-8')
        all_page = re.findall(r'"page":(.*?),', reponse)[0]
        self.all_page = int(all_page)

        total = re.findall(r'"num":(.*?),', reponse)[0]
        self.total = int(total)

        print("UMNK星目前的舰长总数是:", self.total)

        for i in range(2, self.all_page+1):
            time.sleep(0.5)
            caplist = 'https://api.live.bilibili.com/xlive/app-room/v2/guardTab/topList?roomid=23198878&page='+str(i)+'&ruid=1736210&page_size=29'
            request = urllib.request.Request(caplist, headers=self.header)
            reponse = urllib.request.urlopen(request).read()

            path = self.crew_list + 'page' + str(i) + '.html'
            fh = open(path, "wb")
            fh.write(reponse)
            fh.close()

    def count(self):  # 开始统计

        dirs = os.listdir(self.crew_list)

        flag = []

        for i in range(0, self.total + 1):
            flag.append(0)

        for file in dirs:
            path = self.crew_list + file
            fd = open(path, encoding='utf-8')
            content = fd.read()
            self.s_crew_list.extend(re.findall(
                r'"uid":(.*?),"ruid":.*?"rank":.*?,"username":"(.*?)","face":".*?","is_alive":.*?,"guard_level":.*?,',
                content))
            fd.close()
            list(set(self.s_crew_list))

        #print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), end='')
        number = int(input("请输入需要抽奖的人数:"))
        mini=int(input("请设置开奖倒计时时间(单位:分钟):"))
        count_down(minutes=mini,seconds=1)
        print("\n--------------------------------")
        print("恭喜以下B站用户中奖!")
        fileName = datetime.now().strftime('%Y年%m月%d日%H时%M分%S秒')
        path2 = './'+fileName+'抽奖名单.txt'
        file2 = open(path2, 'w+')
        file2.write("本次中奖名单:\n")
        luckyList = random.sample(range(1, self.total+1), number)
        while number>0:
            print("%-30s"%(self.s_crew_list[luckyList[number-1]][1]))
            file2.write("%-30s\tuid:%-10s\t\n"%(self.s_crew_list[luckyList[number-1]][1],self.s_crew_list[luckyList[number-1]][0]))
            number-=1
        file2.close()
        print("--------------------------------")
        print("具体名单与uid副本已生成至同文件夹内")
def count_down(weeks=0, days=0, hours=0, minutes=0, seconds=0):
    remain_time = timedelta(weeks=weeks, days=days, hours=hours, minutes=minutes, seconds=seconds)
    while remain_time.total_seconds() > 0:
        time.sleep(1)
        remain_time -= timedelta(seconds=1)
        print("\r距离开奖还剩:{}".format(remain_time), end="", flush=True)

def print_test():
    for i in range(20):
        print("#", end="", flush=True)
        sleep(0.1)


os.system("mode con cols=36 lines=20")
while(1):
    print("欢迎使用UMNK星抽奖工具!")
    print("本抽奖工具使用Python随机库确保公正性")
    print("代码公示:https://blog.youkuaiyun.com/weixin_46334665/article/details/128803144")
    umnk = marine()
    umnk.ruid()
    umnk.Snapshot()
    umnk.count()
    os.system("pause")
    i = os.system("cls")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值