Add timeout

This commit is contained in:
blank X 2020-09-16 21:21:50 +07:00
parent 98b4a7a1b3
commit b89c7cb32d
2 changed files with 68 additions and 53 deletions

View File

@ -20,6 +20,7 @@ config:
allow_spoilers: true allow_spoilers: true
show_nsfw_warning: true show_nsfw_warning: true
show_spoilers_warning: true show_spoilers_warning: true
timeout: 60
-1001284025455: -1001284025455:
subreddits: subreddits:
- HentaiMemes - HentaiMemes
@ -29,6 +30,7 @@ config:
allow_spoilers: true allow_spoilers: true
show_nsfw_warning: false show_nsfw_warning: false
show_spoilers_warning: true show_spoilers_warning: true
timeout: 60
memes10k: memes10k:
subreddits: subreddits:
- linuxmemes - linuxmemes
@ -41,6 +43,7 @@ config:
allow_spoilers: true allow_spoilers: true
show_nsfw_warning: true show_nsfw_warning: true
show_spoilers_warning: true show_spoilers_warning: true
timeout: 60
bot_admins: bot_admins:
- thekneesocks - thekneesocks
- 214416808 - 214416808

View File

@ -44,7 +44,7 @@ if isinstance(_send_to_chats, list):
j = {'subreddits': j, 'cron_duration': config_data['config']['cron_duration'], j = {'subreddits': j, 'cron_duration': config_data['config']['cron_duration'],
'allow_selfposts': True, 'allow_nsfw': True, 'allow_selfposts': True, 'allow_nsfw': True,
'allow_spoilers': True, 'show_nsfw_warning': True, 'allow_spoilers': True, 'show_nsfw_warning': True,
'show_spoilers_warning': True} 'show_spoilers_warning': True, 'timeout': None}
send_to_chats[i] = j send_to_chats[i] = j
bot_admins = config_data['config']['bot_admins'] bot_admins = config_data['config']['bot_admins']
@ -93,55 +93,26 @@ async def main():
allow_spoilers = chat_data['allow_spoilers'] allow_spoilers = chat_data['allow_spoilers']
show_nsfw_warning = chat_data['show_nsfw_warning'] show_nsfw_warning = chat_data['show_nsfw_warning']
show_spoilers_warning = chat_data['show_spoilers_warning'] show_spoilers_warning = chat_data['show_spoilers_warning']
timeout = chat_data.get('timeout')
@aiocron.crontab(cron_duration) def _get_submission_blocc():
async def start_post():
while True: while True:
random.shuffle(subreddits) subreddit = reddit.subreddit(random.choice(subreddits))
def _get_submission_blocc(): random_post = subreddit.random()
while True: cpid = cpp = None
subreddit = reddit.subreddit(random.choice(subreddits)) if random_post is None:
random_post = subreddit.random() for submission in subreddit.hot():
cpid = cpp = None cpid = getattr(submission, 'crosspost_parent', None)
if random_post is None:
for submission in subreddit.hot():
cpid = getattr(submission, 'crosspost_parent', None)
if cpid and getattr(random_post, 'crosspost_parent_list', None):
cpid = cpid[3:]
if submission.id in chat_sp + global_sp or cpid in chat_sp + global_sp:
continue
if not (allow_selfposts and allow_nsfw and allow_spoilers):
is_self = submission.is_self
nsfw = submission.over_18
spoilers = submission.spoiler
if cpid:
cpp = reddit.submission(cpid)
if not allow_selfposts:
is_self = cpp.is_self
if not (nsfw and allow_nsfw):
nsfw = cpp.over_18
if not (spoilers and allow_spoilers):
nsfw = cpp.spoiler
if is_self and not allow_selfposts:
continue
if nsfw and not allow_nsfw:
continue
if spoilers and not allow_spoilers:
continue
random_post = submission
break
cpid = getattr(random_post, 'crosspost_parent', None)
if cpid and getattr(random_post, 'crosspost_parent_list', None): if cpid and getattr(random_post, 'crosspost_parent_list', None):
cpid = cpid[3:] cpid = cpid[3:]
if random_post.id in chat_sp + global_sp or cpid in chat_sp + global_sp: if submission.id in chat_sp + global_sp or cpid in chat_sp + global_sp:
continue continue
if not (allow_selfposts and allow_nsfw and allow_spoilers): if not (allow_selfposts and allow_nsfw and allow_spoilers):
is_self = random_post.is_self is_self = submission.is_self
nsfw = random_post.over_18 nsfw = submission.over_18
spoilers = random_post.spoiler spoilers = submission.spoiler
if cpid and not cpp:
cpp = reddit.submission(cpid)
if cpid: if cpid:
cpp = reddit.submission(cpid)
if not allow_selfposts: if not allow_selfposts:
is_self = cpp.is_self is_self = cpp.is_self
if not (nsfw and allow_nsfw): if not (nsfw and allow_nsfw):
@ -154,18 +125,59 @@ async def main():
continue continue
if spoilers and not allow_spoilers: if spoilers and not allow_spoilers:
continue continue
chat_sp.append(cpid or random_post.id) random_post = submission
print(random_post.id, random_post.shortlink) break
return random_post, cpp cpid = getattr(random_post, 'crosspost_parent', None)
random_post, cpp = await client.loop.run_in_executor(None, _get_submission_blocc) if cpid and getattr(random_post, 'crosspost_parent_list', None):
cpid = cpid[3:]
if random_post.id in chat_sp + global_sp or cpid in chat_sp + global_sp:
continue
if not (allow_selfposts and allow_nsfw and allow_spoilers):
is_self = random_post.is_self
nsfw = random_post.over_18
spoilers = random_post.spoiler
if cpid and not cpp:
cpp = reddit.submission(cpid)
if cpid:
if not allow_selfposts:
is_self = cpp.is_self
if not (nsfw and allow_nsfw):
nsfw = cpp.over_18
if not (spoilers and allow_spoilers):
nsfw = cpp.spoiler
if is_self and not allow_selfposts:
continue
if nsfw and not allow_nsfw:
continue
if spoilers and not allow_spoilers:
continue
chat_sp.append(cpid or random_post.id)
print(random_post.id, random_post.shortlink)
return random_post, cpp
@aiocron.crontab(cron_duration)
async def start_post():
while True:
try: try:
await _actual_start_post(random_post, [chat], cpp, show_nsfw_warning, show_spoilers_warning) random_post, cpp = await asyncio.wait_for(client.loop.run_in_executor(None, _get_submission_blocc), timeout)
except Exception: except asyncio.TimeoutError:
logging.exception(random_post.id) logging.error('%s timed out', chat)
for i in bot_admins: for i in bot_admins:
await client.send_message(i, f'{random_post.id}\n{traceback.format_exc()}', parse_mode=None) await client.send_message(i, f'{chat} timed out')
else:
break break
except Exception:
logging.exception(chat)
for i in bot_admins:
await client.send_message(i, f'{chat}\n{traceback.format_exc()}')
else:
try:
await _actual_start_post(random_post, [chat], cpp, show_nsfw_warning, show_spoilers_warning)
except Exception:
logging.exception(random_post.id)
for i in bot_admins:
await client.send_message(i, f'{random_post.id}\n{traceback.format_exc()}', parse_mode=None)
else:
break
await write_seen_posts() await write_seen_posts()
_added_chats.append(start_post) _added_chats.append(start_post)
@ -189,7 +201,7 @@ async def main():
async def _download_file(filename, url): async def _download_file(filename, url):
print(url) print(url)
async with session.get(url, timeout=None) as resp: async with session.get(url) as resp:
with open(filename, 'wb') as file: with open(filename, 'wb') as file:
while True: while True:
chunk = await resp.content.read(10) chunk = await resp.content.read(10)