Add function to get the currently-logged in user
This commit is contained in:
parent
3a1be7daf6
commit
c72ff23a4a
@ -486,18 +486,6 @@ class FAAPI:
|
||||
self._cookies = None
|
||||
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:
|
||||
"""
|
||||
Gets a webpage and wraps it in a BeautifulSoup instance.
|
||||
@ -509,3 +497,27 @@ class FAAPI:
|
||||
raise Exception(
|
||||
'Got status code {} when trying to get URL {}'.format(res.status_code, url))
|
||||
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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user