mirror of
https://github.com/gentoo-root/telegram-tracker
synced 2025-08-21 05:43:49 -07:00
User online status polling
This commit is contained in:
commit
5ab815960c
4 changed files with 50 additions and 0 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
/settings/keys.py
|
||||||
|
/tracker.session
|
||||||
|
__pycache__/
|
6
settings/__init__.py
Normal file
6
settings/__init__.py
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
try:
|
||||||
|
from .keys import API_ID, API_HASH
|
||||||
|
except ImportError:
|
||||||
|
print('Put API_ID and API_HASH into settings/keys.py')
|
||||||
|
print()
|
||||||
|
raise IOError('Missing API_ID and API_HASH in settings/keys.py')
|
0
track/__init__.py
Normal file
0
track/__init__.py
Normal file
41
track/__main__.py
Normal file
41
track/__main__.py
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
from datetime import datetime
|
||||||
|
from settings import API_ID, API_HASH
|
||||||
|
from sys import argv, exit
|
||||||
|
from telethon import TelegramClient
|
||||||
|
from telethon.tl.types import UserStatusOnline, UserStatusOffline
|
||||||
|
from time import mktime, sleep
|
||||||
|
|
||||||
|
|
||||||
|
DATETIME_FORMAT = '%Y-%m-%d @ %H:%M:%S'
|
||||||
|
|
||||||
|
|
||||||
|
def utc2localtime(utc):
|
||||||
|
pivot = mktime(utc.timetuple())
|
||||||
|
offset = datetime.fromtimestamp(pivot) - datetime.utcfromtimestamp(pivot)
|
||||||
|
return utc + offset
|
||||||
|
|
||||||
|
|
||||||
|
if len(argv) < 2:
|
||||||
|
print(f'usage: {argv[0]} <contact id>')
|
||||||
|
exit(1)
|
||||||
|
contact_id = argv[1]
|
||||||
|
|
||||||
|
client = TelegramClient('tracker', API_ID, API_HASH)
|
||||||
|
client.start()
|
||||||
|
|
||||||
|
online = None
|
||||||
|
last_offline = None
|
||||||
|
while True:
|
||||||
|
contact = client.get_entity(contact_id)
|
||||||
|
if isinstance(contact.status, UserStatusOffline):
|
||||||
|
if online != False:
|
||||||
|
online = False
|
||||||
|
print(f'User went offline: {utc2localtime(contact.status.was_online).strftime(DATETIME_FORMAT)}')
|
||||||
|
elif last_offline != contact.status.was_online:
|
||||||
|
print(f'User went online and back offline: {utc2localtime(contact.status.was_online).strftime(DATETIME_FORMAT)}')
|
||||||
|
last_offline = contact.status.was_online
|
||||||
|
else:
|
||||||
|
if online != True:
|
||||||
|
online = True
|
||||||
|
print(f'User went online: {datetime.now().strftime(DATETIME_FORMAT)}')
|
||||||
|
sleep(15)
|
Loading…
Add table
Add a link
Reference in a new issue