Move login logic out of __init__
This commit is contained in:
parent
aaf7445b8f
commit
c0ed667f18
@ -421,30 +421,43 @@ class FAAPI:
|
|||||||
A basic FurAffinity API instance.
|
A basic FurAffinity API instance.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, use_webdriver: bool = True):
|
def __init__(self):
|
||||||
"""
|
"""
|
||||||
Initialises a new FAAPI instance.
|
Initialises a new FAAPI instance.
|
||||||
Beware that a Chrome instance is launched to bypass Cloudflare and log in during initialisation. Change use_webdriver to False to disable this.
|
|
||||||
"""
|
"""
|
||||||
|
self._cookies = None
|
||||||
|
self._req = None
|
||||||
|
self._driver = None
|
||||||
|
self.logged_in = False
|
||||||
|
self.username = None
|
||||||
|
|
||||||
if not use_webdriver:
|
def login(self, cookies=None) -> None:
|
||||||
# Can't do any actual API things, stop here
|
"""
|
||||||
|
Logs this instance into FurAffinity.
|
||||||
|
Beware that a Chrome instance is launched to bypass Cloudflare and log in. Specify valid cookies to bypass this.
|
||||||
|
@param cookies: Cookies to use for this session.
|
||||||
|
"""
|
||||||
|
if self.logged_in:
|
||||||
|
# No need to do anything if we're already logged in
|
||||||
return
|
return
|
||||||
|
|
||||||
# Try sign in to FA
|
if not cookies:
|
||||||
self._driver = webdriver.Chrome()
|
# Try sign in to FA
|
||||||
self._driver.get(FA_BASE_URL + '/login')
|
self._driver = webdriver.Chrome()
|
||||||
|
self._driver.get(FA_BASE_URL + '/login')
|
||||||
|
|
||||||
# Wait until our username is visible on the top banner
|
# Wait until our username is visible on the top banner
|
||||||
while not self._driver.find_elements_by_id('my-username'):
|
while not self._driver.find_elements_by_id('my-username'):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Save the username of the account we've logged in as
|
# Save the username of the account we've logged in as
|
||||||
self.username = self._driver.find_elements_by_css_selector('#my-username:not(.hideondesktop)')[0].text.strip()
|
self.username = self._driver.find_elements_by_css_selector('#my-username:not(.hideondesktop)')[0].text.strip()
|
||||||
|
|
||||||
# Extract the cookies and stop the driver
|
# Extract the cookies and stop the driver
|
||||||
self._cookies = self._driver.get_cookies()
|
self._cookies = self._driver.get_cookies()
|
||||||
self._driver.close()
|
self._driver.close()
|
||||||
|
else:
|
||||||
|
self._cookies = cookies
|
||||||
|
|
||||||
# Set up a Requests session with the cookies we just got
|
# Set up a Requests session with the cookies we just got
|
||||||
self._req = requests.session()
|
self._req = requests.session()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user