MatteoScript commited on
Commit
53b4c9f
·
verified ·
1 Parent(s): b73d5af

Update google_function.py

Browse files
Files changed (1) hide show
  1. google_function.py +29 -17
google_function.py CHANGED
@@ -11,6 +11,7 @@ from email.message import EmailMessage
11
  from bs4 import BeautifulSoup
12
  import webbrowser
13
  import datetime
 
14
  import streamlit as st
15
 
16
  SCOPES = ["https://www.googleapis.com/auth/gmail.compose",
@@ -21,6 +22,8 @@ SCOPES = ["https://www.googleapis.com/auth/gmail.compose",
21
  "https://www.googleapis.com/auth/forms.body",
22
  "https://www.googleapis.com/auth/spreadsheets.readonly"]
23
 
 
 
24
  #---------------------------------------------------------- LETTURA EMAIL ---------------------------------------------------------
25
  def converti_email_txt(body):
26
  try:
@@ -32,7 +35,6 @@ def converti_email_txt(body):
32
  return body
33
 
34
  def leggi_gmail(max_results=10):
35
- creds=connetti_google()
36
  links = []
37
  service = build("gmail", "v1", credentials=creds)
38
  results = service.users().messages().list(userId="me", labelIds=["INBOX"], q="is:unread", maxResults=max_results).execute()
@@ -76,7 +78,6 @@ def leggi_gmail(max_results=10):
76
  #---------------------------------------------------------- SCRITTURA BOZZA EMAIL ---------------------------------------------------------
77
  def scrivi_bozza_gmail(testo):
78
  draft_url = ''
79
- creds=connetti_google()
80
  try:
81
  service = build("gmail", "v1", credentials=creds)
82
  message = EmailMessage()
@@ -97,7 +98,6 @@ def scrivi_bozza_gmail(testo):
97
 
98
  #---------------------------------------------------------- LEGGI GOOGLE CALENDAR ---------------------------------------------------------
99
  def leggi_calendario_google(max_results=10):
100
- creds=connetti_google()
101
  try:
102
  service = build("calendar", "v3", credentials=creds)
103
  calendar_list_result = service.calendarList().list().execute()
@@ -133,29 +133,41 @@ def leggi_calendario_google(max_results=10):
133
  return descrizione_eventi, links
134
 
135
  #---------------------------------------------------------- CONNESSIONE ACCOUNT GOOGLE ---------------------------------------------------------
 
 
 
 
 
 
136
  def connetti_google():
137
  load_dotenv()
138
  json_variable_str = os.getenv("JSON_GOOGLE")
139
- print(json_variable_str)
140
  with open("./credentials.json", "w") as f:
141
  f.write(json_variable_str)
142
- if os.path.exists("./credentials.json"):
143
- print('ESISTE')
144
- else:
145
- print('NON ESISTE')
146
- creds = None
147
- st.write("Welcome to My App!")
148
- auth_code = st.query_params.get("code")
149
  flow = InstalledAppFlow.from_client_secrets_file("./credentials.json", SCOPES, redirect_uri='https://matteoscript-streamlitchat-new.hf.space')
150
- if auth_code:
151
- flow.fetch_token(code=auth_code)
 
 
 
 
 
152
  creds = flow.credentials
 
153
  st.write("Login Done")
 
154
  else:
155
- authorization_url, state = flow.authorization_url(include_granted_scopes="true",)
156
- print(authorization_url)
157
- st.markdown(f'<a href="{authorization_url}">Click here to sign in with Google</a>', unsafe_allow_html=True)
158
- return creds
 
 
 
 
 
 
 
159
 
160
  def connetti_google_2():
161
  load_dotenv()
 
11
  from bs4 import BeautifulSoup
12
  import webbrowser
13
  import datetime
14
+ from streamlit_javascript import st_javascript
15
  import streamlit as st
16
 
17
  SCOPES = ["https://www.googleapis.com/auth/gmail.compose",
 
22
  "https://www.googleapis.com/auth/forms.body",
23
  "https://www.googleapis.com/auth/spreadsheets.readonly"]
24
 
25
+ creds = None
26
+
27
  #---------------------------------------------------------- LETTURA EMAIL ---------------------------------------------------------
28
  def converti_email_txt(body):
29
  try:
 
35
  return body
36
 
37
  def leggi_gmail(max_results=10):
 
38
  links = []
39
  service = build("gmail", "v1", credentials=creds)
40
  results = service.users().messages().list(userId="me", labelIds=["INBOX"], q="is:unread", maxResults=max_results).execute()
 
78
  #---------------------------------------------------------- SCRITTURA BOZZA EMAIL ---------------------------------------------------------
79
  def scrivi_bozza_gmail(testo):
80
  draft_url = ''
 
81
  try:
82
  service = build("gmail", "v1", credentials=creds)
83
  message = EmailMessage()
 
98
 
99
  #---------------------------------------------------------- LEGGI GOOGLE CALENDAR ---------------------------------------------------------
100
  def leggi_calendario_google(max_results=10):
 
101
  try:
102
  service = build("calendar", "v3", credentials=creds)
103
  calendar_list_result = service.calendarList().list().execute()
 
133
  return descrizione_eventi, links
134
 
135
  #---------------------------------------------------------- CONNESSIONE ACCOUNT GOOGLE ---------------------------------------------------------
136
+ def local_storage_get(key):
137
+ return st_javascript(f"localStorage.getItem('{key}');")
138
+
139
+ def local_storage_set(key, value):
140
+ return st_javascript(f"localStorage.setItem('{key}', '{value}');")
141
+
142
  def connetti_google():
143
  load_dotenv()
144
  json_variable_str = os.getenv("JSON_GOOGLE")
 
145
  with open("./credentials.json", "w") as f:
146
  f.write(json_variable_str)
 
 
 
 
 
 
 
147
  flow = InstalledAppFlow.from_client_secrets_file("./credentials.json", SCOPES, redirect_uri='https://matteoscript-streamlitchat-new.hf.space')
148
+ token = local_storage_get("token")
149
+ print('------------1------------------')
150
+ print(token)
151
+ if token and token != '':
152
+ print('------------2------------------')
153
+ local_storage_set("token", '')
154
+ flow.fetch_token(code=token)
155
  creds = flow.credentials
156
+ print(creds)
157
  st.write("Login Done")
158
+ st.session_state.login_effettuato = True
159
  else:
160
+ print('------------3------------------')
161
+ auth_code = st.query_params.get("code")
162
+ if auth_code:
163
+ local_storage_set("token", auth_code)
164
+ st_javascript('window.location.href = "https://matteoscript-streamlitchat-new.hf.space";')
165
+ else:
166
+ print('------------4------------------')
167
+ st.write("")
168
+ authorization_url, state = flow.authorization_url(include_granted_scopes="true",)
169
+ st.write('## Login Bonsi A.I.')
170
+ st.markdown(f'<a href="{authorization_url}" target="_self">Effettua Login Google</a>', unsafe_allow_html=True)
171
 
172
  def connetti_google_2():
173
  load_dotenv()