-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.py
73 lines (60 loc) · 2.31 KB
/
config.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
from sqlalchemy import create_engine
from sqlalchemy import Table, Column, String, MetaData
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from google_auth_oauthlib.flow import InstalledAppFlow
from pymstodo import ToDoConnection
from dotenv import load_dotenv
import os
import codecs
import pickle
if os.path.exists('.env'):
load_dotenv()
url = os.environ['DATABASE_URL'].replace('postgres', 'postgresql')
db = create_engine(url)
base = declarative_base()
Session = sessionmaker(db)
session = Session()
class Config(base):
__tablename__ = 'config'
token_google = Column(String, primary_key=True)
token_todo = Column(String)
devoirs = Column(String)
notes = Column(String)
base.metadata.create_all(db)
if __name__ == '__main__':
scopes = ['https://www.googleapis.com/auth/calendar']
try:
flow = InstalledAppFlow.from_client_secrets_file(
"credentials.json", scopes=scopes)
credentials = flow.run_console()
except ValueError:
credentials = None
print("VALUE ERROR -> You don't want to use Google Calendar")
tokenGoogle = codecs.encode(pickle.dumps(credentials), "base64").decode()
# identifiant de connexion à l'app
client_id = '7035ca26-c986-4bae-8b6b-f1db229e05c5'
client_secret = 'HuN_p3_St9~7c36U4H4k8B0pjPEVdi_2bM'
auth_url = ToDoConnection.get_auth_url(client_id)
redirect_resp = input(
f'Go to this link do all the steps:\n{auth_url}\n\nPaste the redirect link here\n')
try:
token = ToDoConnection.get_token(
client_id, client_secret, redirect_resp)
todo_client = ToDoConnection(
client_id=client_id, client_secret=client_secret, token=token)
except ValueError:
todo_client = None
print("VALUE ERROR -> You don't want to use Microsoft ToDo")
tokenToDo = codecs.encode(pickle.dumps(todo_client), "base64").decode()
if session.query(Config).count() == 0:
line = Config(token_google=tokenGoogle,
token_todo=tokenToDo, devoirs=None, notes=None)
session.add(line)
session.commit()
else:
line = session.query(Config).one()
line.token_google = tokenGoogle
line.token_todo = tokenToDo
session.commit()
print('End of configuration')