Skip to content

Commit

Permalink
Fix BulletScreen args parse error
Browse files Browse the repository at this point in the history
  • Loading branch information
albus12138 committed Aug 17, 2018
1 parent 39fa484 commit 4d0b7f5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 27 deletions.
8 changes: 4 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ def in_time(time_list):

def run():
while (today == datetime.datetime.now().day) and keep_running:
client.task()
client.gift()
client.group()
client.silver_to_coin()
if in_time(schedule):
client.bullet_screen()
else:
client.bullet_screen_client.quit()
client.task()
client.gift()
client.group()
client.silver_to_coin()
time.sleep(60)
client.quit()

Expand Down
39 changes: 16 additions & 23 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def __init__(self, config_file):
self.logger,
self.raffle_keyword,
self.raffle_callback,
True
False
)
self.thread_pool = ThreadPool(5, 10)
self.is_sign = False
Expand Down Expand Up @@ -372,7 +372,6 @@ def silver_to_coin(self):
payload = self._build_payload()
res = self._session.get(self.urls["silver2coin_app"], params=payload)
data = res.json()
self.logger.info(res.content.decode("u8"))
if data.get("code") != 0:
self.logger.error("[兑换] 移动端银瓜子兑换硬币失败, 原因: {}".format(data.get("message")))
else:
Expand All @@ -382,7 +381,6 @@ def silver_to_coin(self):
payload = self._build_payload()
res = self._session.get(self.urls["silver2coin_web"], params=payload)
data = res.json()
self.logger.info(res.content.decode("u8"))
if data.get("code") != 0:
self.logger.error("[兑换] 网页端银瓜子兑换硬币失败, 原因: {}".format(data.get("message")))
else:
Expand Down Expand Up @@ -480,7 +478,8 @@ def query_raffle(self):
self.query_queue.remove(record)

def bullet_screen(self):
if not self.bullet_screen_client.main.isAlive():
if self.bullet_screen_client.stop:
print(self.bullet_screen_client.main.isAlive())
self.bullet_screen_client.main.start()

def check_user_info(self):
Expand Down Expand Up @@ -620,35 +619,28 @@ def silver(self):
return True

def quit(self):
self.is_silver = True
self.is_watch = 2
if self.bullet_screen_client.stop == 0:
self.bullet_screen_client.quit()
self.thread_pool.stop()


class BilibiliBulletScreen:
class BilibiliBulletScreen(websocket.WebSocketApp):
def __init__(self, host, origin, room_id, logger, keyword, raffle_callback, silent=True):
super().__init__(host)
self.host = host
self.origin = origin
self.room_id = int(room_id)
self.logger = logger
self.keywords = keyword
self.raffle_callback = raffle_callback
self.silent = silent
self._ws = websocket.WebSocketApp(host,
on_message=self.on_message,
on_error=self.on_error,
on_open=self.on_open,
on_close=self.on_close)

self.status = 0
self.hot = 0
self.stop = 1
self.daemon = threading.Thread(target=self.heart, args=())
self.main = threading.Thread(target=self.run_forever, args=())

def run_forever(self):
self.stop = 0
self._ws.run_forever(host=self.host, origin=self.origin)
self.main = threading.Thread(target=self.run_forever, args=(None, None, 0, None, None, None, None, None, False, self.host, self.origin, None, False))

@staticmethod
def pack_msg(payload, opt):
Expand Down Expand Up @@ -690,10 +682,11 @@ def heart(self):

while not self.stop:
data = self.pack_msg("", 0x2)
self._ws.send(data)
self.send(data)
time.sleep(30)

def on_open(self, ws):
def on_open(self):
self.stop = 0
raw_payload = {
'uid': 0,
'roomid': self.room_id,
Expand All @@ -702,21 +695,21 @@ def on_open(self, ws):
'clientver': '1.4.1'
}
data = self.pack_msg(json.dumps(raw_payload), 0x7)
self._ws.send(data)
self.send(data)
self.daemon.setDaemon(True)
self.daemon.start()

def on_message(self, ws, msg):
def on_message(self, msg):
while len(msg) != 0:
pkt_length = int.from_bytes(msg[:4], byteorder='big')
pkt = struct.unpack(">IHHII{}s{}s".format(pkt_length-16, len(msg)-pkt_length), msg)
self.process_msg(pkt[:-1])
msg = pkt[-1]

def on_error(self, ws, err):
def on_error(self, err):
self.logger.error("[弹幕姬] 错误原因: {}".format(err))

def on_close(self, ws):
def on_close(self):
self.logger.info("[弹幕姬] WebSocket 连接已关闭")
self.stop = 1
self.daemon.join()
Expand All @@ -725,7 +718,7 @@ def on_close(self, ws):
def quit(self):
if self.stop == 0:
self.stop = 1
self._ws.close()
self.close()
self.main.join()
return True

Expand Down

0 comments on commit 4d0b7f5

Please sign in to comment.