From 6b00f181f6c41b3315d7249fe63937466bb6c225 Mon Sep 17 00:00:00 2001 From: Profitroll <47523801+profitrollgame@users.noreply.github.com> Date: Sat, 17 Dec 2022 23:25:53 +0100 Subject: [PATCH] Avatars caching implemented --- config_example.json | 4 ++++ modules/scheduled.py | 19 ++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/config_example.json b/config_example.json index e20747b..d228852 100644 --- a/config_example.json +++ b/config_example.json @@ -37,6 +37,10 @@ "sponsorships": { "time": 9, "enabled": true + }, + "cache_avatars": { + "interval": 6, + "enabled": true } }, "locations": { diff --git a/modules/scheduled.py b/modules/scheduled.py index 5b98aca..14c416b 100644 --- a/modules/scheduled.py +++ b/modules/scheduled.py @@ -1,6 +1,8 @@ +from os import path, sep from apscheduler.schedulers.asyncio import AsyncIOScheduler -from datetime import datetime +from datetime import datetime, timedelta from app import app +from pyrogram.enums.chat_members_filter import ChatMembersFilter from modules.utils import configGet, locale, logWrite from dateutil.relativedelta import relativedelta from modules.database import col_applications @@ -19,6 +21,21 @@ scheduler = AsyncIOScheduler() # except AttributeError: # continue +if configGet("enabled", "scheduler", "cache_avatars"): + @scheduler.scheduled_job(trigger="date", run_date=datetime.now()+timedelta(seconds=10)) + @scheduler.scheduled_job(trigger="interval", hours=configGet("interval", "scheduler", "cache_avatars")) + async def cache_avatars(): + list_of_users = [] + async for member in app.get_chat_members(configGet("destination_group"), filter=ChatMembersFilter.SEARCH, query=""): + list_of_users.append(member.user) + for user in list_of_users: + if user.photo != None: + if not path.exists(f'{configGet("cache", "locations")}{sep}avatars{sep}{user.photo.big_file_id}'): + print(f'Pre-cached avatar {user.photo.big_file_id} of {user.id}', flush=True) + await app.download_media(user.photo.big_file_id, file_name=f'{configGet("cache", "locations")}{sep}avatars{sep}{user.photo.big_file_id}') + logWrite("Avatars caching performed") + + if configGet("enabled", "scheduler", "birthdays"): @scheduler.scheduled_job(trigger="cron", hour=configGet("time", "scheduler", "birthdays")) async def check_birthdays():