Skip to content

Commit

Permalink
Handle exceptions in FC2LiveStream.wait_for_online (#47)
Browse files Browse the repository at this point in the history
* Make FC2LiveStream.get_meta raise on status code >= 400

Currently response with, for example, 503 error will cause json decoding error, making debugging harder.

* Add try-except and exponential backoff in wait_for_online

---------

Co-authored-by: user <user@localhost>
  • Loading branch information
15532th and user authored Sep 16, 2023
1 parent 4d746c0 commit a6c973f
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions fc2_live_dl/fc2.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,32 @@ def __str__(self):


class FC2LiveStream:

MAX_LIVE_CHECK_INTERVAL = 300

def __init__(self, session, channel_id):
self._meta = None
self._session = session
self._logger = Logger("live")
self.channel_id = channel_id

async def wait_for_online(self, interval):
while not await self.is_online():
for _ in range(interval):
current_interval = interval
while True:
try:
if await self.is_online():
break
except Exception as e:
description = f'{e.__class__.__name__}: {e}'
self._logger.warn(f'Error when checking if stream is live: {description}')
current_interval = min(current_interval * 2, self.MAX_LIVE_CHECK_INTERVAL)
self._logger.debug(f'Next check in {current_interval} seconds')
else:
if current_interval != interval:
self._logger.debug(f'Successfully fetched live status, restoring check interval of {interval} seconds')
current_interval = interval

for _ in range(current_interval):
self._logger.info("Waiting for stream", inline=True, spin=True)
await asyncio.sleep(1)

Expand Down Expand Up @@ -271,6 +288,7 @@ async def get_meta(self, *, refetch=False):
}
self._logger.trace("get_meta>", url, data)
async with self._session.post(url, data=data) as resp:
resp.raise_for_status()
# FC2 returns text/javascript instead of application/json
# Content type is specified so aiohttp knows what to expect
data = await resp.json(content_type="text/javascript")
Expand Down

0 comments on commit a6c973f

Please sign in to comment.