최종_final 개선
This commit is contained in:
@ -85,10 +85,10 @@ class DaminationAPI():
|
||||
if widgets[0]:
|
||||
for widget in widgets:
|
||||
await self.add_widget_and_start_fetching_notifications(widget)
|
||||
logger.info(f"[initialize_widgets] WIDGET ID 초기화 : {widgets}")
|
||||
logger.info(f"WIDGET ID 초기화 : {widgets} [initialize_widgets]")
|
||||
|
||||
else:
|
||||
logger.error(f"[initialize_widgets] WIDGET 초기화 실패 - 등록된 WIDGET ID 없음")
|
||||
logger.error(f"WIDGET 초기화 실패 - 등록된 WIDGET ID 없음 [initialize_widgets]")
|
||||
|
||||
|
||||
async def process_notification(self):
|
||||
@ -97,17 +97,17 @@ class DaminationAPI():
|
||||
try:
|
||||
for widget_id, value in donate.items():
|
||||
|
||||
logger.debug(f"[process_notification] Raw Data : widget_id: {widget_id}, value: {value}")
|
||||
logger.debug(f"Raw Data : widget_id: {widget_id}, value: {value} [process_notification]")
|
||||
await self.send_notifications_to_subscribers(widget_id, value)
|
||||
|
||||
logger.debug(f"[process_notification] QUEUE 추출: {widget_id, value}")
|
||||
logger.debug(f"QUEUE 추출: {widget_id, value} [process_notification]")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"[process_notification] Raw Data : {donate}, {e}")
|
||||
logger.error(f"Raw Data : {donate}, {e} [process_notification]")
|
||||
|
||||
|
||||
else:
|
||||
logger.error("[process_notification] QUEUE 추출 실패 - 저장된 QUEUE 없음")
|
||||
logger.error("QUEUE 추출 실패 - 저장된 QUEUE 없음 [process_notification]")
|
||||
|
||||
|
||||
def get_subscribers(self, widget_id: str) -> list:
|
||||
@ -118,11 +118,11 @@ class DaminationAPI():
|
||||
|
||||
if widget_id in self.subscribers:
|
||||
subscribers = self.subscribers[widget_id]
|
||||
logger.info(f"[get_subscribers] 구독자 반환({widget_id}) : {subscribers}")
|
||||
logger.info(f"구독자 반환({widget_id}) : {subscribers} [get_subscribers]")
|
||||
return subscribers
|
||||
|
||||
else:
|
||||
logger.error(f"[get_subscribers] 구독자 없음({widget_id})")
|
||||
logger.error(f"구독자 없음({widget_id}) [get_subscribers]")
|
||||
return []
|
||||
|
||||
|
||||
@ -132,22 +132,22 @@ class DaminationAPI():
|
||||
|
||||
if url not in self.subscribers[widget_id]:
|
||||
self.subscribers[widget_id].append(url)
|
||||
logger.info(f"[add_subscriber] 구독자 추가 widget_id={widget_id}, url={url}")
|
||||
logger.info(f"구독자 추가 widget_id={widget_id}, url={url} [add_subscriber]")
|
||||
return widget_id
|
||||
|
||||
else:
|
||||
logger.error(f"[add_subscriber] 이미 추가된 구독자 widget_id={widget_id}, url={url}")
|
||||
logger.error(f"이미 추가된 구독자 widget_id={widget_id}, url={url} [add_subscriber]")
|
||||
return ''
|
||||
|
||||
|
||||
def remove_subscriber(self, widget_id: str, url: str) -> str:
|
||||
if widget_id in self.subscribers and url in self.subscribers[widget_id]:
|
||||
self.subscribers[widget_id].remove(url)
|
||||
logger.info(f"[remove_subscriber] 구독자 제거: widget_id={widget_id}, url={url}")
|
||||
logger.info(f"구독자 제거: widget_id={widget_id}, url={url} [remove_subscriber]")
|
||||
return widget_id
|
||||
|
||||
else:
|
||||
logger.error(f"[remove_subscriber] 구독자를 찾을 수 없음 widget_id={widget_id}, url={url}")
|
||||
logger.error(f"구독자를 찾을 수 없음 widget_id={widget_id}, url={url} [remove_subscriber]")
|
||||
return ''
|
||||
|
||||
|
||||
@ -157,11 +157,11 @@ class DaminationAPI():
|
||||
"""
|
||||
|
||||
if self.WIDGET_IDS:
|
||||
logger.info(f"[get_widget_ids] WIDGET ID 반환 {self.WIDGET_IDS}")
|
||||
logger.info(f"WIDGET ID 반환 {self.WIDGET_IDS} [get_widget_ids]")
|
||||
return self.WIDGET_IDS
|
||||
|
||||
else:
|
||||
logger.error(f"[get_widget_ids] WIDGET ID 반환 실패 - WIDGET ID 없음")
|
||||
logger.error(f"WIDGET ID 반환 실패 - WIDGET ID 없음 [get_widget_ids]")
|
||||
return []
|
||||
|
||||
|
||||
@ -174,11 +174,11 @@ class DaminationAPI():
|
||||
try:
|
||||
if widget_id not in self.WIDGET_IDS:
|
||||
self.WIDGET_IDS.append(widget_id)
|
||||
logger.info(f"[add_widget_id] WIDGET ID 추가 {widget_id}")
|
||||
logger.info(f"WIDGET ID 추가 {widget_id} [add_widget_id]")
|
||||
return widget_id
|
||||
|
||||
else:
|
||||
logger.error(f"[add_widget_id] 이미 추가된 WIDGET ID {widget_id}")
|
||||
logger.error(f"이미 추가된 WIDGET ID {widget_id} [add_widget_id]")
|
||||
return ''
|
||||
|
||||
except Exception as e:
|
||||
@ -195,11 +195,11 @@ class DaminationAPI():
|
||||
try:
|
||||
if widget_id in self.WIDGET_IDS:
|
||||
widget = self.WIDGET_IDS.pop(self.WIDGET_IDS.index(widget_id))
|
||||
logger.info(f"[remove_widget_id] WIDGET ID 제거 {widget}")
|
||||
logger.info(f"WIDGET ID 제거 {widget} [remove_widget_id]")
|
||||
return widget
|
||||
|
||||
else:
|
||||
logger.error(f"[remove_widget_id] WIDGET ID 제거 실패 - WIDGET ID를 찾을 수 없음")
|
||||
logger.error(f"WIDGET ID 제거 실패 - WIDGET ID를 찾을 수 없음 [remove_widget_id]")
|
||||
return ''
|
||||
|
||||
except Exception as e:
|
||||
@ -224,7 +224,7 @@ class DaminationAPI():
|
||||
async with connect(wss_url, ping_interval=12) as websocket:
|
||||
token = json.loads(await websocket.recv())['token']
|
||||
logger.debug(f"token : {token}")
|
||||
logger.info(f"[fetch_notifications] 웹 소켓 연결 : {widget_id}")
|
||||
logger.info(f"웹 소켓 연결 : {widget_id} [fetch_notifications]")
|
||||
while True:
|
||||
|
||||
message = await websocket.recv()
|
||||
@ -237,23 +237,23 @@ class DaminationAPI():
|
||||
reformed_data = Donate(**alert_data)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"[fetch_notifications] Error: {e}, Raw data: {alert_data}")
|
||||
logger.error(f"Error: {e}, Raw data: {alert_data} [fetch_notifications]")
|
||||
pass
|
||||
|
||||
await queue.put({widget_id: json.loads(reformed_data.json().encode('utf-8').decode('unicode_escape'))})
|
||||
|
||||
except asyncio.CancelledError:
|
||||
logger.info(f"[fetch_notifications] 웹 소켓 연결 취소 : {widget_id}")
|
||||
logger.info(f"웹 소켓 연결 취소 : {widget_id} [fetch_notifications]")
|
||||
break
|
||||
|
||||
except Exception as e:
|
||||
retries += 1
|
||||
logger.error(f"[fetch_notifications] 웹 소켓 연결 실패 : {e}, 재시도 : {retries}")
|
||||
logger.error(f"웹 소켓 연결 실패({widget_id}) : {e}, 재시도 : {retries} [fetch_notifications]")
|
||||
await asyncio.sleep(2 ** retries)
|
||||
|
||||
if retries > 2:
|
||||
self.remove_widget_id(widget_id)
|
||||
logger.error(f"[fetch_notifications] 연결 실패 : {widget_id}")
|
||||
logger.error(f"연결 실패 : {widget_id} [fetch_notifications]")
|
||||
|
||||
|
||||
@staticmethod
|
||||
@ -283,11 +283,11 @@ class DaminationAPI():
|
||||
self.add_widget_id(widget_id)
|
||||
task = asyncio.create_task(self.fetch_notifications(self.queue, widget_id))
|
||||
self.fetch_tasks.append(task)
|
||||
logger.info(f"[add_widget_and_start_fetching_notifications] 투네이션 알림 수신 시작 : {widget_id}")
|
||||
logger.info(f"투네이션 알림 수신 시작 : {widget_id} [add_widget_and_start_fetching_notifications]")
|
||||
return widget_id
|
||||
|
||||
else:
|
||||
logger.error(f"[add_widget_and_start_fetching_notifications] 이미 투네이션 알림 수신 중 : {widget_id}")
|
||||
logger.error(f"이미 투네이션 알림 수신 중 : {widget_id} [add_widget_and_start_fetching_notifications]")
|
||||
return ''
|
||||
|
||||
|
||||
@ -303,11 +303,11 @@ class DaminationAPI():
|
||||
fetch_task = self.fetch_tasks.pop(index)
|
||||
fetch_task.cancel()
|
||||
await fetch_task
|
||||
logger.info(f"[remove_widget_and_stop_fetching_notifications] 투네이션 알림 수신 중단 : {widget_id}")
|
||||
logger.info(f"투네이션 알림 수신 중단 : {widget_id} [remove_widget_and_stop_fetching_notifications]")
|
||||
return widget_id
|
||||
|
||||
else:
|
||||
logger.error(f"[remove_widget_and_stop_fetching_notifications] 수신중인 투네이션 알림을 찾을 수 없음 {widget_id}")
|
||||
logger.error(f"수신중인 투네이션 알림을 찾을 수 없음 {widget_id} [remove_widget_and_stop_fetching_notifications]")
|
||||
return ''
|
||||
|
||||
|
||||
@ -346,16 +346,16 @@ class DaminationAPI():
|
||||
|
||||
async def send_notification_webhook(self, session, subscriber_url, donate):
|
||||
payload = {
|
||||
"text": f"**{donate['content']['name']}({donate['content']['account']}) to {donate['content']['streamer']}**\n{donate}",
|
||||
"text": f"**{donate['content']['name']}**({donate['content']['account']}) to **{donate['content']['streamer']}**\n{donate}",
|
||||
"data": donate
|
||||
}
|
||||
|
||||
async with session.post(subscriber_url, json=payload) as response:
|
||||
if response.status == 200:
|
||||
logger.info(f"[send_notification_webhook] 알림 전송 {subscriber_url}, {payload}")
|
||||
logger.info(f"알림 전송 {subscriber_url}, {payload} [send_notification_webhook]")
|
||||
|
||||
else:
|
||||
logger.error(f"[send_notification_webhook] 알림 전송 실패 {subscriber_url}, 응답: {response.status}")
|
||||
logger.error(f"알림 전송 실패 {subscriber_url}, 응답: {response.status} [send_notification_webhook]")
|
||||
|
||||
|
||||
async def run(self):
|
||||
|
||||
Reference in New Issue
Block a user