Allow for multi chat/subreddit support
This commit is contained in:
parent
e2dd94ee34
commit
9cadb9541a
|
@ -11,9 +11,15 @@ config:
|
||||||
storage_chat: -1001222674489
|
storage_chat: -1001222674489
|
||||||
storage_message_id: 1367
|
storage_message_id: 1367
|
||||||
send_to_chats:
|
send_to_chats:
|
||||||
|
- goodanimemes:
|
||||||
- goodanimemes
|
- goodanimemes
|
||||||
subreddits:
|
- -1001327025649:
|
||||||
- goodanimemes
|
- ProgrammerAnimemes
|
||||||
|
- memes10k:
|
||||||
|
- linuxmemes
|
||||||
|
- dankmemes
|
||||||
|
- memes
|
||||||
|
- ShitPostCrusaders
|
||||||
bot_admins:
|
bot_admins:
|
||||||
- thekneesocks
|
- thekneesocks
|
||||||
- 214416808
|
- 214416808
|
||||||
|
|
44
redditbot.py
44
redditbot.py
|
@ -31,8 +31,15 @@ reddit_client_secret = config_data['reddit']['client_secret']
|
||||||
|
|
||||||
storage_chat = config_data['config'].get('storage_chat')
|
storage_chat = config_data['config'].get('storage_chat')
|
||||||
storage_msg_id = config_data['config'].get('storage_message_id')
|
storage_msg_id = config_data['config'].get('storage_message_id')
|
||||||
send_to_chats = config_data['config']['send_to_chats']
|
_bkup_subreddits = config_data['config'].get('subreddits')
|
||||||
subreddits = config_data['config']['subreddits']
|
_send_to_chats = config_data['config']['send_to_chats']
|
||||||
|
send_to_chats = dict()
|
||||||
|
for chat in _send_to_chats:
|
||||||
|
subs = None
|
||||||
|
if isinstance(chat, dict):
|
||||||
|
subs = tuple(chat.values())[0]
|
||||||
|
chat = tuple(chat.keys())[0]
|
||||||
|
send_to_chats[chat] = subs
|
||||||
bot_admins = config_data['config']['bot_admins']
|
bot_admins = config_data['config']['bot_admins']
|
||||||
cron_duration = config_data['config']['cron_duration']
|
cron_duration = config_data['config']['cron_duration']
|
||||||
|
|
||||||
|
@ -49,9 +56,12 @@ async def main():
|
||||||
await (await client.get_messages(storage_chat, ids=storage_msg_id)).download_media('redditbot.json')
|
await (await client.get_messages(storage_chat, ids=storage_msg_id)).download_media('redditbot.json')
|
||||||
with open('redditbot.json') as file:
|
with open('redditbot.json') as file:
|
||||||
seen_posts = json.load(file)
|
seen_posts = json.load(file)
|
||||||
|
if isinstance(seen_posts, list):
|
||||||
|
seen_posts = {'version': 0, 'chats': {'global': seen_posts}}
|
||||||
except Exception:
|
except Exception:
|
||||||
traceback.print_exc()
|
logging.exception('Loading JSON')
|
||||||
seen_posts = []
|
seen_posts = {'version': 0, 'chats': {'global': []}}
|
||||||
|
# chat dict: {chatid: [array of submission ids]}
|
||||||
|
|
||||||
async def write_seen_posts():
|
async def write_seen_posts():
|
||||||
with open('redditbot.json', 'w') as file:
|
with open('redditbot.json', 'w') as file:
|
||||||
|
@ -61,6 +71,17 @@ async def main():
|
||||||
|
|
||||||
@aiocron.crontab(cron_duration)
|
@aiocron.crontab(cron_duration)
|
||||||
async def start_post():
|
async def start_post():
|
||||||
|
global_sp = seen_posts['chats']['global']
|
||||||
|
for chat in send_to_chats:
|
||||||
|
uses_bkupsub = False
|
||||||
|
subreddits = send_to_chats[chat]
|
||||||
|
if not subreddits:
|
||||||
|
subreddits = _bkup_subreddits
|
||||||
|
uses_bkupsub = True
|
||||||
|
chat = await client.get_peer_id(chat)
|
||||||
|
if str(chat) not in seen_posts['chats']:
|
||||||
|
seen_posts['chats'][str(chat)] = []
|
||||||
|
chat_sp = global_sp if uses_bkupsub else seen_posts['chats'][str(chat)]
|
||||||
while True:
|
while True:
|
||||||
random.shuffle(subreddits)
|
random.shuffle(subreddits)
|
||||||
to_break = False
|
to_break = False
|
||||||
|
@ -74,33 +95,28 @@ async def main():
|
||||||
cpid = getattr(submission, 'crosspost_parent', None)
|
cpid = getattr(submission, 'crosspost_parent', None)
|
||||||
if cpid:
|
if cpid:
|
||||||
cpid = cpid[3:]
|
cpid = cpid[3:]
|
||||||
if submission.id in seen_posts or cpid in seen_posts:
|
if submission.id in chat_sp + global_sp or cpid in chat_sp + global_sp:
|
||||||
continue
|
continue
|
||||||
random_post = submission
|
random_post = submission
|
||||||
break
|
break
|
||||||
cpid = getattr(random_post, 'crosspost_parent', None)
|
cpid = getattr(random_post, 'crosspost_parent', None)
|
||||||
if cpid:
|
if cpid:
|
||||||
cpid = cpid[3:]
|
cpid = cpid[3:]
|
||||||
if random_post.id in seen_posts or cpid in seen_posts:
|
if random_post.id in chat_sp + global_sp or cpid in chat_sp + global_sp:
|
||||||
continue
|
continue
|
||||||
seen_posts.append(cpid or random_post.id)
|
chat_sp.append(cpid or random_post.id)
|
||||||
print(random_post.id, random_post.shortlink)
|
print(random_post.id, random_post.shortlink)
|
||||||
to_break = True
|
to_break = True
|
||||||
break
|
break
|
||||||
if to_break:
|
if to_break:
|
||||||
break
|
break
|
||||||
to_break = False
|
|
||||||
for _ in range(5):
|
|
||||||
try:
|
try:
|
||||||
await _actual_start_post(random_post, send_to_chats)
|
await _actual_start_post(random_post, [chat])
|
||||||
except Exception:
|
except Exception:
|
||||||
traceback.print_exc()
|
logging.exception(random_post.id)
|
||||||
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'{random_post.id}\n{traceback.format_exc()}', parse_mode=None)
|
||||||
else:
|
else:
|
||||||
to_break = True
|
|
||||||
break
|
|
||||||
if to_break:
|
|
||||||
break
|
break
|
||||||
await write_seen_posts()
|
await write_seen_posts()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue