别走
再看看

bilibili自动做任务升级脚本源码

通过该脚本,可自动化迅速完成bilibili的每日65经验任务,可在本地运行也可以挂服务器上运行。

主要是用来通过实战加深对Python的学习,简单易学,有兴趣的可以研究研究。

 

功能

  • 每日登录 (5点经验值)
  • 每日观看视频 (5点经验值和一枚硬币)
  • 每日投币 (50点经验值)
  • 每日分享视频 (5点经验值)
  • 每日直播签到 (直播经验和辣条)

 

源代码

# -*- coding: utf8 -*-
import requests
import json
import time
import random


# 填写cookie即可运行
'''
1、浏览器登入哔哩网站
2、访问 http://api.bilibili.com/x/space/myinfo
3、F12看到cookie的值粘贴即可
'''
headers = {
    'referer': 'https://www.bilibili.com/',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0',
}

# 填写你登录后的cookie
cookies = ""

# cookie转字典
def extract_cookies(cookies):
    global csrf
    try:
        cookies = dict([l.split("=", 1) for l in cookies.split("; ")])
        csrf = cookies['bili_jct']
        return cookies
    except:
        print("请正确填写有效的Cookie!")


# 银币数
def getCoin():
    cookie = extract_cookies(cookies)
    url = "http://account.bilibili.com/site/getCoin"
    mycoin = requests.get(url, headers=headers, cookies=cookie).text
    coin_msg = json.loads(mycoin)
    money = coin_msg['data']['money']
    return int(money)


# 个人信息
def getInfo():
    global uid
    url = "http://api.bilibili.com/x/space/myinfo"
    cookie = extract_cookies(cookies)
    myinfo = requests.get(url, headers=headers, cookies=cookie).text
    myinfo_dict = json.loads(myinfo)
    vmsg = myinfo_dict['data']

    uid = vmsg['mid']  # 账号uid
    name = vmsg['name']  # 用户昵称
    level = vmsg['level']  # 当前级别
    current_exp = vmsg['level_exp']['current_exp']  # 当前经验值
    next_exp = vmsg['level_exp']['next_exp']  # 下一级经验
    sub_exp = int(next_exp) - int(current_exp)  # 离下一级还需多少经验
    days = int(int(sub_exp) / 65)  # 最快还需多少天升到下一级
    coin = getCoin()  # 账号拥有的硬币数
    msg = "Welcome!" + str(name) + " Current level is " + str(level) + " ,Current experience are " + \
          str(current_exp) + ",Not far from upgrading that just need " + str(sub_exp) + \
          " exp ,about " + str(days) + " days," + "Remaining silver coins are " + str(coin)
    print(msg)


# 获取任务经验获得信息
def finish_msg():
    cookie = extract_cookies(cookies)
    url = 'https://api.bilibili.com/x/member/web/exp/reward'  # 包含全部任务情况
    reward = requests.get(url, headers=headers, cookies=cookie).text
    reward_msg = json.loads(reward)
    video_exp_url = 'https://api.bilibili.com/x/web-interface/coin/today/exp'  # 投硬币情况
    video_exp = requests.get(video_exp_url, headers=headers, cookies=cookie).text
    exp_msg = json.loads(video_exp)

    watch_flag = reward_msg['data']['watch']  # 视频观看完成情况
    share_flag = reward_msg['data']['share']  # 视频分享完成情况
    coin_flag = exp_msg['data']  # 视频投币获得经验数,每日最大为50
    return watch_flag, share_flag, coin_flag


# 推荐动态
def getActiveInfo():
    url = "http://api.bilibili.com/x/web-interface/archive/related?aid=" + \
          str(7)
    cookie = extract_cookies(cookies)
    content = requests.get(url, headers=headers, cookies=cookie).text
    video_dict = json.loads(content)
    return video_dict


# 推荐动态第二种方式
def getVideo():
    random_page = random.randint(0, 167)
    url = "http://api.bilibili.cn/recommend?page=" + str(random_page)
    cookie = extract_cookies(cookies)
    content = requests.get(url, headers=headers, cookies=cookie).text
    video_dict = json.loads(content)
    return video_dict


# 投币 分享5次
def Task():
    finish_flag=0
    coin_num = getCoin()  # 账号拥有硬币数量

    coined = finish_msg()[2]  # 已经投币获得的经验
    num = 5 - coined // 10  # 还需投币数量
    if coin_num <= num:
        num = coin_num
    coin_count = 0

    # 直播签到接口
    live_sign_url = 'https://api.live.bilibili.com/xlive/web-ucenter/v1/sign/DoSign'
    content = requests.get(live_sign_url, headers=headers, cookies=cookie).text
    sign_msg = json.loads(content)
    global sign_days
    sign_days=''
    if sign_msg['code'] == 0:
        print('Live has been signed in successfully!', sign_msg['data']['text'])
        print(sign_msg['data']['text'])
        sign_days = sign_msg['data']['hadSignDays']
    else:
        print('INFO:直播' + sign_msg['message'])

    if coined == 50:
        print("INFO:All tasks have been completed, no need to execute again!")
        return

    for i in range(0, 10):
        if finish_flag == 1:
            print('INFO:All tasks have been completed')
            break

        video_dict = getVideo()
        bvid = video_dict['list'][0]['bvid']  # bvid就是视频的BV号
        aid = video_dict['list'][0]['aid']  # aid与bvid一样,每个视频aid唯一,也可以根据aid搜索视频

        print(str(i) + ' ---- ' + str(bvid) + ' ---- ' + str(aid))
        if not finish_msg()[0]:
            toview(bvid)
            time.sleep(3)
        else:
            print('INFO:The viewing task has been completed')
        if not finish_msg()[1]:
            shareVideo(bvid)
            time.sleep(3)
        else:
            print('INFO:The sharing task has been completed')

        if coin_num == 0:
            print('INFO:Not enough coin,coin failed!')
            if finish_msg()[1]:
                return
            continue

        if coin_count < num:
            coin_code = tocoin(bvid)
            if coin_code == -99:
                if finish_msg()[1]:
                    return
                continue
            if coin_code == 1:
                coin_count = coin_count + 1
            if coin_count == num:
                finish_flag=1

        print('----------------------')

# 观看视频【不会点赞投币】
def toview(bvid):
    playedTime = random.randint(10, 100)
    url = "https://api.bilibili.com/x/click-interface/web/heartbeat"
    data = {
        'bvid': bvid,
        'played_time': playedTime,
        'csrf': csrf,
    }
    cookie = extract_cookies(cookies)
    view = requests.post(url, data=data, headers=headers, cookies=cookie).text
    view_msg = json.loads(view)
    code = view_msg['code']
    if code == 0:
        print('watching viedo successful!')
    else:
        print('watching viedo failed!')


# 分享视频
def shareVideo(bvid):
    url = "https://api.bilibili.com/x/web-interface/share/add"
    data = {
        'bvid': bvid,
        'csrf': csrf
    }
    cookie = extract_cookies(cookies)
    # 需要请求头
    header = {
        "Content-Type": "application/x-www-form-urlencoded",
        "Connection": "keep-alive",
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36 Edg/93.0.961.38",
    }

    share = requests.post(url, data=data, cookies=cookie, headers=header).text
    share_msg = json.loads(share)
    code = share_msg['code']
    if code == 0:
        print('share  successful!')

    else:
        print('share failed!')


# 投币函数
def tocoin(bvid):
    coinNum = getCoin()
    if coinNum <= 1:
        print('INFO:Not enough coin,coin failed!')
        return -99
    url = "http://api.bilibili.com/x/web-interface/coin/add"
    data = {
        'bvid': bvid,
        'multiply': 1,
        'select_like': 1,
        'csrf': csrf
    }
    cookie = extract_cookies(cookies)
    coin = requests.post(url, data=data, headers=headers, cookies=cookie).text
    coin_msg = json.loads(coin)
    code = coin_msg['code']
    if code == 0:
        print(str(bvid) + ' toaddcoin successful !')
        return 1
    else:
        print(str(bvid) + ' toaddcoin failed!')
        return 0


# 开启任务运行
def run():
    getInfo()
    Task()


# 云函数使用
def main_handler(event, context):
    print("Received event: " + json.dumps(event, indent=2))
    print("Received context: " + str(context))
    run()
    return ("------ end ------")


if __name__ == '__main__':
    cookie = extract_cookies(cookies)
    # 获取API返回值(验证Cookie是否失效)
    result = requests.get('https://api.bilibili.com/x/web-interface/nav', headers=headers,
                          cookies=cookie).text  # 获取当前用户登录信息
    result_j = json.loads(result)
    if result_j['code'] != 0:  # 账号未登录(-101),请求错误(-400),或者是其他原因导致的失败
        print("请正确填写有效的Cookie!")
    else:
        run()

 

免责声明

本站提供的一切软件、教程和内容信息仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络收集整理,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。我们非常重视版权问题,如有侵权请邮件carillai@qq.com与我们联系处理。敬请谅解!

 

赞(7) 打赏
未经允许不得转载:H深夜笔记 » bilibili自动做任务升级脚本源码

评论 2

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #1

    运行一次成功后,第二天出现以下错误。如果有时间烦请看看多谢
    Welcome! xxx Current level is 5 ,Current experience are 13710,Not far from upgrading that just need 15090 exp ,about 232 days,Remaining silver coins are 124
    —————————————————————————
    TypeError Traceback (most recent call last)
    in
    260 print(“请正确填写有效的Cookie!”)
    261 else:
    –> 262 run()

    2 frames
    in finish_msg()
    74 exp_msg = json.loads(video_exp)
    75
    —> 76 watch_flag = reward_msg[‘data’][‘watch’] # 视频观看完成情况
    77 share_flag = reward_msg[‘data’][‘share’] # 视频分享完成情况
    78 coin_flag = exp_msg[‘data’] # 视频投币获得经验数,每日最大为50

    TypeError: ‘NoneType’ object is not subscriptable

    1231年前 (2022-12-31)回复
  2. #2

    in finish_msg()
    74 exp_msg = json.loads(video_exp)
    75
    —> 76 watch_flag = reward_msg[‘data’][‘watch’] # 视频观看完成情况
    77 share_flag = reward_msg[‘data’][‘share’] # 视频分享完成情况
    78 coin_flag = exp_msg[‘data’] # 视频投币获得经验数,每日最大为50

    TypeError: ‘NoneType’ object is not subscriptable

    1231年前 (2022-12-31)回复

H深夜笔记 爱分享 爱生活

联系我联系我

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