Skip to content

Commit

Permalink
Merge pull request Dawnnnnnn#24 from Dawnnnnnn/master
Browse files Browse the repository at this point in the history
update
  • Loading branch information
yjqiang authored Apr 3, 2018
2 parents bed8609 + b49ac7a commit ef47247
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 105 deletions.
111 changes: 74 additions & 37 deletions API.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,23 @@
import requests
import datetime
import time
import math


def CurrentTime():
currenttime = str(int(time.mktime(datetime.datetime.now().timetuple())))
return currenttime
currenttime = int(time.mktime(datetime.datetime.now().timetuple()))
return str(currenttime)

def calculate_sign(str):
#value = self.GetHash()
# key = value['key']
#Hash = str(value['hash'])
# pubkey = rsa.PublicKey.load_pkcs1_openssl_pem(key.encode())
# password = base64.b64encode(rsa.encrypt((Hash + password).encode('utf-8'), pubkey))
# password = parse.quote_plus(password)
# username = parse.quote_plus(username)
hash = hashlib.md5()
hash.update(str.encode('utf-8'))
sign = hash.hexdigest()
return sign

class API():

def __init__(self, bilibili):
self.bilibili = bilibili
# 本函数只是实现了直播观看历史里的提交,与正常观看仍有区别!!
# 其实csrf_token就是用了token,我懒得再提出来了
# 就是Login函数里面的cookie[0]['value']



def post_watching_history(self,csrf_token, room_id):
data = {
Expand All @@ -39,8 +29,8 @@ def post_watching_history(self,csrf_token, room_id):
}
url = "https://api.live.bilibili.com/room/v1/Room/room_entry_action"
response = requests.post(url, data=data, headers=self.bilibili.pcheaders)
#print(response.json())
return 0

def check_room_true(self,roomid):
url = "https://api.live.bilibili.com/room/v1/Room/room_init?id="+str(roomid)
response = requests.get(url,headers=self.bilibili.pcheaders)
Expand All @@ -50,34 +40,81 @@ def check_room_true(self,roomid):
param3 = response.json()['data']['encrypted']
return param1,param2,param3

def silver2coin(self):
url = "https://api.live.bilibili.com/exchange/silver2coin"
response = requests.post(url,headers=self.bilibili.pcheaders)
print("#",response.json()['msg'])
temp_params = 'access_key=' + self.bilibili.access_key + '&actionKey=' + self.bilibili.actionKey + '&appkey=' + self.bilibili.appkey + '&build=' + self.bilibili.build + '&device=' + self.bilibili.device + '&mobi_app=' + self.bilibili.mobi_app + '&platform=' + self.bilibili.platform + '&ts=' + CurrentTime()
params = temp_params + self.bilibili.app_secret
hash = hashlib.md5()
hash.update(params.encode('utf-8'))
app_url = "https://api.live.bilibili.com/AppExchange/silver2coin?"+temp_params+"&sign="+str(hash.hexdigest())
response1 = requests.post(app_url,headers=self.bilibili.appheaders)
print("#",response1.json()['msg'])

def get_bag_list(self):
url = "https://api.live.bilibili.com/gift/v2/gift/m_bag_list?" + 'access_key='+self.bilibili.access_key+'&actionKey='+self.bilibili.actionKey+'&appkey='+self.bilibili.appkey+'&build='+self.bilibili.build+'&device='+self.bilibili.device + '&mobi_app='+self.bilibili.mobi_app+'&platform='+self.bilibili.platform + '&ts=' + CurrentTime()
response = requests.get(url, headers=self.bilibili.pcheaders)
url = "http://api.live.bilibili.com/gift/v2/gift/bag_list"
response = requests.get(url,headers=self.bilibili.pcheaders)
temp = []
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), '查询可用礼物')
for i in range(len(response.json()['data'])):
gift_name = response.json()['data'][i]['gift_name']
gift_num = str(response.json()['data'][i]['gift_num']).center(4)
expireat = str(round(int(response.json()['data'][i]['expireat']) / 86400, 1)).center(6)
print("# " + gift_name + 'X' + gift_num, '(在' + expireat + '天后过期)')

