Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge updates from base Manishearth's repo #5

Merged
merged 7 commits into from
Nov 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions chatexchange/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <x ago> 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
Expand Down
15 changes: 11 additions & 4 deletions chatexchange/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -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):
Expand Down