Skip to content

Commit

Permalink
Merge pull request #8 from hizkifw/dev
Browse files Browse the repository at this point in the history
fix(ws): catch timeout
  • Loading branch information
hizkifw authored Jan 12, 2022
2 parents 433540f + e1bd359 commit 99e970b
Show file tree
Hide file tree
Showing 3 changed files with 464 additions and 412 deletions.
40 changes: 20 additions & 20 deletions autofc2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import json

Logger.print_inline = False
logger = Logger('autofc2')
logger = Logger("autofc2")

last_valid_config = None


def get_config():
global last_valid_config
try:
with open('autofc2.json', 'r') as f:
with open("autofc2.json", "r") as f:
last_valid_config = json.load(f)
except Exception as ex:
if last_valid_config is None:
Expand All @@ -20,19 +22,23 @@ def get_config():
logger.warn("Warning: unable to load config, using last valid one")
return last_valid_config


def clone(obj):
return json.loads(json.dumps(obj))


def get_channels():
config = get_config()
return config['channels'].keys()
return config["channels"].keys()


def get_channel_params(channel_id):
config = get_config()
params = clone(config['default_params'])
params.update(clone(config['channels'][channel_id]))
params = clone(config["default_params"])
params.update(clone(config["channels"][channel_id]))
return params


def reload_channels_list(tasks):
async def noop():
pass
Expand All @@ -46,26 +52,26 @@ async def noop():
if channel_id not in channels:
tasks[channel_id].cancel()


async def handle_channel(channel_id):
params = get_channel_params(channel_id)
async with FC2LiveDL(params) as fc2:
await fc2.download(channel_id)


async def main():
logger.info('starting')
logger.info("starting")

tasks = {}
sleep_task = None
try:
while True:
reload_channels_list(tasks)
sleep_task = asyncio.create_task(asyncio.sleep(1))
task_arr = [ sleep_task ]
task_arr = [sleep_task]
for channel in tasks.keys():
if tasks[channel].done():
tasks[channel] = asyncio.create_task(
handle_channel(channel)
)
tasks[channel] = asyncio.create_task(handle_channel(channel))
task_arr.append(tasks[channel])

await asyncio.wait(task_arr, return_when=asyncio.FIRST_COMPLETED)
Expand All @@ -77,15 +83,9 @@ async def main():
for task in tasks.values():
task.cancel()

if __name__ == '__main__':
# Set up asyncio loop
loop = asyncio.get_event_loop()
task = asyncio.ensure_future(main())

if __name__ == "__main__":
try:
loop.run_until_complete(task)
asyncio.run(main())
except KeyboardInterrupt:
task.cancel()
finally:
# Give some time for aiohttp cleanup
loop.run_until_complete(asyncio.sleep(0.250))
loop.close()
pass
Loading

0 comments on commit 99e970b

Please sign in to comment.