diff --git a/chatexchange/_utils.py b/chatexchange/_utils.py index 6b8c984..7163ef7 100644 --- a/chatexchange/_utils.py +++ b/chatexchange/_utils.py @@ -55,6 +55,23 @@ def html_to_text(html): return s.get_text() +# Number of seconds since the user was last seen, based on <12d ago> data. +def parse_last_seen(text): + suffixes = { + 's': 1, + 'm': 60, + 'h': 3600, + 'd': 86400, + 'y': 31536000 + } + splat = text.split(' ') + assert len(splat) == 2, "text doesn't appear to be in format" + char = text[-1] + number = int(text[:-1]) + assert char in suffixes, "suffix char unrecognized" + return number * suffixes[char] + + class LazyFrom(object): """ A descriptor used when multiple lazy attributes depend on a common diff --git a/chatexchange/browser.py b/chatexchange/browser.py index 393fa74..97662dd 100644 --- a/chatexchange/browser.py +++ b/chatexchange/browser.py @@ -69,7 +69,7 @@ def _request( # Try again if we fail. We're blaming "the internet" for weirdness. MAX_HTTP_RETRIES = 5 # EGAD! A MAGIC NUMBER! attempt = 0 - while attempt <= MAX_HTTP_RETRIES: + while attempt <= MAX_HTTP_RETRIES: attempt += 1 response = None try: @@ -223,7 +223,7 @@ def _load_user(self, soup): @staticmethod def user_id_and_name_from_link(link_soup): user_name = link_soup.text - user_id = int(link_soup['href'].split('/')[2]) + user_id = int(link_soup['href'].split('/')[-2]) return user_id, user_name def _update_chat_fkey_and_user(self): @@ -410,7 +410,7 @@ def get_transcript_with_message(self, message_id): room_soups = transcript_soup.select('.room-name a') room_soup = room_soups[-1] - room_id = int(room_soup['href'].split('/')[2]) + room_id = int(room_soup['href'].split('/')[-2]) room_name = room_soup.text messages_data = [] @@ -549,12 +549,19 @@ def get_profile(self, user_id): else: reputation = -1 + stats_elements = profile_soup.select('.user-valuecell') + if len(stats_elements) >= 3: + last_seen = _utils.parse_last_seen(stats_elements[2].text) + else: + last_seen = _utils.parse_last_seen('20y ago') + return { 'name': name, 'is_moderator': is_moderator, 'message_count': message_count, 'room_count': room_count, - 'reputation': reputation + 'reputation': reputation, + 'last_seen': last_seen } def get_room_info(self, room_id):