Skip to content
This repository has been archived by the owner on Jun 6, 2023. It is now read-only.

Commit

Permalink
集成小心心获取及自动刷新勋章功能 fix #324 #309
Browse files Browse the repository at this point in the history
  • Loading branch information
Dawnnnnnn committed Jul 26, 2020
1 parent 093768e commit f6fcd9b
Show file tree
Hide file tree
Showing 12 changed files with 320 additions and 244 deletions.
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ ENV LIBRARY_PATH=/lib:/usr/lib \
WORKDIR /app

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories && \
apk add --no-cache tzdata && \
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
echo "Asia/Shanghai" > /etc/timezone && \
apk del tzdata && \
apk add --no-cache build-base git && \
git clone https://github.com/Dawnnnnnn/bilibili-live-tools.git /app && \
pip3 install --no-cache-dir -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/ && \
Expand Down
10 changes: 4 additions & 6 deletions OnlineHeart.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
from printer import Printer




def CurrentTime():
currenttime = int(time.mktime(datetime.datetime.now().timetuple()))
return str(currenttime)
Expand Down Expand Up @@ -39,7 +37,7 @@ async def check_winner(self, i, g, start_time):
Statistics().add_to_result(winner["giftTitle"], 1)

async def draw_lottery(self):
black_list = ["123", "1111", "测试", "測試", "测一测", "ce-shi", "test", "T-E-S-T", "lala", "我是抽奖标题", "压测", # 已经出现
black_list = ["123", "1111", "测试", "測試", "测一测", "ce-shi", "test", "T-E-S-T", "lala", "我是抽奖标题", "压测", # 已经出现
"測一測", "TEST", "Test", "t-e-s-t"] # 合理猜想
former_lottery = queue.Queue(maxsize=4)
[former_lottery.put(True) for _ in range(4)]
Expand All @@ -61,7 +59,7 @@ async def draw_lottery(self):
for k in black_list:
if k in title or k in jp_list:
Printer().printer(f"检测到 {i} 号疑似钓鱼类测试抽奖『{title}>>>{jp_list}』" + \
",默认不参与,请自行判断抽奖可参与性","Warning","red")
",默认不参与,请自行判断抽奖可参与性", "Warning", "red")
break
else:
if bilibili().black_status:
Expand All @@ -81,11 +79,11 @@ async def draw_lottery(self):
async def run(self):
while 1:
try:
Printer().printer("心跳", "Info","green")
Printer().printer("心跳", "Info", "green")
response = await self.pcpost_heartbeat()
json_response = await response.json(content_type=None)
if json_response['code'] in [3, -101]:
Printer().printer(f"cookie过期,将重新登录","Error","red")
Printer().printer(f"cookie过期,将重新登录", "Error", "red")
login().login()
response1 = await self.apppost_heartbeat()
json_response1 = await response1.json(content_type=None)
Expand Down
3 changes: 1 addition & 2 deletions TCP_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

from printer import Printer
from pkLottery import PKLottery
from guardLottery import GuardLottery
from rafflehandler import Rafflehandler


Expand Down Expand Up @@ -153,7 +152,7 @@ async def parseDanMu(self, dic):
elif cmd == 'Storm':
pass
elif cmd == 'Guard':
await GuardLottery().guard_join(dic["data"]["RoomId"], dic["data"]["Id"])
pass
elif cmd == 'PKLottery':
await PKLottery().pk_join(dic["data"]["RoomId"], dic["data"]["Id"])
elif cmd == 'Raffle':
Expand Down
46 changes: 46 additions & 0 deletions Tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os
import configloader
import utils
import ast
from printer import Printer


Expand Down Expand Up @@ -85,6 +86,8 @@ async def auto_send_gift(self):
# id = json_res['data'][j]['id']
# temp_dic[id] = price
temp_dic = {1: 100, 6: 1000}
if self.dic_user['send_exheart']['on/off'] == "1":
temp_dic = {1: 100, 6: 1000, 30607: 5000}
x, temp = await utils.fetch_bag_list(printer=False)
roomid = a[0]
today_feed = a[1]
Expand Down Expand Up @@ -130,11 +133,54 @@ async def sliver2coin(self):
json_response1 = await response1.json()
Printer().printer(f"银瓜子兑换硬币状态:{json_response1['msg']}", "Info", "green")

