-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcron.py
More file actions
48 lines (31 loc) · 1.23 KB
/
Copy pathcron.py
File metadata and controls
48 lines (31 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import glob
import os
import telebot
from app.services.users import download_user_avatar, get_users, User
from app.utils.logging import logger
from server import app
STATIC_PATH = f'{app.config["APP_DIR"]}/static'
bot = telebot.TeleBot(app.config['TELEGRAM_BOT_TOKEN'], parse_mode='HTML', threaded=False)
def load_new_avatars(users: list[User]):
for user in users:
try:
download_user_avatar(user)
except:
continue
def clean_old_avatars(users):
photos = [user.photo_id for user in users]
for photo in glob.glob(f'{STATIC_PATH}/pictures/*.jpg'):
if not os.path.basename(photo)[:-4] in photos:
app.logger.debug(f'Delete photo {os.path.basename(photo)}')
os.remove(photo)
def backup_database():
return os.system(f"mysqldump -u {app.config['DB_USER']} -h {app.config['DB_HOST']} --set-gtid-purged=OFF "
f"--no-tablespaces --column-statistics=0 '{app.config['DB_NAME']}' > db-backup.sql")
@logger.catch
def main():
users = get_users()
load_new_avatars(users)
clean_old_avatars(users)
if app.config['DB_USER'] and app.config['DB_PASSWORD'] and app.config['DB_HOST'] and app.config['DB_NAME']:
backup_database()
main()