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:
|
||||||
subreddits:
|
- goodanimemes
|
||||||
- goodanimemes
|
- -1001327025649:
|
||||||
|
- ProgrammerAnimemes
|
||||||
|
- memes10k:
|
||||||
|
- linuxmemes
|
||||||
|
- dankmemes
|
||||||
|
- memes
|
||||||
|
- ShitPostCrusaders
|
||||||
bot_admins:
|
bot_admins:
|
||||||
- thekneesocks
|
- thekneesocks
|
||||||
- 214416808
|
- 214416808
|
||||||
|
|
94
redditbot.py
94
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,47 +71,53 @@ async def main():
|
||||||
|
|
||||||
@aiocron.crontab(cron_duration)
|
@aiocron.crontab(cron_duration)
|
||||||
async def start_post():
|
async def start_post():
|
||||||
while True:
|
global_sp = seen_posts['chats']['global']
|
||||||
random.shuffle(subreddits)
|
for chat in send_to_chats:
|
||||||
to_break = False
|
uses_bkupsub = False
|
||||||
for subreddit_name in subreddits:
|
subreddits = send_to_chats[chat]
|
||||||
subreddit = reddit.subreddit(subreddit_name)
|
if not subreddits:
|
||||||
while True:
|
subreddits = _bkup_subreddits
|
||||||
random_post = subreddit.random()
|
uses_bkupsub = True
|
||||||
cpid = None
|
chat = await client.get_peer_id(chat)
|
||||||
if random_post is None:
|
if str(chat) not in seen_posts['chats']:
|
||||||
for submission in subreddit.hot():
|
seen_posts['chats'][str(chat)] = []
|
||||||
cpid = getattr(submission, 'crosspost_parent', None)
|
chat_sp = global_sp if uses_bkupsub else seen_posts['chats'][str(chat)]
|
||||||
if cpid:
|
while True:
|
||||||
cpid = cpid[3:]
|
random.shuffle(subreddits)
|
||||||
if submission.id in seen_posts or cpid in seen_posts:
|
to_break = False
|
||||||
continue
|
for subreddit_name in subreddits:
|
||||||
random_post = submission
|
subreddit = reddit.subreddit(subreddit_name)
|
||||||
break
|
while True:
|
||||||
cpid = getattr(random_post, 'crosspost_parent', None)
|
random_post = subreddit.random()
|
||||||
if cpid:
|
cpid = None
|
||||||
cpid = cpid[3:]
|
if random_post is None:
|
||||||
if random_post.id in seen_posts or cpid in seen_posts:
|
for submission in subreddit.hot():
|
||||||
continue
|
cpid = getattr(submission, 'crosspost_parent', None)
|
||||||
seen_posts.append(cpid or random_post.id)
|
if cpid:
|
||||||
print(random_post.id, random_post.shortlink)
|
cpid = cpid[3:]
|
||||||
to_break = True
|
if submission.id in chat_sp + global_sp or cpid in chat_sp + global_sp:
|
||||||
break
|
continue
|
||||||
if to_break:
|
random_post = submission
|
||||||
break
|
break
|
||||||
to_break = False
|
cpid = getattr(random_post, 'crosspost_parent', None)
|
||||||
for _ in range(5):
|
if cpid:
|
||||||
|
cpid = cpid[3:]
|
||||||
|
if random_post.id in chat_sp + global_sp or cpid in chat_sp + global_sp:
|
||||||
|
continue
|
||||||
|
chat_sp.append(cpid or random_post.id)
|
||||||
|
print(random_post.id, random_post.shortlink)
|
||||||
|
to_break = True
|
||||||
|
break
|
||||||
|
if to_break:
|
||||||
|
break
|
||||||
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
|
break
|
||||||
if to_break:
|
|
||||||
break
|
|
||||||
await write_seen_posts()
|
await write_seen_posts()
|
||||||
|
|
||||||
async def _start_broadcast(text, file, chats):
|
async def _start_broadcast(text, file, chats):
|
||||||
|
|
Loading…
Reference in New Issue