for i in range(len(response.json()['data']['list'])):
bag_id = (response.json()['data']['list'][i]['bag_id'])
gift_id = (response.json()['data']['list'][i]['gift_id'])
gift_num = str((response.json()['data']['list'][i]['gift_num'])).center(4)
gift_name = response.json()['data']['list'][i]['gift_name']
expireat = (response.json()['data']['list'][i]['expire_at'])
left_time = (expireat-int(CurrentTime()))
left_days = (expireat-int(CurrentTime()))/86400
print("# " + gift_name + 'X' + gift_num, '(在'+str(math.ceil(left_days))+'天后过期)')
if 0 < int(left_time) < 86400:
temp.append([gift_id,gift_num,bag_id])
return temp

def get_uid_in_room(self, roomID):
url = "https://api.live.bilibili.com/room/v1/Room/room_init?id=" + roomID
response = requests.get(url, headers=self.bilibili.pcheaders)
return response.json()['data']['uid'],response.json()['data']['room_id']

def send_bag_gift_web(self, roomID, giftID, giftNum, bagID):
url = "http://api.live.bilibili.com/gift/v2/live/bag_send"
temp = self.get_uid_in_room(roomID)
data = {
'uid': self.bilibili.uid,
'gift_id': giftID,
'ruid': temp[0],
'gift_num': giftNum,
'bag_id': bagID,
'platform': 'pc',
'biz_code': 'live',
'biz_id': temp[1],
'rnd': CurrentTime(),
'storm_beat_id': '0',
'metadata': '',
'price': '0',
'csrf_token': self.bilibili.csrf
}
response = requests.post(url, headers=self.bilibili.pcheaders, data=data)
try:
print(response.json())
print("# 清理快到期礼物:",response.json()['data']['gift_name']+"x"+str(response.json()['data']['gift_num']))
except:
print("# 清理快到期礼物成功,但请联系开发者修bug!")

def user_info(self):
url = "https://api.live.bilibili.com/User/getUserInfo?ts=" + CurrentTime()
url = "https://api.live.bilibili.com/i/api/liveinfo"
response = requests.get(url, headers=self.bilibili.pcheaders)
json = response.json()
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), '查询用户信息')
if(json['code'] == 'REPONSE_OK'):
data = json['data']
uname = data['uname']
silver = data['silver']
gold = data['gold']
achieve = data['achieve']
user_level = data['user_level']
user_next_level = data['user_next_level']
user_intimacy = data['user_intimacy']
user_next_intimacy = data['user_next_intimacy']
user_level_rank = data['user_level_rank']
billCoin = data['billCoin']
if(response.json()['code'] == 0):
uname = response.json()['data']['userInfo']['uname']
achieve = response.json()['data']['achieves']
user_level = response.json()['data']['userCoinIfo']['user_level']
silver = response.json()['data']['userCoinIfo']['silver']
gold = response.json()['data']['userCoinIfo']['gold']
user_next_level = response.json()['data']['userCoinIfo']['user_next_level']
user_intimacy = response.json()['data']['userCoinIfo']['user_intimacy']
user_next_intimacy = response.json()['data']['userCoinIfo']['user_next_intimacy']
user_level_rank = response.json()['data']['userCoinIfo']['user_level_rank']
billCoin = response.json()['data']['userCoinIfo']['coins']
print('# 用户名', uname)
print('# 银瓜子', silver)
print('# 金瓜子', gold)
Expand Down
4 changes: 1 addition & 3 deletions LotteryResult.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ def __init__(self, bilibili):
self.bilibili =bilibili

async def query(self):
while 1:
# print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), "检查抽奖结果")
# print(self.bilibili.activity_raffleid_list)
while 1:
if self.bilibili.activity_raffleid_list:
for i in range(0,len(self.bilibili.activity_roomid_list)):
url = "http://api.live.bilibili.com/activity/v1/Raffle/notice?roomid="+str(self.bilibili.activity_roomid_list[0])+"&raffleId="+str(self.bilibili.activity_raffleid_list[0])
Expand Down
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
应援团签到
获取心跳礼物
20倍节奏风暴领取
获取总督开通奖励(实验性)
实物抽奖(实验性)
获取总督开通奖励
实物抽奖
清空当日到期礼物
银瓜子兑换硬币