async def refresh_medals(self):
if self.dic_user['refresh_medals']['on/off'] == '1':
await utils.refresh_all_gray_medals()

async def refresh_medals_by_roomid(self):
if self.dic_user['refresh_medals_by_roomid']['on/off'] == "1":
roomids = ast.literal_eval(self.dic_user['refresh_medals_by_roomid']['room_ids'])
await utils.refresh_medals_by_roomids(roomids)

async def get_rooms(self):
room_ids = []
response = await bilibili().request_fetchmedal()
json_response = await response.json(content_type=None)
for i in range(0, len(json_response['data']['fansMedalList'])):
short_room_id = json_response['data']['fansMedalList'][i]['roomid']
response1 = await bilibili().get_room_info(short_room_id)
json_response1 = await response1.json(content_type=None)
long_room_id = json_response1['data']['room_info']['room_id']
room_ids.append(long_room_id)
return room_ids

async def XE_heartbeat(self, room_ids, room_id):
index_num = round(24 / len(room_ids))
data = await bilibili().heart_beat_e(room_id)
for index in range(1, index_num + 1):
try:
# print(f"房间{room_id}休眠{data['heartbeat_interval']}s后开始第 {index} 次")
await asyncio.sleep(data['heartbeat_interval'])
response = await bilibili().heart_beat_x(index, data, room_id)
response = await response.json(content_type=None)
data['ets'] = response['data']['timestamp']
data['secret_key'] = response['data']['secret_key']
data['heartbeat_interval'] = response['data']['heartbeat_interval']
except:
pass

async def run(self):
while 1:
try:
Printer().printer(f"开始执行每日任务", "Info", "green")
await self.DoSign()
room_ids = await self.get_rooms()
coroutine_list = []
for room_id in room_ids:
coroutine_list.append(self.XE_heartbeat(room_ids, room_id))
await asyncio.wait(coroutine_list)
await self.refresh_medals_by_roomid()
await self.refresh_medals()
await self.Daily_bag()
await self.Daily_Task()
await self.link_sign()
Expand Down
102 changes: 90 additions & 12 deletions bilibili.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import requests
import rsa
import base64
import uuid
from urllib import parse
from printer import Printer
import aiohttp
Expand Down Expand Up @@ -241,7 +242,7 @@ def request_load_img(self, url):
return requests.get(url)

async def request_fetchmedal(self):
url = 'https://api.live.bilibili.com/i/api/medal?page=1&pageSize=50'
url = 'https://api.live.bilibili.com/i/api/medal?page=1&pageSize=168'
response = await self.bili_section_get(url, headers=self.dic_bilibili['pcheaders'])
return response

Expand Down Expand Up @@ -432,17 +433,9 @@ async def heart_gift(self):
response = await self.bili_section_get(url, headers=self.dic_bilibili['pcheaders'])
return response

async def guard_list(self):
url = "http://118.25.108.153:8080/guard"
headers = {
"User-Agent": "bilibili-live-tools/" + str(self.dic_bilibili['uid'])
}
response = requests.get(url, headers=headers, timeout=3)
return response

async def guard_list_v2(self):
url = "https://api.live.bilibili.com/xlive/web-room/v1/index/getInfoByRoom?room_id=465671"
response = requests.get(url, headers=self.dic_bilibili['pcheaders'], timeout=3)
async def get_room_info(self, roomid):
url = f"https://api.live.bilibili.com/xlive/web-room/v1/index/getInfoByRoom?room_id={roomid}"
response = await self.bili_section_get(url, headers=self.dic_bilibili['pcheaders'], timeout=3)
return response

async def pk_list(self):
Expand Down Expand Up @@ -568,3 +561,88 @@ async def req_area_list(self):
url = "http://api.live.bilibili.com/room/v1/Area/getList"
response = await self.bili_section_get(url)
return response

async def heart_beat_e(self, room_id):
response = await self.get_room_info(room_id)
json_response = await response.json(content_type=None)
parent_area_id = json_response['data']['room_info']['parent_area_id']
area_id = json_response['data']['room_info']['area_id']

