Add function to get the currently-logged in user

This commit is contained in:
Thomas Wade 2020-04-22 03:59:16 +09:30
parent 3a1be7daf6
commit c72ff23a4a

View File

@ -486,18 +486,6 @@ class FAAPI:
self._cookies = None self._cookies = None
self._req = requests.session() self._req = requests.session()
def get_user(self, username: str) -> User:
soup = self.get_soup('{}/user/{}/'.format(FA_BASE_URL, username))
try:
# Try extract the username, stripping the ~/∞/! in front
username = soup.select('div.username h2')[0].text.strip()[1:]
except IndexError:
# No username, give up
return None
return User.new_or_cached(self, username)
def get_soup(self, url: str) -> BeautifulSoup: def get_soup(self, url: str) -> BeautifulSoup:
""" """
Gets a webpage and wraps it in a BeautifulSoup instance. Gets a webpage and wraps it in a BeautifulSoup instance.
@ -509,3 +497,27 @@ class FAAPI:
raise Exception( raise Exception(
'Got status code {} when trying to get URL {}'.format(res.status_code, url)) 'Got status code {} when trying to get URL {}'.format(res.status_code, url))
return BeautifulSoup(res.content, 'lxml') return BeautifulSoup(res.content, 'lxml')
def get_user(self, username: str) -> User:
"""
Gets a user by their username.
:param username: Username
:return: User
"""
soup = self.get_soup('{}/user/{}/'.format(FA_BASE_URL, username))
try:
# Try extract the username, stripping the ~/∞/! in front
username = soup.select('div.username h2')[0].text.strip()[1:]
except IndexError:
# No username, give up
return None
return User.new_or_cached(self, username)
def get_self(self) -> User:
"""
Gets the user that this instance is logged in as.
:return: User that this instance is logged in as.
"""
return self.get_user(self.username) if self.logged_in else None