更新说明
Expand Down Expand Up @@ -76,6 +78,13 @@
3.30:

增加一个防钓鱼措施
4.1:

活动名称显示,总督奖励领取修复,修改结构
4.2:

添加自动清空快到期礼物的功能,添加银瓜子兑换硬币功能
on/off 的配置选项:1代表on,0代表off

环境:
------
Expand All @@ -84,17 +93,13 @@
第三方库配置:
------

pip install requests
pip install rsa
pip install aiohttp
pip install webcolors

pip install -r requirements.txt

使用方法:
------

第一种:
自行按照百度配置python运行环境(要把那个pip的勾上!),并安装所需第三方库,最后执行python run.py
自行按照百度配置python运行环境(要把那个PATH的勾上!),并安装所需第三方库,最后执行python run.py
第二种:
下载release中的exe版本,双击运行(已恢复)
第三种:
Expand All @@ -116,5 +121,3 @@

本项目采用MIT开源协议



38 changes: 26 additions & 12 deletions Tasks.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from API import API
from bilibili import bilibili
import hashlib
import datetime
Expand All @@ -8,19 +9,18 @@

class Tasks():

def __init__(self, bilibili):
def __init__(self, bilibili, configloader,api):
self.bilibili = bilibili
self.configloader = configloader
self.api = api

# 获取每日包裹奖励
def Daily_bag(self):
url = 'http://api.live.bilibili.com/gift/v2/live/receive_daily_bag'
response = requests.get(url, headers=self.bilibili.pcheaders)
try:
print("# 获得-" + response.json()['data']['bag_list'][0]['bag_name'] + "-成功")
print("# 获得-" + response.json()['data']['bag_list'][1]['bag_name'] + "-成功")
print("# 获得-" + response.json()['data']['bag_list'][2]['bag_name'] + "-成功")
except:
pass
for i in range(0,len(response.json()['data']['bag_list'])):
print("# 获得-" + response.json()['data']['bag_list'][i]['bag_name'] + "-成功")


def CurrentTime(self):
currenttime = str(int(time.mktime(datetime.datetime.now().timetuple())))
Expand All @@ -36,12 +36,8 @@ def DoSign(self):
# 领取每日任务奖励
def Daily_Task(self):
url = 'https://api.live.bilibili.com/activity/v1/task/receive_award'
#payload1 = {'task_id': 'single_watch_task'}
#response1 = requests.post(url, data=payload1, headers=self.appheaders)
payload2 = {'task_id': 'double_watch_task'}
response2 = requests.post(url, data=payload2, headers=self.bilibili.appheaders)
#payload3 = {'task_id': 'share_task'}
#response3 = requests.post(url, data=payload3, headers=self.appheaders)
print("# 双端观看直播:", response2.json()["msg"])

# 应援团签到
Expand Down Expand Up @@ -74,12 +70,30 @@ def link_sign(self):
else:
print("# 应援团 %s 应援失败" %(i1))

def send_gift(self):
if self.configloader.dic_user['gift']['on/off'] == "1":
try:
argvs = self.api.get_bag_list()
for i in range(0,len(argvs)):
giftID = argvs[i][0]
giftNum = argvs[i][1]
bagID = argvs[i][2]
roomID = self.configloader.dic_user['gift']['send_to_room']
self.api.send_bag_gift_web(roomID,giftID,giftNum,bagID)
except:
print("# 没有将要过期的礼物~")

def sliver2coin(self):
if self.configloader.dic_user['coin']['on/off'] == "1":
self.api.silver2coin()

async def run(self):
while 1:
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), "每日任务")
self.send_gift()
self.sliver2coin()
self.DoSign()
self.Daily_bag()
self.Daily_Task()
self.link_sign()

await asyncio.sleep(21600)
40 changes: 13 additions & 27 deletions bilibiliCilent.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ async def parseDanMu(self, messages):

