-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
93 lines (80 loc) · 2.78 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import json
import requests
import smtplib
import textwrap
from bs4 import BeautifulSoup
from datetime import datetime
from email.header import Header
from email.mime.text import MIMEText
def get_dividend(url):
html_doc = requests.get(url)
soup = BeautifulSoup(html_doc.text, 'html.parser')
table = soup.find('table', {'id': 'divCapGainsTable'})
thead = [
'distribution-type',
'distribution',
'record-date',
'ex-dividend-date',
'payable-date',
'distribution-yield',
'sec-yield'
]
ex_dividend_list = []
for tr in table.find_all('tr')[1:]:
ex_dividend_dict = {}
for index, td in enumerate(tr.find_all('td')):
ex_dividend_dict[thead[index]] = td.text
ex_dividend_list.append(ex_dividend_dict)
for record in ex_dividend_list:
if record['ex-dividend-date'] == today:
message = textwrap.dedent("""\
<table border="0" style="font-family: Verdana">
<tr>
<td>
<h3>Today is {} Ex-Dividend Date</h3>
</td>
</tr>
<tr>
<td>
<ul>
<li>Type: {}</li>
<li>Distribution: {}</li>
<li>Payable Date: {}</li>
</ul>
</td>
</tr>
</table>
""".format(
symbol,
record['distribution-type'],
record['distribution'],
record['payable-date'])
)
send_email(message)
def send_email(message):
global symbol, config
try:
# connect to SMTP server
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login(config['email'], config['password'])
# create email content
email_text = MIMEText(message, 'html', 'utf-8')
email_text['From'] = Header(config['email'], 'utf-8')
email_text['To'] = Header(', '.join(config['recipients']), 'utf-8')
email_text['Subject'] = Header('Today is {} Ex-Dividend Date'.format(symbol), 'utf-8')
# send email notification
server.sendmail(config['email'], config['recipients'], email_text.as_string())
server.quit()
except smtplib.SMTPAuthenticationError as e:
exit(e)
except smtplib.SMTPServerDisconnected as e:
exit(e)
except smtplib.SMTPException as e:
exit(e)
if __name__ == '__main__':
config = json.load(open('config.json'))
today = datetime.now().strftime('%m/%d/%Y')
for target in config['target_list']:
symbol = target['symbol']
get_dividend(config['base_url'] + target['fund_id'])