<jupyter_start><jupyter_text>Gmail - Schedule mailbox cleaning **Tags:** gmail productivity  Input  Import libraries<jupyter_code>import naas
from naas_drivers import email
import pandas as pd
import numpy as np
import plotly.express as px<jupyter_output><empty_output><jupyter_text>Account credentials<jupyter_code>username = "<EMAIL>"
password = "<PASSWORD>"
smtp_server = "imap.gmail.com"
box = "INBOX"<jupyter_output><empty_output><jupyter_text>Note: You need to create an application password following this procedure - https://support.google.com/mail/answer/185833?hl=en  Model  Setting the scheduler<jupyter_code>naas.scheduler.add(recurrence="0 9 * * *") # Scheduler set for 9 am<jupyter_output><empty_output><jupyter_text>Connect to email box<jupyter_code>emails = naas_drivers.email.connect(username, 
        password, 
        username, 
        smtp_server)<jupyter_output><empty_output><jupyter_text>Get email list<jupyter_code>dataframe = emails.get(criteria="seen")
dataframe<jupyter_output><empty_output><jupyter_text>Creating dataframe and inserting values<jupyter_code>sender_name = []
sender_email = []
for df in dataframe["from"]:
    sender_name.append(df['name'])
    sender_email.append(df['email'])
result = pd.DataFrame(columns = ['SENDER_NAME','SENDER_EMAIL','COUNT','PERCENTAGE'])
name_unique = np.unique(sender_name)
email_unique = np.unique(sender_email)
total_email = len(emails.get(criteria="seen")) + len(emails.get(criteria="unseen"))
c = 0
for i in np.unique(sender_name):
    new_row = {'SENDER_NAME':i,'SENDER_EMAIL':sender_email[c],'COUNT':sender_name.count(i),'PERCENTAGE':round(((sender_name.count(i))/total_email)*100)}
    result = result.append(new_row, ignore_index=True)
    c+=1
result<jupyter_output><empty_output><jupyter_text>Email graph plot<jupyter_code>fig = px.bar(x=result['COUNT'], y=result['SENDER_NAME'], orientation='h')
fig.show()<jupyter_output><empty_output><jupyter_text>Output  Deleting using email id<jupyter_code>d_email = "<EMAIL>"  # email id to be deleted
data_from = dataframe['from']
data_uid = dataframe['uid']
uid = []<jupyter_output><empty_output><jupyter_text>Updating the uid values<jupyter_code>for i in range(len(dataframe)):
    if data_from[i]['email'] == d_email:
        uid.append(data_uid[i])
print(uid)<jupyter_output><empty_output><jupyter_text>Deleting the emails<jupyter_code>for i in uid:
    attachments = emails.set_flag(i, "DELETED", True)<jupyter_output><empty_output><jupyter_text>Showing the upated email list<jupyter_code>dataframe = emails.get(criteria="seen")
dataframe<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13366718027734978, 'fraction_numerical': 0.00539291217257319, 'mean_word_length': 8.08041958041958, 'pattern_counts': {'":': 0, '<': 48, '<?xml version=': 0, '>': 48, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '13183', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 946, 'n_tokens_neox': 926, 'n_words': 149, 'path': 'Gmail/Gmail_Schedule_mailbox_cleaning.ipynb', 'repo_name': 'Charles-de-Montigny/awesome-notebooks', 'size': '2632'}