if cmd == 'DANMU_MSG':
# print(dic)
self.printer.printlist_append(['danmu', '弹幕', 'user', dic])
# self.printer.printlist_append(['danmu', '弹幕', 'user', dic])
pass
if cmd == 'SYS_GIFT':
if 'giftId' in dic.keys():
Expand Down Expand Up @@ -379,41 +379,27 @@ async def parseDanMu(self, messages):
except:
pass
if cmd == 'GUARD_MSG':
print(dic)
try:
a = re.compile(r"(?<=在主播 )\S+(?= 的直播间开通了总督)")
res = a.findall(str(dic))
search_url = "https://search.bilibili.com/api/search?search_type=live&keyword=" + str(res[0])
response = requests.get(search_url)
roomid = response.json()['result']['live_user'][0]['roomid']
temp_params = 'access_key=' + self.bilibili.access_key + '&actionKey=' + self.bilibili.actionKey + '&appkey=' + self.bilibili.appkey + '&build=' + self.bilibili.build + '&device=' + self.bilibili.device + \
'&mobi_app=' + self.bilibili.mobi_app + '&platform=' + self.bilibili.platform + '&roomid=' + str(
roomid) + '&ts=' + CurrentTime() + "&type=guard"
params = temp_params + self.bilibili.app_secret
hash = hashlib.md5()
hash.update(params.encode('utf-8'))
true_url = 'https://api.live.bilibili.com/lottery/v1/lottery/check?' + temp_params + '&sign=' + str(
hash.hexdigest())
response1 = requests.get(true_url, headers=self.bilibili.appheaders)
print(roomid)
true_url = 'https://api.live.bilibili.com/lottery/v1/lottery/check?roomid=' + str(roomid)
response1 = requests.get(true_url)
print(response1.json())
num = len(response1.json()['data']['guard'])
for i in range(0, num):
ts = CurrentTime()
id = response1.json()['data']['guard'][i]['id']
temp_params = 'access_key=' + self.bilibili.access_key + '&actionKey=' + self.bilibili.actionKey + '&appkey=' + self.bilibili.appkey + '&build=' + self.bilibili.build + '&device=' + self.bilibili.device + '&id=' + str(
id) + '&mobi_app=' + self.bilibili.mobi_app + '&platform=' + self.bilibili.platform + '&roomid=' + str(
roomid) + '&ts=' + ts + "&type=guard"
params = temp_params + self.bilibili.app_secret
hash = hashlib.md5()
hash.update(params.encode('utf-8'))
print(id)
join_url = "https://api.live.bilibili.com/lottery/v1/lottery/join"
payload = {"access_key": self.bilibili.access_key, "actionKey": self.bilibili.actionKey,
"appkey": self.bilibili.appkey, "build": self.bilibili.build,
"device": self.bilibili.device,
"id": id, "mobi_app": self.bilibili.mobi_app, "platform": self.bilibili.platform,
"roomid": roomid,
"ts": ts, "type": "guard", "sign": hash.hexdigest()}
response2 = requests.post(join_url, data=payload, headers=self.bilibili.appheaders)
print("# 获取到某个总督的奖励:", response2.json()['data']['message'])
payload = {"roomid": roomid, "id": id, "type": "guard", "csrf_token": self.bilibili.csrf}
print(payload)
response2 = requests.post(join_url, data=payload, headers=self.bilibili.pcheaders)
print(response2.json())

except:
self.printer.printlist_append(['join_lotter', '', 'develop', "# 没领取到奖励,请联系开发者"])
return
self.printer.printlist_append(['join_lotter', '', 'user', "# 没领取到奖励,请联系开发者"])
return
3 changes: 2 additions & 1 deletion conf/bilibili.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ platform = android
app_secret = 560c52ccd288fed045859ed18bffd973
access_key =
cookie =
csrf =
csrf =
uid =
_CIDInfoUrl = http://live.bilibili.com/api/player?id=cid:
_ChatPort = 2243
_protocolversion = 1
Expand Down
7 changes: 6 additions & 1 deletion conf/user.conf
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
[platform]
platform = other

[task]
[gift]
on/off = 0
send_to_room =

[coin]
on/off = 0

[account]
username =
Expand Down
2 changes: 1 addition & 1 deletion configloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def hex_to_rgb_percent(hex_str):
color = webcolors.hex_to_rgb_percent(hex_str)
# print([float(i.strip('%'))/100.0 for i in color])
return [float(i.strip('%'))/100.0 for i in color]


# "255 255 255"
def rgb_to_percent(rgb_str):
Expand Down
Loading

0 comments on commit ef47247

Please sign in to comment.