mirror of
https://github.com/ClaytonWWilson/Scraper-for-theTVDB.com.git
synced 2025-12-15 17:28:46 +00:00
Saving login credentials to file
This commit is contained in:
parent
9c4bcefa04
commit
2c86a7ce8d
13
checks.py
13
checks.py
@ -1,5 +1,6 @@
|
||||
import json
|
||||
import requests
|
||||
import datetime
|
||||
|
||||
def getToken(data):#TODO add a timeout and try catch to all requests
|
||||
url = "https://api.thetvdb.com/login"
|
||||
@ -7,8 +8,12 @@ def getToken(data):#TODO add a timeout and try catch to all requests
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json"
|
||||
}
|
||||
try:
|
||||
response = requests.post(url, data=json.dumps(data), headers=headers)
|
||||
except requests.exceptions.ConnectionError as e:
|
||||
print("An error occurred. Please check your internet and try again.")
|
||||
quit()
|
||||
|
||||
response = requests.post(url, data=json.dumps(data), headers=headers)
|
||||
if (checkStatus(response, True)):
|
||||
parsed_token = json.loads(response.content)
|
||||
token = parsed_token["token"]
|
||||
@ -25,3 +30,9 @@ def checkStatus(response, v):
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
||||
def checkTimestamp(saveTime, curTime):
|
||||
if curTime - saveTime < datetime.timedelta(0, 86100, 0):# if less than 23h 55m since last token grab
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
35
login.py
35
login.py
@ -2,11 +2,12 @@ import json
|
||||
import os.path
|
||||
from checks import *
|
||||
import datetime
|
||||
import dateutil.parser
|
||||
|
||||
def login():
|
||||
if os.path.exists("login.json") == False:
|
||||
obj = open("login.json")
|
||||
# obj.write()
|
||||
obj = open("login.json", "w")
|
||||
obj.write("")
|
||||
obj.close()
|
||||
|
||||
if os.stat("login.json").st_size == 0:# Will only ask for credentials if the login file is empty
|
||||
@ -36,17 +37,29 @@ def login():
|
||||
}
|
||||
|
||||
tmp_token = getToken(LOGIN_DATA)
|
||||
|
||||
if tmp_token is "":
|
||||
print("Authentication failed. Please try again.")
|
||||
else:
|
||||
with open("login.json") as json_data:
|
||||
login["API_KEY"] = tmp_api_key
|
||||
login["USER_KEY"] = tmp_user_key
|
||||
login["USER_NAME"] = tmp_user_name
|
||||
login["TOKEN"] = tmp_token
|
||||
login["TIMESTAMP"] = datetime.datetime.now()
|
||||
obj = open("login.json", "w")
|
||||
obj.write(json.dumps(login))
|
||||
obj.close()
|
||||
login["API_KEY"] = tmp_api_key
|
||||
login["USER_KEY"] = tmp_user_key
|
||||
login["USER_NAME"] = tmp_user_name
|
||||
login["TOKEN"] = tmp_token
|
||||
login["TIMESTAMP"] = str(datetime.datetime.now().replace(tzinfo=None))
|
||||
obj = open("login.json", "w")
|
||||
obj.write(json.dumps(login))
|
||||
obj.close()
|
||||
else:# if login.json already exists
|
||||
|
||||
with open("login.json") as json_data:
|
||||
login = json.load(json_data)
|
||||
json_data.close()
|
||||
saveTime = dateutil.parser.parse(login["TIMESTAMP"])
|
||||
curTime = datetime.datetime.now().replace(tzinfo=None)
|
||||
|
||||
if checkTimestamp(saveTime, curTime):# token does not need refreshed
|
||||
print("token is good")
|
||||
else:
|
||||
print("token needs refreshed")
|
||||
# TODO try to get token, if token fails, ask for login info again, if it passes save login details to login.py and save token with timestamp.
|
||||
# TODO at startup, check token for validity and remove it if it is expired
|
||||
|
||||
Loading…
Reference in New Issue
Block a user