云函数运行会莫名其妙的报错(我也不知道为什么,我这边测试有时候报错,有时候不会),并非代码有错,建议在本地运行。
最后更新于2022-1-18
以上每天经验共65点经验。
1、浏览器登入哔哩网站
2、访问 http://api.bilibili.com/x/space/myinfo
3、F12看到cookie的值粘贴即可
# -*- coding: utf-8 -*-
import requests
import json
import time
import random
'''
1、浏览器登入哔哩网站
2、访问 http://api.bilibili.com/x/space/myinfo
3、F12看到cookie的值粘贴即可
'''
cookies = ""
coin_operated = 0 # (投币开关,0不投币,1投币)
def extract_cookies(cookies):
global csrf
cookies = dict([l.split("=", 1) for l in cookies.split("; ")])
csrf = cookies['bili_jct']
return cookies
def getCoin():
cookie = extract_cookies(cookies)
url = "http://account.bilibili.com/site/getCoin"
r = requests.get(url, cookies=cookie).text
j = json.loads(r)
money = j['data']['money']
return int(money)
def getInfo():
global uid
url = "http://api.bilibili.com/x/space/myinfo"
cookie = extract_cookies(cookies)
j = requests.get(url, cookies=cookie).json()
username = j['data']['name']
uid = j['data']['mid']
level = j['data']['level']
current_exp = j['data']['level_exp']['current_exp']
next_exp = j['data']['level_exp']['next_exp']
sub_exp = int(next_exp) - int(current_exp)
if coin_operated:
days = int(int(sub_exp) / 65)
else:
days = int(int(sub_exp) / 15)
coin = getCoin()
msg = f"欢迎 {username}! 当前等级:" + str(level) + " ,当前经验:" + \
str(current_exp) + ",升级还差 " + str(sub_exp) + \
"经验,还需" + str(days) + "天," + "当前硬币数量:" + str(coin)
return current_exp, msg
def getActiveInfo():
url = "http://api.bilibili.com/x/web-interface/archive/related?aid=" + \
str(7)
cookie = extract_cookies(cookies)
r = requests.get(url, cookies=cookie).text
j = json.loads(r)
return j
def getVideo():
random_page = random.randint(0, 167)
url = "http://api.bilibili.cn/recommend?page=" + str(random_page)
cookie = extract_cookies(cookies)
r = requests.get(url, cookies=cookie).text
j = json.loads(r)
return j
def Task():
coin_num = getCoin()
coin_count = 0
for i in range(0, 10):
j = getVideo()
list_len = len(j['list'])
random_list = random.randint(1, list_len - 1)
bvid = j['list'][random_list]['bvid']
aid = j['list'][random_list]['aid']
print(str(i + 1) + ' ---- ' + str(bvid) + ' ---- ' + str(aid))
toview(bvid)
time.sleep(3)
shareVideo(bvid)
time.sleep(3)
if coin_operated:
while coin_count < min(coin_num, 5):
coin_code = tocoin(bvid)
if coin_code == 1:
coin_count += 1
break
elif coin_code == -99:
break
else:
pass
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)
j = requests.post(url, data=data, cookies=cookie).json()
code = j['code']
if code == 0:
print('看视频成功!')
else:
print('看视频失败!')
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",
}
r = requests.post(url, data=data, cookies=cookie, headers=header).text
j = json.loads(r)
code = j['code']
if code == 0:
print('分享成功!')
else:
print('分享失败!')
def tocoin(bvid):
coinNum = getCoin()
if coinNum == 0:
print('太穷了,硬币不够!')
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)
r = requests.post(url, data=data, cookies=cookie).text
j = json.loads(r)
code = j['code']
if code == 0:
print(str(bvid) + ' 投币成功!')
return 1
else:
print(str(bvid) + ' 投币失败!')
return 0
def run():
exp1, msg = getInfo()
print(msg)
Task()
exp2, c = getInfo()
print(f'任务完成,获得经验{int(exp2) - int(exp1)}')
def main_handler(event, context):
print("Received event: " + json.dumps(event, indent=2))
print("Received context: " + str(context))
run()
return "------ end ------"
if __name__ == '__main__':
run()
手机扫一扫
移动阅读更方便
你可能感兴趣的文章