Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import datetime | |
| import uuid | |
| import os | |
| from google_auth_oauthlib.flow import Flow | |
| from googleapiclient.discovery import build | |
| import gspread | |
| # ========================================================== | |
| # CONFIG | |
| # ========================================================== | |
| APP_NAME = "UnreadGbox β¨" | |
| SPREADSHEET_URL = "https://docs.google.com/spreadsheets/d/1j_uDxzWfp2PiZyJXDbFPz5Pa8HLg8zFTuJAYq1XiTDo/edit?usp=drivesdk" | |
| SHEET_NAME = "UnreadGbox" | |
| SCOPES = [ | |
| "openid", | |
| "https://www.googleapis.com/auth/userinfo.email", | |
| "https://www.googleapis.com/auth/userinfo.profile", | |
| "https://www.googleapis.com/auth/gmail.modify", | |
| "https://www.googleapis.com/auth/spreadsheets", | |
| ] | |
| CLIENT_ID = os.environ.get("GOOGLE_CLIENT_ID") | |
| CLIENT_SECRET = os.environ.get("GOOGLE_CLIENT_SECRET") | |
| PROJECT_ID = os.environ.get("GOOGLE_PROJECT_ID") | |
| BASE_URL = os.environ.get("BASE_URL") | |
| # ========================================================== | |
| # PAGE SETUP | |
| # ========================================================== | |
| st.set_page_config( | |
| page_title="UnreadGbox β¨", | |
| page_icon="β¨", | |
| layout="centered" | |
| ) | |
| st.markdown("# β¨ UnreadGbox") | |
| st.caption("Minimal Gmail Cleanup Tool") | |
| st.divider() | |
| if not CLIENT_ID or not CLIENT_SECRET or not PROJECT_ID or not BASE_URL: | |
| st.error("Missing Hugging Face Secrets.") | |
| st.stop() | |
| # ========================================================== | |
| # OAUTH FLOW | |
| # ========================================================== | |
| def create_flow(): | |
| flow = Flow.from_client_config( | |
| { | |
| "web": { | |
| "client_id": CLIENT_ID, | |
| "project_id": PROJECT_ID, | |
| "auth_uri": "https://accounts.google.com/o/oauth2/auth", | |
| "token_uri": "https://oauth2.googleapis.com/token", | |
| "client_secret": CLIENT_SECRET, | |
| "redirect_uris": [BASE_URL], | |
| } | |
| }, | |
| scopes=SCOPES, | |
| ) | |
| flow.redirect_uri = BASE_URL | |
| return flow | |
| def login(): | |
| flow = create_flow() | |
| auth_url, state = flow.authorization_url( | |
| access_type="offline", | |
| include_granted_scopes="true", | |
| prompt="consent", | |
| ) | |
| st.session_state["state"] = state | |
| st.markdown(f"[π Login with Google]({auth_url})") | |
| st.stop() | |
| def fetch_token(): | |
| if "code" not in st.query_params: | |
| login() | |
| flow = create_flow() | |
| flow.fetch_token( | |
| code=st.query_params["code"], | |
| include_client_id=True, | |
| ) | |
| return flow.credentials | |
| # ========================================================== | |
| # AUTHENTICATION | |
| # ========================================================== | |
| if "credentials" not in st.session_state: | |
| credentials = fetch_token() | |
| st.session_state["credentials"] = credentials | |
| creds = st.session_state["credentials"] | |
| # ========================================================== | |
| # BUILD GOOGLE SERVICES | |
| # ========================================================== | |
| gmail_service = build("gmail", "v1", credentials=creds) | |
| gc = gspread.authorize(creds) | |
| sheet = gc.open_by_url(SPREADSHEET_URL).worksheet(SHEET_NAME) | |
| # ========================================================== | |
| # GET USER INFO | |
| # ========================================================== | |
| profile = gmail_service.users().getProfile(userId="me").execute() | |
| user_email = profile.get("emailAddress") | |
| st.success(f"Logged in as: {user_email}") | |
| st.divider() | |
| # ========================================================== | |
| # CLEAN UNREAD EMAILS | |
| # ========================================================== | |
| if st.button("β¨ Clean Unread Emails"): | |
| with st.spinner("Scanning unread emails..."): | |
| results = gmail_service.users().messages().list( | |
| userId="me", | |
| q="is:unread" | |
| ).execute() | |
| messages = results.get("messages", []) | |
| unread_count = len(messages) | |
| if unread_count == 0: | |
| st.success("No unread emails π") | |
| else: | |
| for msg in messages: | |
| gmail_service.users().messages().trash( | |
| userId="me", | |
| id=msg["id"] | |
| ).execute() | |
| now = datetime.datetime.now() | |
| sheet.append_row([ | |
| str(uuid.uuid4()), | |
| now.strftime("%Y-%m-%d"), | |
| now.strftime("%H:%M:%S"), | |
| user_email.split("@")[0], | |
| user_email, | |
| unread_count | |
| ]) | |
| st.success(f"{unread_count} unread emails moved to Trash β ") | |
| st.divider() | |
| st.caption("UnreadGbox β¨ β Secure Micro SaaS") |