├── .gitignore ├── config ├── config.py └── __init__.py ├── utils ├── __init__.py └── utils.py ├── README.md ├── requirements.txt └── main.py /.gitignore: -------------------------------------------------------------------------------- 1 | venv/ 2 | .vscode/ 3 | *__pycache__/ 4 | *.session -------------------------------------------------------------------------------- /config/config.py: -------------------------------------------------------------------------------- 1 | api_id = "YOUR_API_ID" 2 | api_hash = "YOUR_API_HASH" 3 | -------------------------------------------------------------------------------- /config/__init__.py: -------------------------------------------------------------------------------- 1 | from .config import api_id, api_hash 2 | 3 | __all__ = ['api_id', 'api_hash'] 4 | -------------------------------------------------------------------------------- /utils/__init__.py: -------------------------------------------------------------------------------- 1 | from .utils import time_has_changed, generate_time_image_bytes 2 | 3 | __all__ = ['time_has_changed', 'generate_time_image_bytes'] 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Установка: 2 | 1. Убедитесь, что используете питон версии 3.6+, чтобы успешно запустился код. Узнать можно посредством команды **python -V**. 3 | 2. Скачайте исходный код. В терминале пропишите **git clone https://github.com/mumtozvalijonov/telegram-avatar-time.git** или просто скачайте [zip](https://github.com/mumtozvalijonov/telegram-avatar-time/archive/master.zip). 4 | 5 | Для успешного запуска необходимо: 6 | 1. В файле config/config.py изменить реквизиты api на свои. 7 | 2. Находясь в той же папке, что и requirements.txt ввести в терминале pip install -r requirements.txt. 8 | 3. Находясь в той же папке, что и main.py ввести в терминале python main.py. 9 | -------------------------------------------------------------------------------- /utils/utils.py: -------------------------------------------------------------------------------- 1 | from datetime import datetime 2 | import numpy as np 3 | import cv2 4 | 5 | 6 | def convert_time_to_string(dt): 7 | return f"{dt.hour}:{dt.minute:02}" 8 | 9 | 10 | def time_has_changed(prev_time): 11 | return convert_time_to_string(datetime.now()) != convert_time_to_string(prev_time) 12 | 13 | 14 | def get_black_background(): 15 | return np.zeros((500, 500)) 16 | 17 | 18 | def generate_time_image_bytes(dt): 19 | text = convert_time_to_string(dt) 20 | image = get_black_background() 21 | font = cv2.FONT_HERSHEY_SIMPLEX 22 | cv2.putText(image, text, (int(image.shape[0]*0.35), int(image.shape[1]*0.5)), font, 1.5, (255, 255, 0), 2, cv2.LINE_AA) 23 | _, bts = cv2.imencode('.jpg', image) 24 | return bts.tobytes() 25 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | appdirs==1.4.3 asn1crypto==0.24.0 astroid==2.3.3 certifi==2018.1.18 chardet==3.0.4 colorama==0.4.3 cryptography==2.1.4 distlib==0.3.0 distro-info===0.18ubuntu0.18.04.1 filelock==3.0.12 httplib2==0.9.2 idna==2.6 importlib-metadata==1.6.0 importlib-resources==1.5.0 isort==4.3.21 keyring==10.6.0 keyrings.alt==3.0 lazy-object-proxy==1.4.3 mccabe==0.6.1 netifaces==0.10.4 numpy==1.18.1 opencv-python==4.2.0.32 pyaes==1.6.1 pyasn1==0.4.8 pycrypto==2.6.1 pycurl==7.43.0.1 pygobject==3.26.1 pylint==2.4.4 pyOpenSSL==17.5.0 python-apt==1.6.5+ubuntu0.2 python-debian==0.1.32 pyxdg==0.25 PyYAML==3.12 requests==2.18.4 requests-unixsocket==0.1.5 rsa==4.0 SecretStorage==2.3.1 six==1.14.0 ssh-import-id==5.7 systemd-python==234 Telethon==1.13.0 typed-ast==1.4.1 unattended-upgrades==0.1 urllib3==1.22 virtualenv==20.0.20 wrapt==1.11.2 zipp==3.1.0 -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import time 2 | from telethon import TelegramClient 3 | from telethon.tl.functions.photos import UploadProfilePhotoRequest, DeletePhotosRequest 4 | from config import api_hash, api_id 5 | from utils import time_has_changed, generate_time_image_bytes 6 | from datetime import datetime, timedelta 7 | import argparse 8 | import pytz 9 | 10 | 11 | def valid_tz(s): 12 | try: 13 | return pytz.timezone(s) 14 | except: 15 | msg = "Not a valid tz: '{0}'.".format(s) 16 | raise argparse.ArgumentTypeError(msg) 17 | 18 | 19 | parser = argparse.ArgumentParser() 20 | parser.add_argument("--api_id", required=False, help="user api ID", type=str, default=api_id) 21 | parser.add_argument("--api_hash", required=False, help="user api Hash", type=str, default=api_hash) 22 | parser.add_argument("--tz", required=False, help="user api Hash", type=valid_tz, default=valid_tz('Asia/Tashkent')) 23 | 24 | args = parser.parse_args() 25 | 26 | client = TelegramClient("carpediem", args.api_id, args.api_hash) 27 | client.start() 28 | 29 | 30 | async def main(): 31 | prev_update_time = datetime.now() - timedelta(minutes=1) 32 | 33 | while True: 34 | if time_has_changed(prev_update_time): 35 | bts = generate_time_image_bytes(datetime.now(args.tz).replace(tzinfo=None)) 36 | await client(DeletePhotosRequest(await client.get_profile_photos('me'))) 37 | file = await client.upload_file(bts) 38 | await client(UploadProfilePhotoRequest(file)) 39 | prev_update_time = datetime.now() 40 | time.sleep(1) 41 | 42 | 43 | if __name__ == '__main__': 44 | import asyncio 45 | asyncio.get_event_loop().run_until_complete(main()) 46 | --------------------------------------------------------------------------------