url = 'https://live-trace.bilibili.com/xlive/data-interface/v1/x25Kn/E'
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Origin': 'https://live.bilibili.com',
'Referer': f'https://live.bilibili.com/{room_id}',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36',
'Cookie': self.dic_bilibili['cookie'],
}
payload = {
'id': [parent_area_id, area_id, 0, room_id],
'device': f'["{self.calc_sign(str(uuid.uuid4()))}","{uuid.uuid4()}"]',
'ts': int(time.time()) * 1000,
'is_patch': 0,
'heart_beat': [],
'ua': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36',
'csrf_token': self.dic_bilibili['csrf'],
'csrf': self.dic_bilibili['csrf'],
'visit_id': ''
}
data = parse.urlencode(payload)
# {"code":0,"message":"0","ttl":1,"data":{"timestamp":1595342828,"heartbeat_interval":300,"secret_key":"seacasdgyijfhofiuxoannn","secret_rule":[2,5,1,4],"patch_status":2}}
response = await self.bili_section_post(url, headers=headers, data=data)
response = await response.json(content_type=None)
payload['ets'] = response['data']['timestamp']
payload['secret_key'] = response['data']['secret_key']
payload['heartbeat_interval'] = response['data']['heartbeat_interval']
payload['secret_rule'] = response['data']['secret_rule']
return payload

async def heart_beat_x(self, index, payload, room_id):
response = await self.get_room_info(room_id)
json_response = await response.json(content_type=None)
parent_area_id = json_response['data']['room_info']['parent_area_id']
area_id = json_response['data']['room_info']['area_id']
url = 'https://live-trace.bilibili.com/xlive/data-interface/v1/x25Kn/X'
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Origin': 'https://live.bilibili.com',
'Referer': f'https://live.bilibili.com/{room_id}',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36',
'Cookie': self.dic_bilibili['cookie'],
}
s_data = {
"t": {
'id': [parent_area_id, area_id, index, room_id],
"device": payload['device'], # LIVE_BUVID
"ets": payload['ets'],
"benchmark": payload['secret_key'],
"time": payload['heartbeat_interval'],
"ts": int(time.time()) * 1000,
"ua": payload['ua']
},
"r": payload['secret_rule']
}
t = s_data['t']
payload = {
's': self.generate_s(s_data),
'id': t['id'],
'device': t['device'],
'ets': t['ets'],
'benchmark': t['benchmark'],
'time': t['time'],
'ts': t['ts'],
"ua": t['ua'],
'csrf_token': self.dic_bilibili['csrf'],
'csrf': self.dic_bilibili['csrf'],
'visit_id': '',
}
payload = parse.urlencode(payload)
# {"code":0,"message":"0","ttl":1,"data":{"heartbeat_interval":300,"timestamp":1595346846,"secret_rule":[2,5,1,4],"secret_key":"seacasdgyijfhofiuxoannn"}}
response = await self.bili_section_post(url, headers=headers, data=payload)
return response

# 加密s
def generate_s(self, data):
url = 'http://127.0.0.1:3000/enc'
response = requests.post(url, json=data).json()
return response['s']
Loading

9 comments on commit f6fcd9b

@superexboom
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有个问题 要怎么知道自己部署的小心心的那个服务已经被你的脚本正确的使用了呢?
是否有提示关于小心心的?

@Dawnnnnnn
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有个问题 要怎么知道自己部署的小心心的那个服务已经被你的脚本正确的使用了呢?
是否有提示关于小心心的?

看部署的node server,如果py脚本运行五分钟后有显示数据,就说明正常

@superexboom
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有个问题 要怎么知道自己部署的小心心的那个服务已经被你的脚本正确的使用了呢?
是否有提示关于小心心的?

看部署的node server,如果py脚本运行五分钟后有显示数据,就说明正常

那就坏起来了 我这都装完之后 跑app.js就只提示个当前正在监听端口3000
然后就啥都没有了 甚至卡在了当前进程里
所以使用上是我整错了?
(或许可能需要在原作者那开个issues了)

@Dawnnnnnn
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那小心心增加了吗

@superexboom
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那小心心增加了吗

并没有 挂了一晚上 什么也没发生

@Dawnnnnnn
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那大概是没进行心跳...
你是有勋章的吧

@superexboom
Copy link

@superexboom superexboom commented on f6fcd9b Jul 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

对的,有勋章的
刚刚重新开了下监听 等了一阵子 依旧没有变化
顺便提一嘴 是跑的app.js 而不是example.py吧?

@Dawnnnnnn
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是的

@superexboom
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我去原作者那问一圈好了
虽然我觉得可能和我的环境有关系
总觉得这最新的nodejs有点问题 好多东西 似乎都有点兼容性上的问题

Please sign in to comment.