Cache FAAPI instance in scraper example

This commit is contained in:
Thomas Wade 2020-12-03 23:59:59 +10:30
parent 4b72b7c593
commit 421d87ffe2

View File

@ -3,6 +3,7 @@
import multiprocessing import multiprocessing
import requests import requests
import shelve
from FAAPI import FAAPI from FAAPI import FAAPI
@ -20,11 +21,20 @@ def download(url: str):
if __name__ == '__main__': if __name__ == '__main__':
# Get an API instance # Get an API instance
api = FAAPI() shelf = shelve.open('api.shelf')
try:
api = shelf['api'] if 'api' in shelf.keys() else FAAPI()
except:
api = FAAPI()
shelf['api'] = api
# Log in # Log in
api.login() api.login()
# Save the API
shelf['api'] = api
shelf.close()
# Get the user we've authenticated as # Get the user we've authenticated as
user = api.get_user(api.username) user = api.get_user(api.username)
if not user: if not user: