kopia lustrzana https://github.com/mkdryden/telegram-stats-bot
stats: Add underscores to internal StatsRunner methods
rodzic
bf2966d457
commit
dbcc41ffac
|
@ -92,8 +92,8 @@ def update_usernames_wrapper(context: CallbackContext):
|
|||
|
||||
|
||||
def update_usernames(context: CallbackContext): # context.job.context contains the chat_id
|
||||
user_ids = stats.get_message_user_ids()
|
||||
db_users = stats.get_db_users()
|
||||
user_ids = stats._get_message_user_ids()
|
||||
db_users = stats._get_db_users()
|
||||
tg_users = {user_id: None for user_id in user_ids}
|
||||
to_update = {}
|
||||
for u_id in tg_users:
|
||||
|
@ -109,9 +109,9 @@ def update_usernames(context: CallbackContext): # context.job.context contains
|
|||
to_update[u_id] = tg_users[u_id]
|
||||
except BadRequest: # Handle users no longer in chat or haven't messaged since bot joined
|
||||
logger.debug("Couldn't get user %s", u_id) # debug level because will spam every hour
|
||||
stats.update_user_ids(to_update)
|
||||
stats._update_user_ids(to_update)
|
||||
if stats.users_lock.acquire(timeout=10):
|
||||
stats.users = stats.get_db_users()
|
||||
stats.users = stats._get_db_users()
|
||||
stats.users_lock.release()
|
||||
else:
|
||||
logger.warning("Couldn't acquire username lock.")
|
||||
|
|
|
@ -79,15 +79,17 @@ class StatsRunner(object):
|
|||
self.engine = engine
|
||||
self.tz = tz
|
||||
|
||||
self.users: Dict[int, Tuple[str, str]] = self.get_db_users()
|
||||
self.users: Dict[int, Tuple[str, str]] = self._get_db_users()
|
||||
self.users_lock = Lock()
|
||||
|
||||
def get_message_user_ids(self) -> List[int]:
|
||||
def _get_message_user_ids(self) -> List[int]:
|
||||
"""Returns list of unique user ids from messages in database."""
|
||||
with self.engine.connect() as con:
|
||||
result = con.execute("SELECT DISTINCT from_user FROM messages_utc;")
|
||||
return [user for user, in result.fetchall()]
|
||||
|
||||
def get_db_users(self) -> Dict[int, Tuple[str, str]]:
|
||||
def _get_db_users(self) -> Dict[int, Tuple[str, str]]:
|
||||
"""Returns dictionary mapping user ids to usernames and full names."""
|
||||
query = """
|
||||
select user_id, username, display_name from (
|
||||
select
|
||||
|
@ -105,7 +107,11 @@ class StatsRunner(object):
|
|||
|
||||
return {user_id: (username, name) for user_id, username, name in result}
|
||||
|
||||
def update_user_ids(self, user_dict: Dict[int, Tuple[str, str]]):
|
||||
def _update_user_ids(self, user_dict: Dict[int, Tuple[str, str]]):
|
||||
"""
|
||||
Updates user names table with user_dict
|
||||
:param user_dict: mapping of user ids to (username, display name)
|
||||
"""
|
||||
for uid in user_dict:
|
||||
username, display_name = user_dict[uid]
|
||||
sql_dict = {'uid': uid, 'username': username, 'display_name': display_name}
|
||||
|
|
Ładowanie…
Reference in New Issue