-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetToken.py
89 lines (78 loc) · 2.54 KB
/
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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 10 08:10:50 2018
@authors: jfarrugia and mheesema
"""
import requests
import re
import tkinter as tk
from appdirs import user_data_dir
import os
root = tk.Tk()
def Token():
global tokenpath
tokenpath = user_data_dir("ONC-Token", "ONC")
if os.path.exists(tokenpath + r"\token.txt"):
print("Token file exists.")
f = open(tokenpath + r"\token.txt", "r")
token = f.read()
f.close()
return token
else:
if not os.path.isdir(tokenpath):
print("Retrieving new token.")
NewToken(root)
root.mainloop()
token = NewToken.get_token()
return token
class NewToken():
"""
A class to handle login credentials for Oceans 2.0:
Retrieves user token for API data download.
"""
def __init__(self, master):
global email, password
tk.Label(master, text='E-Mail: ').grid(row=0)
tk.Label(master, text='Password: ').grid(row=1)
self.e1 = tk.Entry(master)
self.e2 = tk.Entry(master, show='*')
self.e1.grid(row=0, column=1)
self.e2.grid(row=1, column=1)
enter_button = tk.Button(master, text='Enter', command=self.enter)
enter_button.grid(row=2, columnspan=2, sticky="WNES")
email = ""
password = ""
def enter(self):
"""
Button. Close the current app and assign e-mail and password global variables.
"""
global email, password
email = self.e1.get()
password = self.e2.get()
root.destroy() #delete Frame to continue
def get_token():
"""
Return the user specific token for API data download.
"""
s = requests.Session()
loginURL = 'https://data.oceannetworks.ca/Login'
payload = {
'email' : email,
'password' : password,
'submit' : 'Login',
'action' : 'login'
}
r = s.post(loginURL, data=payload)
r = s.get('https://data.oceannetworks.ca/Profile')
try:
token=str(re.findall('<span id="generated_token">(.*)</span>',r.text)[0])
except IndexError as e:
print('Incorrect e-mail and/or password.\n\n', e)
#create directory and store token in file
os.makedirs(tokenpath)
f = open(tokenpath + r"\token.txt", "w+")
f.write(token)
f.close()
return token
if __name__=='__main__':
token = Token()