Skip to content

Commit

Permalink
added check to stop email sending every time application is run
Browse files Browse the repository at this point in the history
  • Loading branch information
ph1go committed Nov 1, 2021
1 parent 7969096 commit 30567f6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 28 deletions.
1 change: 1 addition & 0 deletions constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
source_path = Path(__file__).parent
config_file = source_path / 'config.ini'
validators_file = source_path / 'validators.txt'
notified_file = source_path / 'notified.json'

cfg = configparser.RawConfigParser()

Expand Down
74 changes: 46 additions & 28 deletions functions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

import requests
import re
import smtplib, ssl
Expand All @@ -6,7 +8,7 @@
from dataclasses import dataclass, field, InitVar
from typing import List

from constants import validators_file, config_file, finalized_url, genesis_url, block_url, email_details
from constants import validators_file, config_file, notified_file, finalized_url, genesis_url, block_url, email_details


def fetch_url(url):
Expand Down Expand Up @@ -272,40 +274,56 @@ def generate_notification(current_committee, next_committee):
print(f'{v_msg_1}\n\n {v_msg_2}')

if email_details.are_valid:
msg = EmailMessage()
if notified_file.is_file():
with notified_file.open() as f:
notified_committees = json.load(f)

msg['subject '] = (
f'You have {"validators" if num_both > 1 else "a validator"} in the '
f'{"current and next" if in_both else "current" if in_current else "next"} '
f'sync committee{"s" if in_both else ""}!'
)
else:
notified_committees = {'current': '0', 'next': '0'}

if (
current_committee.epoch_number != notified_committees['current'] and
next_committee.epoch_number != notified_committees['next']
):
msg = EmailMessage()

msg['subject '] = (
f'You have {"validators" if num_both > 1 else "a validator"} in the '
f'{"current and next" if in_both else "current" if in_current else "next"} '
f'sync committee{"s" if in_both else ""}!'
)

body = ''
body = ''

if num_current:
body += (
f'sync committee: current\n'
f'validators: {current_committee.validators_str}\n'
f'committee end time: {current_committee.end_str}'
)
if num_current:
body += (
f'sync committee: current\n'
f'validators: {current_committee.validators_str}\n'
f'committee end time: {current_committee.end_str}'
)

if num_next:
body += "\n\n" if num_current else ""
body += (
f'sync committee: next\n'
f'validators: {next_committee.validators_str}\n'
f'committee start time: {next_committee.start_str}\n'
f'committee end time: {next_committee.end_str}'
)
if num_next:
body += "\n\n" if num_current else ""
body += (
f'sync committee: next\n'
f'validators: {next_committee.validators_str}\n'
f'committee start time: {next_committee.start_str}\n'
f'committee end time: {next_committee.end_str}'
)

a = v_msg_2.replace("\n ", "\n")
body += f'\n\n{a}'

msg.set_content(body)
msg['From'] = email_details.from_addr
msg['To'] = email_details.to_addr

a = v_msg_2.replace("\n ", "\n")
body += f'\n\n{a}'
send_email(msg)

msg.set_content(body)
msg['From'] = email_details.from_addr
msg['To'] = email_details.to_addr
notified_committees = {'current': current_committee.epoch_number, 'next': next_committee.epoch_number}

send_email(msg)
with notified_file.open('w') as f:
json.dump(notified_committees, f)

else:
print(
Expand Down

0 comments on commit 30567f6

Please sign in to comment.