Spaces:
Running
Running
Update google_function.py
Browse files10 unmodified lines1112131415164 unmodified lines2122232425265 unmodified lines3233343536373837 unmodified lines7677787980818214 unmodified lines97989910010110210329 unmodified lines13313413513613713813914014114214314414514614714814915015115215315415515615715815916016110 unmodified linesfrom bs4 import BeautifulSoupimport webbrowserimport datetimeimport streamlit as stSCOPES = ["https://www.googleapis.com/auth/gmail.compose",4 unmodified lines"https://www.googleapis.com/auth/forms.body","https://www.googleapis.com/auth/spreadsheets.readonly"]#---------------------------------------------------------- LETTURA EMAIL ---------------------------------------------------------def converti_email_txt(body):try:5 unmodified linesreturn bodydef leggi_gmail(max_results=10):creds=connetti_google()links = []service = build("gmail", "v1", credentials=creds)results = service.users().messages().list(userId="me", labelIds=["INBOX"], q="is:unread", maxResults=max_results).execute()37 unmodified lines#---------------------------------------------------------- SCRITTURA BOZZA EMAIL ---------------------------------------------------------def scrivi_bozza_gmail(testo):draft_url = ''creds=connetti_google()try:service = build("gmail", "v1", credentials=creds)message = EmailMessage()14 unmodified lines#---------------------------------------------------------- LEGGI GOOGLE CALENDAR ---------------------------------------------------------def leggi_calendario_google(max_results=10):creds=connetti_google()try:service = build("calendar", "v3", credentials=creds)calendar_list_result = service.calendarList().list().execute()29 unmodified linesreturn descrizione_eventi, links#---------------------------------------------------------- CONNESSIONE ACCOUNT GOOGLE ---------------------------------------------------------def connetti_google():load_dotenv()json_variable_str = os.getenv("JSON_GOOGLE")print(json_variable_str)with open("./credentials.json", "w") as f:f.write(json_variable_str)if os.path.exists("./credentials.json"):print('ESISTE')else:print('NON ESISTE')creds = Nonest.write("Welcome to My App!")auth_code = st.query_params.get("code")flow = InstalledAppFlow.from_client_secrets_file("./credentials.json", SCOPES, redirect_uri='https://matteoscript-streamlitchat-new.hf.space')if auth_code:flow.fetch_token(code=auth_code)creds = flow.credentialsst.write("Login Done")else:authorization_url, state = flow.authorization_url(include_granted_scopes="true",)print(authorization_url)st.markdown(f'<a href="{authorization_url}">Click here to sign in with Google</a>', unsafe_allow_html=True)return credsdef connetti_google_2():load_dotenv()10 unmodified lines111213141516174 unmodified lines22232425262728295 unmodified lines35363738394037 unmodified lines78798081828314 unmodified lines989910010110210329 unmodified lines13313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217310 unmodified linesfrom bs4 import BeautifulSoupimport webbrowserimport datetimefrom streamlit_javascript import st_javascriptimport streamlit as stSCOPES = ["https://www.googleapis.com/auth/gmail.compose",4 unmodified lines"https://www.googleapis.com/auth/forms.body","https://www.googleapis.com/auth/spreadsheets.readonly"]creds = None#---------------------------------------------------------- LETTURA EMAIL ---------------------------------------------------------def converti_email_txt(body):try:5 unmodified linesreturn bodydef leggi_gmail(max_results=10):links = []service = build("gmail", "v1", credentials=creds)results = service.users().messages().list(userId="me", labelIds=["INBOX"], q="is:unread", maxResults=max_results).execute()37 unmodified lines#---------------------------------------------------------- SCRITTURA BOZZA EMAIL ---------------------------------------------------------def scrivi_bozza_gmail(testo):draft_url = ''try:service = build("gmail", "v1", credentials=creds)message = EmailMessage()14 unmodified lines#---------------------------------------------------------- LEGGI GOOGLE CALENDAR ---------------------------------------------------------def leggi_calendario_google(max_results=10):try:service = build("calendar", "v3", credentials=creds)calendar_list_result = service.calendarList().list().execute()29 unmodified linesreturn descrizione_eventi, links#---------------------------------------------------------- CONNESSIONE ACCOUNT GOOGLE ---------------------------------------------------------def local_storage_get(key):return st_javascript(f"localStorage.getItem('{key}');")def local_storage_set(key, value):return st_javascript(f"localStorage.setItem('{key}', '{value}');")def connetti_google():load_dotenv()json_variable_str = os.getenv("JSON_GOOGLE")with open("./credentials.json", "w") as f:f.write(json_variable_str)flow = InstalledAppFlow.from_client_secrets_file("./credentials.json", SCOPES, redirect_uri='https://matteoscript-streamlitchat-new.hf.space')token = local_storage_get("token")print('------------1------------------')print(token)if token and token != '':print('------------2------------------')local_storage_set("token", '')flow.fetch_token(code=token)creds = flow.credentialsprint(creds)st.write("Login Done")st.session_state.login_effettuato = Trueelse:print('------------3------------------')auth_code = st.query_params.get("code")if auth_code:local_storage_set("token", auth_code)st_javascript('window.location.href = "https://matteoscript-streamlitchat-new.hf.space";')else:print('------------4------------------')st.write("")authorization_url, state = flow.authorization_url(include_granted_scopes="true",)st.write('## Login Bonsi A.I.')st.markdown(f'<a href="{authorization_url}" target="_self">Effettua Login Google</a>', unsafe_allow_html=True)def connetti_google_2():load_dotenv()
google_function.py+29-17