diff --git a/Silver.py b/Silver.py index 883ece4..200eae9 100644 --- a/Silver.py +++ b/Silver.py @@ -1,9 +1,10 @@ from bilibili import bilibili +import hashlib +import requests import datetime import time import asyncio import utils - class Silver(): # 将time_end时间转换成正常时间 @@ -49,7 +50,6 @@ async def run(self): print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), "检查宝箱状态") temp = self.GetAward() if temp == None or temp == -10017: - #print("# 半小时后检测是否第二天了") await asyncio.sleep(utils.seconds_until_tomorrow() + 300) elif temp == 0: print("# 打开了宝箱") diff --git a/Tasks.py b/Tasks.py index 8b02e16..877fab6 100644 --- a/Tasks.py +++ b/Tasks.py @@ -62,7 +62,7 @@ def link_sign(self): def send_gift(self): if self.dic_user['gift']['on/off'] == '1': try: - argvs = utils.fetch_bag_list() + argvs = utils.fetch_bag_list(printer=False)[0] for i in range(0,len(argvs)): giftID = argvs[i][0] giftNum = argvs[i][1] @@ -72,6 +72,34 @@ def send_gift(self): except: print("# 没有将要过期的礼物~") + def auto_send_gift(self): + if self.dic_user['auto-gift']['on/off'] == "1": + a = utils.fetch_medal(printer=False) + temp_dic = bilibili().gift_list() + temp = utils.fetch_bag_list(printer=False)[1] + roomid = a[0] + today_feed = a[1] + day_limit = a[2] + left_num = int(day_limit) - int(today_feed) + calculate = 0 + for i in range(0,len(temp)): + gift_id = int(temp[i][0]) + gift_num = int(temp[i][1]) + bag_id = int(temp[i][2]) + if (gift_num * (temp_dic[gift_id] / 100) < left_num) and (gift_id != 4 and gift_id != 3): + calculate = calculate + temp_dic[gift_id] / 100 * gift_num + # tmp = calculate / (temp_dic[gift_id] / 100) + tmp2 = temp_dic[gift_id] / 100 * gift_num + utils.send_gift_web(roomid,gift_id,gift_num,bag_id) + left_num = left_num-tmp2 + elif left_num - temp_dic[gift_id] / 100 >= 0 and (gift_id != 4 and gift_id != 3): + tmp = (left_num) / (temp_dic[gift_id] / 100) + tmp1 = (temp_dic[gift_id] / 100) * int(tmp) + calculate = calculate + tmp1 + utils.send_gift_web(roomid, gift_id, tmp, bag_id) + left_num = left_num - tmp1 + print("# 自动送礼共送出亲密度为%s的礼物" % int(calculate)) + def sliver2coin(self): if self.dic_user['coin']['on/off'] == '1': response, response1= bilibili().silver2coin() @@ -87,5 +115,6 @@ async def run(self): self.Daily_Task() self.link_sign() self.send_gift() + self.auto_send_gift() # print('Tasks over.') await asyncio.sleep(21600) diff --git a/bilibili.py b/bilibili.py index 451cb92..023e027 100644 --- a/bilibili.py +++ b/bilibili.py @@ -541,4 +541,12 @@ def assign_group(self, i1, i2): response = requests.get(url, headers=appheaders) return response - + def gift_list(self): + temp_dic = {} + url = "https://api.live.bilibili.com/gift/v2/live/room_gift_list?roomid=2721650&area_v2_id=86" + res = self.bili_section_get(url) + for j in range(0, len(res.json()['data'])): + price = res.json()['data'][j]['price'] + id = res.json()['data'][j]['id'] + temp_dic[id] = price + return temp_dic diff --git a/conf/bilibili.conf b/conf/bilibili.conf index 4f8c610..6932c7f 100644 --- a/conf/bilibili.conf +++ b/conf/bilibili.conf @@ -37,7 +37,7 @@ cookie = [account] username = -password = +password = [dic-giftids_raffle] 117 = 怦然心动 diff --git a/conf/user.conf b/conf/user.conf index 0d207fe..8694ab8 100644 --- a/conf/user.conf +++ b/conf/user.conf @@ -8,7 +8,8 @@ send_to_room = [coin] on/off = 0 - +[auto-gift] +on/off = 0 [print_control] join_lottery = user diff --git a/utils.py b/utils.py index 890b4cd..5144c79 100644 --- a/utils.py +++ b/utils.py @@ -22,17 +22,17 @@ def adjust_for_chinese(str): def CurrentTime(): currenttime = int(time.mktime(datetime.datetime.now().timetuple())) return str(currenttime) - def seconds_until_tomorrow(): - today = datetime.date.today() - tomorrow = today + datetime.timedelta(days=1) - tomorrow_start_time = int(time.mktime(time.strptime(str(tomorrow), '%Y-%m-%d'))) - current_time = int(time.mktime(datetime.datetime.now().timetuple())) - return tomorrow_start_time - current_time - + today = datetime.date.today() + tomorrow = today + datetime.timedelta(days=1) + tomorrow_start_time = int(time.mktime(time.strptime(str(tomorrow), '%Y-%m-%d'))) + current_time = int(time.mktime(datetime.datetime.now().timetuple())) + return tomorrow_start_time - current_time -def fetch_medal(): - print('{} {} {:^12} {:^10} {} {:^6} '.format(adjust_for_chinese('勋章'), adjust_for_chinese('主播昵称'), '亲密度', '今日的亲密度', +def fetch_medal(printer=True): + if printer == True: + print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), '查询勋章信息') + print('{} {} {:^12} {:^10} {} {:^6} '.format(adjust_for_chinese('勋章'), adjust_for_chinese('主播昵称'), '亲密度', '今日的亲密度', adjust_for_chinese('排名'), '勋章状态')) dic_worn = {'1': '正在佩戴', '0': '待机状态'} response = bilibili().request_fetchmedal() @@ -40,13 +40,18 @@ def fetch_medal(): json_response = response.json() if json_response['code'] == 0: for i in json_response['data']['fansMedalList']: - print('{} {} {:^14} {:^14} {} {:^6} '.format(adjust_for_chinese(i['medal_name'] + '|' + str(i['level'])), - adjust_for_chinese(i['anchorInfo']['uname']), - str(i['intimacy']) + '/' + str(i['next_intimacy']), - str(i['todayFeed']) + '/' + str(i['dayLimit']), - adjust_for_chinese(str(i['rank'])), - dic_worn[str(i['status'])])) - + if printer == True: + print('{} {} {:^14} {:^14} {} {:^6} '.format(adjust_for_chinese(i['medal_name'] + '|' + str(i['level'])), + adjust_for_chinese(i['anchorInfo']['uname']), + str(i['intimacy']) + '/' + str(i['next_intimacy']), + str(i['todayFeed']) + '/' + str(i['dayLimit']), + adjust_for_chinese(str(i['rank'])), + dic_worn[str(i['status'])])) + if i['status'] == 1: + roomid = i['roomid'] + today_feed = i['today_feed'] + day_limit = i['day_limit'] + return roomid,today_feed,day_limit def send_danmu_msg_andriod(msg, roomId): response = bilibili().request_send_danmu_msg_andriod(msg, roomId) print(response.json()) @@ -86,11 +91,12 @@ def fetch_user_info(): print(process_bar) print('# 等级榜', user_level_rank) -def fetch_bag_list(verbose=False, bagid=None): +def fetch_bag_list(verbose=False, bagid=None,printer=True): response = bilibili().request_fetch_bag_list() temp = [] - # print(response.json()) - print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), '查询可用礼物') + gift_list = [] + if printer == True: + print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), '查询可用礼物') 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']) @@ -99,17 +105,19 @@ def fetch_bag_list(verbose=False, bagid=None): expireat = (response.json()['data']['list'][i]['expire_at']) left_time = (expireat - int(CurrentTime())) left_days = (expireat - int(CurrentTime())) / 86400 + gift_list.append([gift_id, gift_num, bag_id]) if bagid is not None: if bag_id == int(bagid): return gift_id else: if verbose: print("# 编号为" + str(bag_id) + '的'+ gift_name + 'X' + gift_num, '(在' + str(math.ceil(left_days)) + '天后过期)') - else: + elif printer == True: print("# " + gift_name + 'X' + gift_num, '(在' + str(math.ceil(left_days)) + '天后过期)') + if 0 < int(left_time) < 43200: # 剩余时间少于半天时自动送礼 temp.append([gift_id, gift_num, bag_id]) - return temp + return temp,gift_list def check_taskinfo(): response = bilibili().request_check_taskinfo() @@ -179,7 +187,6 @@ def send_gift_web(roomid, giftid, giftnum, bagid): response = bilibili().request_check_room(roomid) ruid = response.json()['data']['uid'] biz_id = response.json()['data']['room_id'] - print(ruid, biz_id) response1 = bilibili().request_send_gift_web(giftid, giftnum, bagid, ruid, biz_id) json_response1 = response1.json() if json_response1['code'] == 0: @@ -198,4 +205,4 @@ def check_room_true(roomid): param3 = data['encrypted'] return param1, param2, param3 - +