2017-09-30 11:12:01 +03:00
|
|
|
import logging
|
2017-10-14 12:37:47 +03:00
|
|
|
import pickle
|
2017-09-07 19:49:08 +03:00
|
|
|
from collections import deque
|
2017-11-30 23:09:34 +03:00
|
|
|
from queue import Queue, Empty
|
2017-09-07 21:17:40 +03:00
|
|
|
from datetime import datetime
|
2017-11-30 23:09:34 +03:00
|
|
|
from threading import RLock, Thread
|
2017-09-07 21:17:40 +03:00
|
|
|
|
|
|
|
from .tl import types as tl
|
2017-09-07 19:49:08 +03:00
|
|
|
|
2017-12-20 14:47:10 +03:00
|
|
|
__log__ = logging.getLogger(__name__)
|
|
|
|
|
2017-09-07 19:49:08 +03:00
|
|
|
|
|
|
|
class UpdateState:
|
|
|
|
"""Used to hold the current state of processed updates.
|
2017-09-08 13:54:38 +03:00
|
|
|
To retrieve an update, .poll() should be called.
|
2017-09-07 19:49:08 +03:00
|
|
|
"""
|
2017-09-30 12:21:07 +03:00
|
|
|
WORKER_POLL_TIMEOUT = 5.0 # Avoid waiting forever on the workers
|
|
|
|
|
2017-09-30 12:17:31 +03:00
|
|
|
def __init__(self, workers=None):
|
|
|
|
"""
|
|
|
|
:param workers: This integer parameter has three possible cases:
|
|
|
|
workers is None: Updates will *not* be stored on self.
|
|
|
|
workers = 0: Another thread is responsible for calling self.poll()
|
|
|
|
workers > 0: 'workers' background threads will be spawned, any
|
|
|
|
any of them will invoke all the self.handlers.
|
|
|
|
"""
|
|
|
|
self._workers = workers
|
2017-09-30 11:12:01 +03:00
|
|
|
self._worker_threads = []
|
|
|
|
|
2017-09-07 19:58:54 +03:00
|
|
|
self.handlers = []
|
2017-09-07 21:17:40 +03:00
|
|
|
self._updates_lock = RLock()
|
2017-11-30 23:09:34 +03:00
|
|
|
self._updates = Queue()
|
2017-10-14 12:37:47 +03:00
|
|
|
self._latest_updates = deque(maxlen=10)
|
2017-09-07 19:49:08 +03:00
|
|
|
|
2017-09-07 21:17:40 +03:00
|
|
|
# https://core.telegram.org/api/updates
|
|
|
|
self._state = tl.updates.State(0, 0, datetime.now(), 0, 0)
|
2017-09-30 11:12:01 +03:00
|
|
|
|
2017-09-08 13:54:38 +03:00
|
|
|
def can_poll(self):
|
|
|
|
"""Returns True if a call to .poll() won't lock"""
|
2017-11-30 23:09:34 +03:00
|
|
|
return not self._updates.empty()
|
2017-09-07 19:49:08 +03:00
|
|
|
|
2017-09-30 12:21:07 +03:00
|
|
|
def poll(self, timeout=None):
|
|
|
|
"""Polls an update or blocks until an update object is available.
|
|
|
|
If 'timeout is not None', it should be a floating point value,
|
|
|
|
and the method will 'return None' if waiting times out.
|
|
|
|
"""
|
2017-11-30 23:09:34 +03:00
|
|
|
try:
|
|
|
|
update = self._updates.get(timeout=timeout)
|
|
|
|
except Empty:
|
2017-09-30 12:21:07 +03:00
|
|
|
return
|
|
|
|
|
2017-09-18 11:59:54 +03:00
|
|
|
if isinstance(update, Exception):
|
2017-09-30 19:39:31 +03:00
|
|
|
raise update # Some error was set through (surely StopIteration)
|
2017-09-18 11:59:54 +03:00
|
|
|
|
|
|
|
return update
|
2017-09-07 19:49:08 +03:00
|
|
|
|
2017-09-30 11:12:01 +03:00
|
|
|
def get_workers(self):
|
|
|
|
return self._workers
|
|
|
|
|
|
|
|
def set_workers(self, n):
|
2017-09-30 12:17:31 +03:00
|
|
|
"""Changes the number of workers running.
|
|
|
|
If 'n is None', clears all pending updates from memory.
|
|
|
|
"""
|
2017-10-01 20:56:24 +03:00
|
|
|
self.stop_workers()
|
2017-09-30 11:12:01 +03:00
|
|
|
self._workers = n
|
2017-09-30 12:17:31 +03:00
|
|
|
if n is None:
|
2017-11-30 23:09:34 +03:00
|
|
|
while self._updates:
|
|
|
|
self._updates.get()
|
2017-09-30 12:17:31 +03:00
|
|
|
else:
|
2017-10-01 20:56:24 +03:00
|
|
|
self.setup_workers()
|
2017-09-30 11:12:01 +03:00
|
|
|
|
|
|
|
workers = property(fget=get_workers, fset=set_workers)
|
|
|
|
|
2017-10-01 20:56:24 +03:00
|
|
|
def stop_workers(self):
|
2017-09-30 11:12:01 +03:00
|
|
|
"""Raises "StopIterationException" on the worker threads to stop them,
|
|
|
|
and also clears all of them off the list
|
|
|
|
"""
|
2017-09-30 19:44:37 +03:00
|
|
|
if self._workers:
|
|
|
|
with self._updates_lock:
|
|
|
|
# Insert at the beginning so the very next poll causes an error
|
|
|
|
# on all the worker threads
|
|
|
|
# TODO Should this reset the pts and such?
|
|
|
|
for _ in range(self._workers):
|
2017-11-30 23:09:34 +03:00
|
|
|
self._updates.put(StopIteration())
|
2017-09-30 19:39:31 +03:00
|
|
|
|
2017-09-30 11:12:01 +03:00
|
|
|
for t in self._worker_threads:
|
|
|
|
t.join()
|
|
|
|
|
|
|
|
self._worker_threads.clear()
|
|
|
|
|
2017-10-01 20:56:24 +03:00
|
|
|
def setup_workers(self):
|
2017-09-30 12:17:31 +03:00
|
|
|
if self._worker_threads or not self._workers:
|
|
|
|
# There already are workers, or workers is None or 0. Do nothing.
|
2017-09-30 11:12:01 +03:00
|
|
|
return
|
|
|
|
|
|
|
|
for i in range(self._workers):
|
|
|
|
thread = Thread(
|
|
|
|
target=UpdateState._worker_loop,
|
|
|
|
name='UpdateWorker{}'.format(i),
|
|
|
|
daemon=True,
|
|
|
|
args=(self, i)
|
|
|
|
)
|
|
|
|
self._worker_threads.append(thread)
|
|
|
|
thread.start()
|
|
|
|
|
|
|
|
def _worker_loop(self, wid):
|
|
|
|
while True:
|
|
|
|
try:
|
2017-09-30 12:21:07 +03:00
|
|
|
update = self.poll(timeout=UpdateState.WORKER_POLL_TIMEOUT)
|
2017-09-30 11:12:01 +03:00
|
|
|
# TODO Maybe people can add different handlers per update type
|
2017-09-30 12:21:07 +03:00
|
|
|
if update:
|
|
|
|
for handler in self.handlers:
|
|
|
|
handler(update)
|
2017-09-30 11:12:01 +03:00
|
|
|
except StopIteration:
|
|
|
|
break
|
2017-10-25 14:04:12 +03:00
|
|
|
except:
|
2017-09-30 11:12:01 +03:00
|
|
|
# We don't want to crash a worker thread due to any reason
|
2017-12-20 14:47:10 +03:00
|
|
|
__log__.exception('Unhandled exception on worker %d', wid)
|
2017-09-30 11:12:01 +03:00
|
|
|
|
2017-09-07 19:49:08 +03:00
|
|
|
def process(self, update):
|
|
|
|
"""Processes an update object. This method is normally called by
|
|
|
|
the library itself.
|
|
|
|
"""
|
2017-09-30 12:17:31 +03:00
|
|
|
if self._workers is None:
|
|
|
|
return # No processing needs to be done if nobody's working
|
2017-09-07 21:17:40 +03:00
|
|
|
|
|
|
|
with self._updates_lock:
|
|
|
|
if isinstance(update, tl.updates.State):
|
2017-12-20 14:47:10 +03:00
|
|
|
__log__.debug('Saved new updates state')
|
2017-09-07 21:17:40 +03:00
|
|
|
self._state = update
|
2017-09-19 14:17:40 +03:00
|
|
|
return # Nothing else to be done
|
2017-09-07 19:58:54 +03:00
|
|
|
|
2017-09-19 14:17:40 +03:00
|
|
|
pts = getattr(update, 'pts', self._state.pts)
|
2017-09-23 00:12:36 +03:00
|
|
|
if hasattr(update, 'pts') and pts <= self._state.pts:
|
2017-12-20 14:47:10 +03:00
|
|
|
__log__.info('Ignoring %s, already have it', update)
|
2017-09-19 14:17:40 +03:00
|
|
|
return # We already handled this update
|
|
|
|
|
|
|
|
self._state.pts = pts
|
2017-10-01 17:30:27 +03:00
|
|
|
|
2017-10-14 12:37:47 +03:00
|
|
|
# TODO There must be a better way to handle updates rather than
|
|
|
|
# keeping a queue with the latest updates only, and handling
|
|
|
|
# the 'pts' correctly should be enough. However some updates
|
|
|
|
# like UpdateUserStatus (even inside UpdateShort) will be called
|
|
|
|
# repeatedly very often if invoking anything inside an update
|
|
|
|
# handler. TODO Figure out why.
|
|
|
|
"""
|
|
|
|
client = TelegramClient('anon', api_id, api_hash, update_workers=1)
|
|
|
|
client.connect()
|
|
|
|
def handle(u):
|
|
|
|
client.get_me()
|
|
|
|
client.add_update_handler(handle)
|
|
|
|
input('Enter to exit.')
|
|
|
|
"""
|
|
|
|
data = pickle.dumps(update.to_dict())
|
|
|
|
if data in self._latest_updates:
|
2017-12-20 14:47:10 +03:00
|
|
|
__log__.info('Ignoring %s, already have it', update)
|
2017-10-14 12:37:47 +03:00
|
|
|
return # Duplicated too
|
|
|
|
|
|
|
|
self._latest_updates.append(data)
|
|
|
|
|
2017-11-30 22:40:35 +03:00
|
|
|
if isinstance(update, tl.UpdateShort):
|
2017-11-30 23:09:34 +03:00
|
|
|
self._updates.put(update.update)
|
2017-11-30 22:40:35 +03:00
|
|
|
# Expand "Updates" into "Update", and pass these to callbacks.
|
|
|
|
# Since .users and .chats have already been processed, we
|
|
|
|
# don't need to care about those either.
|
|
|
|
elif isinstance(update, (tl.Updates, tl.UpdatesCombined)):
|
2017-11-30 23:09:34 +03:00
|
|
|
for u in update.updates:
|
|
|
|
self._updates.put(u)
|
2017-11-30 23:10:02 +03:00
|
|
|
# TODO Handle "tl.UpdatesTooLong"
|
2017-10-01 17:30:27 +03:00
|
|
|
else:
|
2017-11-30 23:09:34 +03:00
|
|
|
self._updates.put(update)
|