Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Robota #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion classes/mail_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,5 @@ def get_sender(self):

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually commit messages are don in an imperative format, take a look in https://chris.beams.io/posts/git-commit/

def get_subject(self):
subject = SubjectParser(self.mail['subject'])
print(subject.text)
return subject.text

4 changes: 4 additions & 0 deletions classes/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
class ComponentDatasheet(NamedTuple):
name: str
link: str

class MailSenderSubject(NamedTuple):
sender: str
subject: str
34 changes: 16 additions & 18 deletions mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,46 @@

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit messages is not clear, I believe that you want to say:
Changing email script to support multiple emails subjects in inbox or something like.

from classes.mail_wrapper import MailWrapper
from classes.imap import ImapGmailWrapper
from classes.types import MailSenderSubject

from config import mail, app_password, url_telegram, chat_id

def check_gmail():

imap = ImapGmailWrapper(mail, app_password)
imap.select_mail_box('inbox')
possible_new_email_id = int(imap.get_last_email_id('UNSEEN'))
new_email_id = int(imap.get_last_email_id('UNSEEN'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be squashed with 5453374


if not os.path.exists('recent_mail.txt'):
save_latest_email_id(possible_new_email_id)
save_latest_email_id(new_email_id)
return

file_reader = open('recent_mail.txt', 'r')
if file_reader.readable():
current_mail_id = int(file_reader.read())
file_reader.close()

if possible_new_email_id > current_mail_id:
latest_email_id = possible_new_email_id
number_of_new_mails = latest_email_id - current_mail_id
if new_email_id > current_mail_id:
save_latest_email_id(new_email_id)

file_reader.close()
senders_subjects = []
message = 'Shalom Adonai, irmãos. Novo(s) e-mail(s), deem uma olhada:\n'

save_latest_email_id(latest_email_id)
for mail_id in range(current_mail_id + 1, new_email_id + 1):
raw_email = imap.fetchs([mail_id])

raw_email = imap.fetchs([latest_email_id])
mail_parser = MailWrapper(raw_email)

mail_parser = MailWrapper(raw_email)
sender = mail_parser.get_sender()
senders_subjects.append(MailSenderSubject(mail_parser.get_sender(), mail_parser.get_subject()))

subject = mail_parser.get_subject()

if number_of_new_mails > 1:
message = 'Shalom Adonai, irmãos. {} novos emails, deem uma olhada.'.format(number_of_new_mails)
else:
message = 'Shalom Adonai, irmãos. Novo email, deem uma olhada.\n\nTítulo: {}\n\nRemetente: {}'.format(subject, sender)
for sender_subject in senders_subjects:
message += '\nRemetente: {}\nTítulo: {}\n'.format(sender_subject.sender, sender_subject.subject)

send_message(message)

def save_latest_email_id(latest_email_id):
def save_latest_email_id(new_email_id):
file_writer = open('recent_mail.txt', 'w')
file_writer.write(str(latest_email_id))
file_writer.write(str(new_email_id))
file_writer.close()

def send_message(message):
Expand Down