-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenhandler.py
71 lines (65 loc) · 2.84 KB
/
tokenhandler.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# get token
import datetime
import os.path
import json
import requests
from requests.auth import HTTPBasicAuth
def read_key(filename):
if not os.path.isfile(filename):
print(filename+" doesn't exist")
f = open(filename, "rt")
data = json.loads(f.readline())
return data
def get_token():
credentials = read_key("wcl.key")
currentDate = datetime.datetime.today()
file_exists = os.path.isfile("token.trk")
token = ""
if file_exists:
print("found a token file")
f = open("token.trk", "rt")
data = json.loads(f.readline())
expiryTimeStamp = data['expires']
token = data['token']
f.close()
print("currentDate time stamp is ", currentDate.timestamp(), "expiry time stamp is ", expiryTimeStamp)
if not file_exists or float(expiryTimeStamp) - currentDate.timestamp() < 0:
print("token not there or expired, trying to retrieve a new one")
oauth = requests.post("https://www.warcraftlogs.com/oauth/token",
auth=HTTPBasicAuth(credentials['key'],
credentials['secret']),
data={'grant_type': 'client_credentials'})
token = "Bearer " + oauth.json()['access_token']
delta = datetime.timedelta(seconds=oauth.json()["expires_in"] - 1000)
trackingToken = {"token": token, "expires": str((currentDate + delta).timestamp())}
f = open("token.trk", "wt")
f.write(json.dumps(trackingToken))
f.close()
return token
def get_btoken():
credentials = read_key("blizzard.key")
currentDate = datetime.datetime.today()
file_exists = os.path.isfile("btoken.trk")
token = ""
credentials = read_key("blizzard.key")
if file_exists:
print("found a blizzard token file")
f = open("btoken.trk", "rt")
data = json.loads(f.readline())
expiryTimeStamp = data['expires']
token = data['token']
f.close()
print("currentDate time stamp is ", currentDate.timestamp(), "expiry time stamp is ", expiryTimeStamp)
if not file_exists or float(expiryTimeStamp) - currentDate.timestamp() < 0:
print("btoken not there or expired, trying to retrieve a new one")
oauth = requests.post("https://us.battle.net/oauth/token",
auth=HTTPBasicAuth(credentials['key'],
credentials['secret']),
data={'grant_type': 'client_credentials'})
token = "Bearer " + oauth.json()['access_token']
delta = datetime.timedelta(seconds=oauth.json()["expires_in"] - 100)
trackingToken = {"token": token, "expires": str((currentDate + delta).timestamp())}
f = open("btoken.trk", "wt")
f.write(json.dumps(trackingToken))
f.close()
return token