Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,146 +10,163 @@ import base64
|
|
| 10 |
|
| 11 |
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
}, token_file)
|
| 25 |
-
|
| 26 |
def authenticate_gmail(credentials_file):
|
| 27 |
-
|
| 28 |
-
creds = None
|
| 29 |
-
if os.path.exists("token.json"):
|
| 30 |
try:
|
| 31 |
-
creds = Credentials.from_authorized_user_file(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
except Exception as e:
|
| 33 |
st.error(f"Invalid token.json file: {e}")
|
| 34 |
-
os.remove(
|
| 35 |
-
|
| 36 |
-
if not creds or not creds.valid:
|
| 37 |
-
if creds and creds.expired and creds.refresh_token:
|
| 38 |
-
creds.refresh(Request())
|
|
|
|
|
|
|
|
|
|
| 39 |
else:
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
| 43 |
st.info("Please visit this URL to authorize the application:")
|
| 44 |
-
st.code(auth_url)
|
| 45 |
|
| 46 |
auth_code = st.text_input("Enter the authorization code:")
|
| 47 |
-
if st.button("Submit
|
| 48 |
try:
|
| 49 |
-
flow.fetch_token(code=auth_code)
|
| 50 |
-
creds = flow.credentials
|
| 51 |
-
save_credentials_to_file(creds)
|
| 52 |
-
st.success("Authentication successful!")
|
| 53 |
st.session_state.authenticated = True
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
except Exception as e:
|
| 57 |
st.error(f"Error during authentication: {e}")
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
|
|
|
| 62 |
def fetch_emails(service, label):
|
| 63 |
-
"""Fetch emails from Gmail API."""
|
| 64 |
emails = []
|
| 65 |
next_page_token = None
|
| 66 |
total_emails = 0
|
| 67 |
|
| 68 |
-
# Get total
|
| 69 |
-
results = service.users().messages().list(userId=
|
| 70 |
-
total_emails = results.get(
|
| 71 |
st.info(f"Total emails in {label}: {total_emails}")
|
| 72 |
|
| 73 |
progress_bar = st.progress(0)
|
| 74 |
|
| 75 |
while True:
|
| 76 |
results = service.users().messages().list(
|
| 77 |
-
userId=
|
| 78 |
).execute()
|
| 79 |
-
messages = results.get(
|
| 80 |
for message in messages:
|
| 81 |
-
msg = service.users().messages().get(userId=
|
| 82 |
-
headers = {header[
|
| 83 |
-
body =
|
| 84 |
-
if
|
| 85 |
-
for part in msg[
|
| 86 |
-
if part[
|
| 87 |
-
body = base64.urlsafe_b64decode(part[
|
| 88 |
email_data = {
|
| 89 |
-
"Date": headers.get(
|
| 90 |
-
"From": headers.get(
|
| 91 |
-
"To": headers.get(
|
| 92 |
-
"Subject": headers.get(
|
| 93 |
-
"Snippet": msg.get(
|
| 94 |
"Body": body,
|
| 95 |
-
"Attachments": "Yes" if
|
| 96 |
}
|
| 97 |
emails.append(email_data)
|
| 98 |
|
| 99 |
progress_bar.progress(min(len(emails) / total_emails, 1.0))
|
| 100 |
-
next_page_token = results.get(
|
| 101 |
if not next_page_token:
|
| 102 |
break
|
| 103 |
|
| 104 |
st.success(f"Fetched {len(emails)} emails from {label}")
|
| 105 |
return emails
|
| 106 |
|
| 107 |
-
|
| 108 |
-
# Pages
|
| 109 |
def main_page():
|
| 110 |
-
"
|
| 111 |
-
st.
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
credentials_file = st.file_uploader("Upload credentials.json", type="json")
|
| 116 |
|
| 117 |
-
if st.button("
|
| 118 |
-
if not credentials_file:
|
| 119 |
-
st.error("Please
|
| 120 |
else:
|
| 121 |
# Save credentials.json locally
|
| 122 |
with open("credentials.json", "wb") as f:
|
| 123 |
f.write(credentials_file.getbuffer())
|
| 124 |
-
|
| 125 |
-
if creds:
|
| 126 |
-
st.session_state.authenticated = True
|
| 127 |
-
st.session_state.creds = creds
|
| 128 |
-
st.experimental_rerun()
|
| 129 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
st.
|
| 134 |
-
st.sidebar.header("Navigation")
|
| 135 |
-
st.sidebar.radio("Go to:", ["Email Fetch Page"], key="page_selector")
|
| 136 |
|
| 137 |
-
|
| 138 |
-
st.error("You are not authenticated. Please go back to the Main Page.")
|
| 139 |
-
else:
|
| 140 |
-
label = st.selectbox("Select Label", ["INBOX", "SENT", "DRAFTS", "TRASH", "SPAM"])
|
| 141 |
-
if st.button("Fetch Emails"):
|
| 142 |
-
service = build("gmail", "v1", credentials=st.session_state.creds)
|
| 143 |
-
emails = fetch_emails(service, label)
|
| 144 |
-
if emails:
|
| 145 |
-
df = pd.DataFrame(emails)
|
| 146 |
-
st.dataframe(df)
|
| 147 |
-
csv = df.to_csv(index=False).encode("utf-8")
|
| 148 |
-
st.download_button("Download Emails as CSV", csv, f"{label}_emails.csv", "text/csv")
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
# Navigation Logic
|
| 152 |
-
if "authenticated" in st.session_state and st.session_state.authenticated:
|
| 153 |
-
email_fetch_page()
|
| 154 |
-
else:
|
| 155 |
main_page()
|
|
|
|
| 10 |
|
| 11 |
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']
|
| 12 |
|
| 13 |
+
# Initialize session state variables
|
| 14 |
+
if "authenticated" not in st.session_state:
|
| 15 |
+
st.session_state.authenticated = False
|
| 16 |
+
if "creds" not in st.session_state:
|
| 17 |
+
st.session_state.creds = None
|
| 18 |
+
if "auth_url" not in st.session_state:
|
| 19 |
+
st.session_state.auth_url = None
|
| 20 |
+
if "flow" not in st.session_state:
|
| 21 |
+
st.session_state.flow = None
|
| 22 |
+
|
| 23 |
+
# Authenticate Gmail API
|
|
|
|
|
|
|
| 24 |
def authenticate_gmail(credentials_file):
|
| 25 |
+
if os.path.exists('token.json'):
|
|
|
|
|
|
|
| 26 |
try:
|
| 27 |
+
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
|
| 28 |
+
if creds and creds.valid:
|
| 29 |
+
st.session_state.creds = creds
|
| 30 |
+
st.session_state.authenticated = True
|
| 31 |
+
fetch_emails_directly()
|
| 32 |
+
return
|
| 33 |
except Exception as e:
|
| 34 |
st.error(f"Invalid token.json file: {e}")
|
| 35 |
+
os.remove('token.json')
|
| 36 |
+
|
| 37 |
+
if not st.session_state.creds or not st.session_state.creds.valid:
|
| 38 |
+
if st.session_state.creds and st.session_state.creds.expired and st.session_state.creds.refresh_token:
|
| 39 |
+
st.session_state.creds.refresh(Request())
|
| 40 |
+
st.session_state.authenticated = True
|
| 41 |
+
fetch_emails_directly()
|
| 42 |
+
return
|
| 43 |
else:
|
| 44 |
+
if not st.session_state.flow:
|
| 45 |
+
st.session_state.flow = InstalledAppFlow.from_client_secrets_file(credentials_file, SCOPES)
|
| 46 |
+
st.session_state.flow.redirect_uri = 'http://localhost'
|
| 47 |
+
auth_url, _ = st.session_state.flow.authorization_url(prompt='consent')
|
| 48 |
+
st.session_state.auth_url = auth_url
|
| 49 |
+
|
| 50 |
st.info("Please visit this URL to authorize the application:")
|
| 51 |
+
st.code(st.session_state.auth_url)
|
| 52 |
|
| 53 |
auth_code = st.text_input("Enter the authorization code:")
|
| 54 |
+
if st.button("Submit Authentication Code"):
|
| 55 |
try:
|
| 56 |
+
st.session_state.flow.fetch_token(code=auth_code)
|
| 57 |
+
st.session_state.creds = st.session_state.flow.credentials
|
|
|
|
|
|
|
| 58 |
st.session_state.authenticated = True
|
| 59 |
+
# Save the credentials to a file for later use
|
| 60 |
+
with open('token.json', 'w') as token_file:
|
| 61 |
+
json.dump({
|
| 62 |
+
"token": st.session_state.creds.token,
|
| 63 |
+
"refresh_token": st.session_state.creds.refresh_token,
|
| 64 |
+
"token_uri": st.session_state.creds.token_uri,
|
| 65 |
+
"client_id": st.session_state.creds.client_id,
|
| 66 |
+
"client_secret": st.session_state.creds.client_secret,
|
| 67 |
+
"scopes": st.session_state.creds.scopes
|
| 68 |
+
}, token_file)
|
| 69 |
+
st.success("Authentication successful! Fetching emails...")
|
| 70 |
+
fetch_emails_directly()
|
| 71 |
except Exception as e:
|
| 72 |
st.error(f"Error during authentication: {e}")
|
| 73 |
|
| 74 |
+
def fetch_emails_directly():
|
| 75 |
+
if st.session_state.authenticated and st.session_state.creds:
|
| 76 |
+
service = build('gmail', 'v1', credentials=st.session_state.creds)
|
| 77 |
+
label = "INBOX" # Default label, you can make this dynamic
|
| 78 |
+
st.info(f"Fetching emails from {label}...")
|
| 79 |
+
emails = fetch_emails(service, label)
|
| 80 |
+
if emails:
|
| 81 |
+
df = pd.DataFrame(emails)
|
| 82 |
+
st.dataframe(df)
|
| 83 |
+
csv = df.to_csv(index=False).encode('utf-8')
|
| 84 |
+
st.download_button("Download Emails as CSV", csv, f"{label}_emails.csv", "text/csv")
|
| 85 |
+
else:
|
| 86 |
+
st.warning(f"No emails found in {label}.")
|
| 87 |
+
else:
|
| 88 |
+
st.error("You are not authenticated. Please authenticate again.")
|
| 89 |
|
| 90 |
+
# Fetch Emails from Gmail API
|
| 91 |
def fetch_emails(service, label):
|
|
|
|
| 92 |
emails = []
|
| 93 |
next_page_token = None
|
| 94 |
total_emails = 0
|
| 95 |
|
| 96 |
+
# Get total emails
|
| 97 |
+
results = service.users().messages().list(userId='me', labelIds=[label], maxResults=1).execute()
|
| 98 |
+
total_emails = results.get('resultSizeEstimate', 0)
|
| 99 |
st.info(f"Total emails in {label}: {total_emails}")
|
| 100 |
|
| 101 |
progress_bar = st.progress(0)
|
| 102 |
|
| 103 |
while True:
|
| 104 |
results = service.users().messages().list(
|
| 105 |
+
userId='me', labelIds=[label], maxResults=100, pageToken=next_page_token
|
| 106 |
).execute()
|
| 107 |
+
messages = results.get('messages', [])
|
| 108 |
for message in messages:
|
| 109 |
+
msg = service.users().messages().get(userId='me', id=message['id'], format='full').execute()
|
| 110 |
+
headers = {header['name']: header['value'] for header in msg['payload']['headers']}
|
| 111 |
+
body = ''
|
| 112 |
+
if 'parts' in msg['payload']:
|
| 113 |
+
for part in msg['payload']['parts']:
|
| 114 |
+
if part['mimeType'] == 'text/plain':
|
| 115 |
+
body = base64.urlsafe_b64decode(part['body'].get('data', '').encode('UTF-8')).decode('UTF-8')
|
| 116 |
email_data = {
|
| 117 |
+
"Date": headers.get('Date', ''),
|
| 118 |
+
"From": headers.get('From', ''),
|
| 119 |
+
"To": headers.get('To', ''),
|
| 120 |
+
"Subject": headers.get('Subject', ''),
|
| 121 |
+
"Snippet": msg.get('snippet', ''),
|
| 122 |
"Body": body,
|
| 123 |
+
"Attachments": "Yes" if 'attachmentId' in str(msg['payload']) else "No",
|
| 124 |
}
|
| 125 |
emails.append(email_data)
|
| 126 |
|
| 127 |
progress_bar.progress(min(len(emails) / total_emails, 1.0))
|
| 128 |
+
next_page_token = results.get('nextPageToken')
|
| 129 |
if not next_page_token:
|
| 130 |
break
|
| 131 |
|
| 132 |
st.success(f"Fetched {len(emails)} emails from {label}")
|
| 133 |
return emails
|
| 134 |
|
| 135 |
+
# Main Page
|
|
|
|
| 136 |
def main_page():
|
| 137 |
+
st.sidebar.title("Steps to Use")
|
| 138 |
+
st.sidebar.write("""
|
| 139 |
+
1. Enable Gmail API from [Google Cloud Console](https://console.cloud.google.com/).
|
| 140 |
+
2. Download the `credentials.json` file.
|
| 141 |
+
3. Enter your Gmail email and App Password.
|
| 142 |
+
4. Upload your `credentials.json` file.
|
| 143 |
+
5. Authenticate and proceed to fetch emails.
|
| 144 |
+
""")
|
| 145 |
+
|
| 146 |
+
st.title("Gmail Email Fetcher")
|
| 147 |
+
|
| 148 |
+
user_email = st.text_input("Gmail Email")
|
| 149 |
+
app_password = st.text_input("App Password", type="password")
|
| 150 |
credentials_file = st.file_uploader("Upload credentials.json", type="json")
|
| 151 |
|
| 152 |
+
if st.button("Authenticate"):
|
| 153 |
+
if not user_email or not app_password or not credentials_file:
|
| 154 |
+
st.error("Please provide all required inputs (Gmail, App Password, credentials.json)")
|
| 155 |
else:
|
| 156 |
# Save credentials.json locally
|
| 157 |
with open("credentials.json", "wb") as f:
|
| 158 |
f.write(credentials_file.getbuffer())
|
| 159 |
+
authenticate_gmail("credentials.json")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
+
# Display Authentication Status
|
| 162 |
+
if st.session_state.authenticated:
|
| 163 |
+
st.success("You are authenticated!")
|
| 164 |
+
else:
|
| 165 |
+
st.warning("You are not authenticated yet.")
|
| 166 |
|
| 167 |
+
# Navigation
|
| 168 |
+
if "page" not in st.session_state:
|
| 169 |
+
st.session_state.page = "main"
|
|
|
|
|
|
|
| 170 |
|
| 171 |
+
if st.session_state.page == "main":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
main_page()
|