-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetToken.py
35 lines (26 loc) · 1018 Bytes
/
getToken.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import argparse
import requests
import xml.etree.ElementTree as ET
from dotenv import load_dotenv, find_dotenv
import os
parser = argparse.ArgumentParser(description='Login to Newberry API and get token')
parser.add_argument('username', type=str, help='Newberry API username')
parser.add_argument('password', type=str, help='Newberry API password')
args = parser.parse_args()
url = f'https://collections.newberry.org/API/Authentication/v2.0/Login?Login={args.username}&Password={args.password}'
response = requests.post(url)
root = ET.fromstring(response.content)
token = root.find('.//Token').text
# print(f'Token: {token}')
load_dotenv(find_dotenv())
# Write the new API key to the .env file
with open(find_dotenv(), 'r') as f:
lines = f.readlines()
with open(find_dotenv(), 'w') as f:
for line in lines:
if line.startswith('CORTEX_API_KEY'):
f.write(f'CORTEX_API_KEY="{token}"\n')
else:
f.write(line)
# Print the new API key
# print(f'New API key: {token}')