mirror of
https://github.com/ClaytonWWilson/Scraper-for-theTVDB.com.git
synced 2025-12-18 10:18:48 +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 json
|
||||||
import requests
|
import requests
|
||||||
|
import datetime
|
||||||
|
|
||||||
def getToken(data):#TODO add a timeout and try catch to all requests
|
def getToken(data):#TODO add a timeout and try catch to all requests
|
||||||
url = "https://api.thetvdb.com/login"
|
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",
|
"Content-Type": "application/json",
|
||||||
"Accept": "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)):
|
if (checkStatus(response, True)):
|
||||||
parsed_token = json.loads(response.content)
|
parsed_token = json.loads(response.content)
|
||||||
token = parsed_token["token"]
|
token = parsed_token["token"]
|
||||||
@ -25,3 +30,9 @@ def checkStatus(response, v):
|
|||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
return True
|
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
|
import os.path
|
||||||
from checks import *
|
from checks import *
|
||||||
import datetime
|
import datetime
|
||||||
|
import dateutil.parser
|
||||||
|
|
||||||
def login():
|
def login():
|
||||||
if os.path.exists("login.json") == False:
|
if os.path.exists("login.json") == False:
|
||||||
obj = open("login.json")
|
obj = open("login.json", "w")
|
||||||
# obj.write()
|
obj.write("")
|
||||||
obj.close()
|
obj.close()
|
||||||
|
|
||||||
if os.stat("login.json").st_size == 0:# Will only ask for credentials if the login file is empty
|
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)
|
tmp_token = getToken(LOGIN_DATA)
|
||||||
|
|
||||||
if tmp_token is "":
|
if tmp_token is "":
|
||||||
print("Authentication failed. Please try again.")
|
print("Authentication failed. Please try again.")
|
||||||
else:
|
else:
|
||||||
with open("login.json") as json_data:
|
login["API_KEY"] = tmp_api_key
|
||||||
login["API_KEY"] = tmp_api_key
|
login["USER_KEY"] = tmp_user_key
|
||||||
login["USER_KEY"] = tmp_user_key
|
login["USER_NAME"] = tmp_user_name
|
||||||
login["USER_NAME"] = tmp_user_name
|
login["TOKEN"] = tmp_token
|
||||||
login["TOKEN"] = tmp_token
|
login["TIMESTAMP"] = str(datetime.datetime.now().replace(tzinfo=None))
|
||||||
login["TIMESTAMP"] = datetime.datetime.now()
|
obj = open("login.json", "w")
|
||||||
obj = open("login.json", "w")
|
obj.write(json.dumps(login))
|
||||||
obj.write(json.dumps(login))
|
obj.close()
|
||||||
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 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
|
# TODO at startup, check token for validity and remove it if it is expired
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user