From 50017483d848b100dd0d32fc22f76305118d08db Mon Sep 17 00:00:00 2001 From: yjqiang Date: Mon, 22 Apr 2019 22:58:49 +0800 Subject: [PATCH] buy medal --- bili_console.py | 7 ++++++- reqs/custom.py | 9 +++++++++ tasks/custom.py | 13 +++++++++++-- tasks/utils.py | 9 +++++++++ 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/bili_console.py b/bili_console.py index a81f2c1..7dd42c7 100644 --- a/bili_console.py +++ b/bili_console.py @@ -5,7 +5,7 @@ from cmd import Cmd import getopt from tasks.utils import UtilsTask -from tasks.custom import SendLatiaoTask, BuyLatiaoTask +from tasks.custom import SendLatiaoTask, BuyLatiaoTask, BuyMedalTask from tasks.main_daily_job import JudgeCaseTask @@ -39,6 +39,7 @@ def guide_of_console(self): print('|21 赠指定总数的辣条到房间    |') print('|22 银瓜子全部购买辣条并送到房间 |') + print('|23 购买勋章(使用银瓜子或者硬币)|') print('  ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ') def default(self, line): @@ -151,6 +152,10 @@ def do_21(self, arg): def do_22(self, arg): id, real_roomid = self.parse(arg, '-u:-p:') self.exec_notifier_func_threads(id, BuyLatiaoTask.clean_latiao, [real_roomid]) + + def do_23(self, arg): + id, coin_type, real_roomid = self.parse(arg, '-u:-c:-p:') + self.exec_notifier_func_threads(id, BuyMedalTask.buy_medal, [real_roomid, coin_type]) def fetch_real_roomid(self, room_id): real_roomid = [-1, UtilsTask.get_real_roomid, room_id] diff --git a/reqs/custom.py b/reqs/custom.py index b8ab5de..9ec3810 100644 --- a/reqs/custom.py +++ b/reqs/custom.py @@ -1,6 +1,7 @@ import utils from bili_global import API_LIVE + class BuyLatiaoReq: # 其实与utils部分差不多,怀疑可能是新旧api @staticmethod @@ -8,3 +9,11 @@ async def fetch_livebili_userinfo_pc(user): url = f'{API_LIVE}/xlive/web-ucenter/user/get_user_info' json_rsp = await user.bililive_session.request_json('GET', url, headers=user.dict_bili['pcheaders']) return json_rsp + + +class BuyMedalReq: + @staticmethod + async def buy_medal(user, uid, coin_type): + url = f'https://api.vc.bilibili.com/link_group/v1/member/buy_medal?coin_type={coin_type}&master_uid={uid}&platform=android' + json_rsp = await user.other_session.request_json('GET', url, headers=user.dict_bili['pcheaders']) + return json_rsp diff --git a/tasks/custom.py b/tasks/custom.py index 847e334..27085db 100644 --- a/tasks/custom.py +++ b/tasks/custom.py @@ -1,7 +1,7 @@ # 理论上项目已有的task不要相互引用,只能引用utils的东西,防止交叉,但是自定义的无所谓,因为绝对不可能交叉 from tasks.live_daily_job import SendGiftTask from tasks.utils import UtilsTask -from reqs.custom import BuyLatiaoReq +from reqs.custom import BuyLatiaoReq, BuyMedalReq class SendLatiaoTask: @@ -46,7 +46,6 @@ async def fetch_silver(user): if not json_rsp['code']: silver = json_rsp['data']['silver'] return silver - @staticmethod async def clean_latiao(user, room_id): @@ -55,4 +54,14 @@ async def clean_latiao(user, room_id): coin_type = 'silver' num_sent = int((await BuyLatiaoTask.fetch_silver(user)) / 100) await UtilsTask.buy_gift(user, room_id, num_sent, coin_type, gift_id) + +class BuyMedalTask: + @staticmethod + async def buy_medal(user, room_id, coin_type): + if coin_type == 'metal' or coin_type == 'silver': + uid = await UtilsTask.check_uid_by_roomid(user, room_id) + if uid is not None: + json_rsp = await BuyMedalReq.buy_medal(user, uid, coin_type) + user.info(json_rsp['msg']) + user.info('请重新检查填写coin_type或者房间号') diff --git a/tasks/utils.py b/tasks/utils.py index 3fd3fe1..76a3cd1 100644 --- a/tasks/utils.py +++ b/tasks/utils.py @@ -373,6 +373,15 @@ async def get_real_roomid(user, room_id): elif json_rsp['code'] == 60004: print(json_rsp['msg']) + @staticmethod + async def check_uid_by_roomid(user, room_id): + json_rsp = await user.req_s(UtilsReq.init_room, user, room_id) + if not json_rsp['code']: + uid = json_rsp['data']['uid'] + print(f'房间号{room_id}对应的UID为{uid}') + return int(uid) + return None + @staticmethod async def send_danmu(user, msg, room_id): json_rsp = await user.req_s(UtilsReq.send_danmu, user, msg, room_id)