|
|
|
@ -31,8 +31,15 @@ reddit_client_secret = config_data['reddit']['client_secret'] |
|
|
|
|
|
|
|
|
|
storage_chat = config_data['config'].get('storage_chat') |
|
|
|
|
storage_msg_id = config_data['config'].get('storage_message_id') |
|
|
|
|
send_to_chats = config_data['config']['send_to_chats'] |
|
|
|
|
subreddits = config_data['config']['subreddits'] |
|
|
|
|
_bkup_subreddits = config_data['config'].get('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'] |
|
|
|
|
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') |
|
|
|
|
with open('redditbot.json') as file: |
|
|
|
|
seen_posts = json.load(file) |
|
|
|
|
if isinstance(seen_posts, list): |
|
|
|
|
seen_posts = {'version': 0, 'chats': {'global': seen_posts}} |
|
|
|
|
except Exception: |
|
|
|
|
traceback.print_exc() |
|
|
|
|
seen_posts = [] |
|
|
|
|
logging.exception('Loading JSON') |
|
|
|
|
seen_posts = {'version': 0, 'chats': {'global': []}} |
|
|
|
|
# chat dict: {chatid: [array of submission ids]} |
|
|
|
|
|
|
|
|
|
async def write_seen_posts(): |
|
|
|
|
with open('redditbot.json', 'w') as file: |
|
|
|
@ -61,47 +71,53 @@ async def main(): |
|
|
|
|
|
|
|
|
|
@aiocron.crontab(cron_duration) |
|
|
|
|
async def start_post(): |
|
|
|
|
while True: |
|
|
|
|
random.shuffle(subreddits) |
|
|
|
|
to_break = False |
|
|
|
|
for subreddit_name in subreddits: |
|
|
|
|
subreddit = reddit.subreddit(subreddit_name) |
|
|
|
|
while True: |
|
|
|
|
random_post = subreddit.random() |
|
|
|
|
cpid = None |
|
|
|
|
if random_post is None: |
|
|
|
|
for submission in subreddit.hot(): |
|
|
|
|
cpid = getattr(submission, 'crosspost_parent', None) |
|
|
|
|
if cpid: |
|
|
|
|
cpid = cpid[3:] |
|
|
|
|
if submission.id in seen_posts or cpid in seen_posts: |
|
|
|
|
continue |
|
|
|
|
random_post = submission |
|
|
|
|
break |
|
|
|
|
cpid = getattr(random_post, 'crosspost_parent', None) |
|
|
|
|
if cpid: |
|
|
|
|
cpid = cpid[3:] |
|
|
|
|
if random_post.id in seen_posts or cpid in seen_posts: |
|
|
|
|
continue |
|
|
|
|
seen_posts.append(cpid or random_post.id) |
|
|
|
|
print(random_post.id, random_post.shortlink) |
|
|
|
|
to_break = True |
|
|
|
|
break |
|
|
|
|
if to_break: |
|
|
|
|
break |
|
|
|
|
to_break = False |
|
|
|
|
for _ in range(5): |
|
|
|
|
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: |
|
|
|
|
random.shuffle(subreddits) |
|
|
|
|
to_break = False |
|
|
|
|
for subreddit_name in subreddits: |
|
|
|
|
subreddit = reddit.subreddit(subreddit_name) |
|
|
|
|
while True: |
|
|
|
|
random_post = subreddit.random() |
|
|
|
|
cpid = None |
|
|
|
|
if random_post is None: |
|
|
|
|
for submission in subreddit.hot(): |
|
|
|
|
cpid = getattr(submission, 'crosspost_parent', None) |
|
|
|
|
if cpid: |
|
|
|
|
cpid = cpid[3:] |
|
|
|
|
if submission.id in chat_sp + global_sp or cpid in chat_sp + global_sp: |
|
|
|
|
continue |
|
|
|
|
random_post = submission |
|
|
|
|
break |
|
|
|
|
cpid = getattr(random_post, 'crosspost_parent', None) |
|
|
|
|
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: |
|
|
|
|
await _actual_start_post(random_post, send_to_chats) |
|
|
|
|
await _actual_start_post(random_post, [chat]) |
|
|
|
|
except Exception: |
|
|
|
|
traceback.print_exc() |
|
|
|
|
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: |
|
|
|
|
to_break = True |
|
|
|
|
break |
|
|
|
|
if to_break: |
|
|
|
|
break |
|
|
|
|
await write_seen_posts() |
|
|
|
|
|
|
|
|
|
async def _start_broadcast(text, file, chats): |
|
|
|
|