1616import codecs
1717reload (sys )
1818
19+
1920def CurrentTime ():
2021 currenttime = int (time .mktime (datetime .datetime .now ().timetuple ()))
2122 return str (currenttime )
22- # 13:30 ---> 13.5
23- def eventtime ():
24- now = datetime .datetime .now ()
25- return now .hour + now .minute / 60.0
23+
24+
25+
2626
2727
2828
@@ -32,20 +32,6 @@ def calculate_sign(str):
3232 sign = hash .hexdigest ()
3333 return sign
3434
35- def adjust_for_chinese (str ):
36- SPACE = '\N{IDEOGRAPHIC SPACE} '
37- EXCLA = '\N{FULLWIDTH EXCLAMATION MARK} '
38- TILDE = '\N{FULLWIDTH TILDE} '
39-
40- # strings of ASCII and full-width characters (same order)
41- west = '' .join (chr (i ) for i in range (ord (' ' ),ord ('~' )))
42- east = SPACE + '' .join (chr (i ) for i in range (ord (EXCLA ),ord (TILDE )))
43-
44- # build the translation table
45- full = str .maketrans (west ,east )
46- str = str .translate (full ).rstrip ().split ('\n ' )
47- md = '{:^10}' .format (str [0 ])
48- return md .translate (full )
4935
5036
5137class bilibili ():
@@ -57,24 +43,9 @@ def __new__(cls, *args, **kw):
5743 fileDir = os .path .dirname (os .path .realpath ('__file__' ))
5844 cls .instance .file_bilibili = fileDir + "/conf/bilibili.conf"
5945 cls .instance .dic_bilibili = configloader .load_bilibili (cls .instance .file_bilibili )
60- cls .instance .activity_raffleid_list = []
61- cls .instance .activity_roomid_list = []
62- # cls.instance.activity_time_list = []
63- cls .instance .TV_raffleid_list = []
64- cls .instance .TV_roomid_list = []
65- # cls.instance.TV_time_list = []
66- # cls.instance.TVsleeptime = 185
67- # cls.instance.activitysleeptime = 125
68- cls .instance .joined_event = []
69- cls .instance .joined_TV = []
70-
7146 return cls .instance
7247
73- def getlist (self ):
74- # print(self.joined_event)
75- # print(self.joined_TV)
76- print ('本次参与活动抽奖次数:' , len (self .joined_event ))
77- print ('本次参与电视抽奖次数:' , len (self .joined_TV ))
48+
7849
7950 def post_watching_history (self , room_id ):
8051 data = {
@@ -110,23 +81,10 @@ def silver2coin(self):
11081 response1 = requests .post (app_url , headers = self .dic_bilibili ['appheaders' ])
11182 print ("#" , response1 .json ()['msg' ])
11283
113- def get_bag_list (self ):
84+ def request_fetch_bag_list (self ):
11485 url = "http://api.live.bilibili.com/gift/v2/gift/bag_list"
11586 response = requests .get (url , headers = self .dic_bilibili ['pcheaders' ])
116- temp = []
117- print (time .strftime ('%Y-%m-%d %H:%M:%S' , time .localtime (time .time ())), '查询可用礼物' )
118- for i in range (len (response .json ()['data' ]['list' ])):
119- bag_id = (response .json ()['data' ]['list' ][i ]['bag_id' ])
120- gift_id = (response .json ()['data' ]['list' ][i ]['gift_id' ])
121- gift_num = str ((response .json ()['data' ]['list' ][i ]['gift_num' ])).center (4 )
122- gift_name = response .json ()['data' ]['list' ][i ]['gift_name' ]
123- expireat = (response .json ()['data' ]['list' ][i ]['expire_at' ])
124- left_time = (expireat - int (CurrentTime ()))
125- left_days = (expireat - int (CurrentTime ())) / 86400
126- print ("# " + gift_name + 'X' + gift_num , '(在' + str (math .ceil (left_days )) + '天后过期)' )
127- if 0 < int (left_time ) < 43200 : # 剩余时间少于半天时自动送礼
128- temp .append ([gift_id , gift_num , bag_id ])
129- return temp
87+ return response
13088
13189 def get_uid_in_room (self , roomID ):
13290 url = "https://api.live.bilibili.com/room/v1/Room/room_init?id=" + roomID
@@ -158,37 +116,13 @@ def send_bag_gift_web(self, roomID, giftID, giftNum, bagID):
158116 except :
159117 print ("# 清理快到期礼物成功,但请联系开发者修bug!" )
160118
161- def user_info (self ):
119+ def request_fetch_user_info (self ):
162120 url = "https://api.live.bilibili.com/i/api/liveinfo"
163121 response = requests .get (url , headers = self .dic_bilibili ['pcheaders' ])
164- print (time .strftime ('%Y-%m-%d %H:%M:%S' , time .localtime (time .time ())), '查询用户信息' )
165- if (response .json ()['code' ] == 0 ):
166- uname = response .json ()['data' ]['userInfo' ]['uname' ]
167- achieve = response .json ()['data' ]['achieves' ]
168- user_level = response .json ()['data' ]['userCoinIfo' ]['user_level' ]
169- silver = response .json ()['data' ]['userCoinIfo' ]['silver' ]
170- gold = response .json ()['data' ]['userCoinIfo' ]['gold' ]
171- user_next_level = response .json ()['data' ]['userCoinIfo' ]['user_next_level' ]
172- user_intimacy = response .json ()['data' ]['userCoinIfo' ]['user_intimacy' ]
173- user_next_intimacy = response .json ()['data' ]['userCoinIfo' ]['user_next_intimacy' ]
174- user_level_rank = response .json ()['data' ]['userCoinIfo' ]['user_level_rank' ]
175- billCoin = response .json ()['data' ]['userCoinIfo' ]['coins' ]
176- print ('# 用户名' , uname )
177- print ('# 银瓜子' , silver )
178- print ('# 金瓜子' , gold )
179- print ('# 硬币数' , billCoin )
180- print ('# 成就值' , achieve )
181- print ('# 等级值' , user_level , '———>' , user_next_level )
182- print ('# 经验值' , user_intimacy )
183- print ('# 剩余值' , user_next_intimacy - user_intimacy )
184- arrow = int (user_intimacy * 30 / user_next_intimacy )
185- line = 30 - arrow
186- percent = user_intimacy / user_next_intimacy * 100.0
187- process_bar = '[' + '>' * arrow + '-' * line + ']' + '%.2f' % percent + '%'
188- print (process_bar )
189- print ('# 等级榜' , user_level_rank )
190-
191- def send_danmu_msg_andriod (self , msg , roomId ):
122+ return response
123+
124+
125+ def request_send_danmu_msg_andriod (self , msg , roomId ):
192126 url = 'https://api.live.bilibili.com/api/sendmsg?'
193127 # page ??
194128 time = CurrentTime ()
@@ -240,9 +174,9 @@ def send_danmu_msg_andriod(self, msg, roomId):
240174 }
241175
242176 response = requests .post (url , headers = self .dic_bilibili ['appheaders' ], data = data )
243- print ( response . json ())
177+ return response
244178
245- def send_danmu_msg_web (self , msg , roomId ):
179+ def request_send_danmu_msg_web (self , msg , roomId ):
246180 url = 'https://api.live.bilibili.com/msg/send'
247181 data = {
248182 'color' : '16777215' ,
@@ -255,19 +189,13 @@ def send_danmu_msg_web(self, msg, roomId):
255189 }
256190
257191 response = requests .post (url , headers = self .dic_bilibili ['pcheaders' ], data = data )
258- print ( response . json ())
192+ return response
259193
260194
261- def fetchmedal (self ):
262- print ('{} {} {:^12} {:^10} {} {:^6} ' .format (adjust_for_chinese ('勋章' ), adjust_for_chinese ('主播昵称' ), '亲密度' , '今日的亲密度' , adjust_for_chinese ('排名' ), '勋章状态' ))
263- dic_worn = {'1' : '正在佩戴' , '0' :'待机状态' }
195+ def request_fetchmedal (self ):
264196 url = 'https://api.live.bilibili.com/i/api/medal?page=1&pageSize=50'
265197 response = requests .post (url , headers = self .dic_bilibili ['pcheaders' ])
266- # print(response.json())
267- json_response = response .json ()
268- if json_response ['code' ] == 0 :
269- for i in json_response ['data' ]['fansMedalList' ]:
270- 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' ])]))
198+ return response
271199
272200
273201 def GetHash (self ):
@@ -509,32 +437,7 @@ def get_giftlist_of_captain(self, roomid):
509437 response1 = requests .get (true_url )
510438 return response1
511439
512- def append_to_activitylist (self , raffleid , text1 , time = '' ):
513- self .activity_raffleid_list .append (raffleid )
514- self .activity_roomid_list .append (text1 )
515- # self.activity_time_list.append(int(time))
516- # self.activity_time_list.append(int(CurrentTime()))
517- self .joined_event .append (eventtime ())
518- # print("activity加入成功", self.joined_event)
519-
520-
521- def append_to_TVlist (self , raffleid , real_roomid , time = '' ):
522- self .TV_raffleid_list .append (raffleid )
523- self .TV_roomid_list .append (real_roomid )
524- # self.TV_time_list.append(int(time)+int(CurrentTime()))
525- # self.TV_time_list.append(int(CurrentTime()))
526- self .joined_TV .append (eventtime ())
527- # print("tv加入成功", self.joined_TV)
528440
529- def check_TVlist (self , raffleid ):
530- if raffleid not in self .TV_raffleid_list :
531- return True
532- return False
533-
534- def check_activitylist (self , raffleid ):
535- if raffleid not in self .activity_raffleid_list :
536- return True
537- return False
538441
539442 def get_giftids_raffle (self , str ):
540443 return self .dic_bilibili ['giftids_raffle' ][str ]
@@ -570,72 +473,6 @@ def get_TV_result(self, TV_roomid, TV_raffleid):
570473 response = requests .get (url , headers = headers )
571474 return response
572475
573- def delete_0st_activitylist (self ):
574- del self .activity_roomid_list [0 ]
575- del self .activity_raffleid_list [0 ]
576- # del self.activity_time_list[0]
577-
578- def delete_0st_TVlist (self ):
579- del self .TV_roomid_list [0 ]
580- del self .TV_raffleid_list [0 ]
581- # del self.TV_time_list[0]
582-
583- def clean_activity (self ):
584- # print(self.activity_raffleid_list)
585- if self .activity_raffleid_list :
586- for i in range (0 , len (self .activity_roomid_list )):
587- response = self .get_activity_result (self .activity_roomid_list [0 ], self .activity_raffleid_list [0 ])
588- json_response = response .json ()
589- # print(json_response)
590- if json_response ['code' ] == 0 :
591- data = json_response ['data' ]
592- print ("# 房间" , str (self .activity_roomid_list [0 ]).center (9 ), "网页端活动抽奖结果:" ,
593- data ['gift_name' ] + "x" + str (data ['gift_num' ]))
594-
595- self .delete_0st_activitylist ()
596-
597- elif json_response ['code' ] == - 400 :
598- # sleepseconds = self.activitysleeptime + self.activity_time_list[0] - int(CurrentTime())+ 2
599- # sleepseconds = self.activity_time_list[0] - int(CurrentTime())
600- # return sleepsecondsq
601- return
602-
603- else :
604- print ('未知情况' )
605- print (json_response )
606-
607- else :
608- return
609-
610- def clean_TV (self ):
611- # print(self.TV_raffleid_list)
612- if self .TV_raffleid_list :
613- for i in range (0 , len (self .TV_roomid_list )):
614-
615- response = self .get_TV_result (self .TV_roomid_list [0 ], self .TV_raffleid_list [0 ])
616- # if response.json()['data']['gift_name'] != "":
617- json_response = response .json ()
618- # print(json_response)
619- if json_response ['data' ]['gift_id' ] == '-1' :
620- return
621- elif json_response ['data' ]['gift_id' ] != '-1' :
622-
623- data = json_response ['data' ]
624- print ("# 房间" , str (self .TV_roomid_list [0 ]).center (9 ), "小电视道具抽奖结果:" ,
625- data ['gift_name' ] + "x" + str (data ['gift_num' ]))
626-
627- self .delete_0st_TVlist ()
628- # else:
629- # print(int(CurrentTime()))
630- # sleepseconds = self.TV_time_list[0] - int(CurrentTime()) + 1
631- # sleepseconds = self.TV_time_list[0] - int(CurrentTime())
632- # return
633-
634- # else:
635- # print('未知')
636- else :
637- return
638-
639476 def pcpost_heartbeat (self ):
640477 url = 'http://api.live.bilibili.com/User/userOnlineHeart'
641478 response = requests .post (url , headers = self .dic_bilibili ['pcheaders' ])
0 commit comments