-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeed2mail.py
executable file
·129 lines (110 loc) · 3.56 KB
/
feed2mail.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python3
#
# imports required modules
import argparse
import datetime
import jinja2
import json
import os
import pprint
import sys
import time
import utils
#
# set command-line arguments and parsing options
parser = argparse.ArgumentParser()
parser.add_argument('-a','--attachment', help='add HTML content as attachment', default=False, action='store_true')
parser.add_argument('-d','--debug', help='force debug mode (not used yet)', default=False, action='store_true')
parser.add_argument('-c','--config', help='config file name (defaut: config.json)', metavar='<filename>', action='store')
parser.add_argument('-t','--template', help='template file name (defaut: templates/feed2mail.html)', metavar='<filename>', action='store')
parser.add_argument('-o','--output', help='output file name', metavar='<filename>', action='store')
args = parser.parse_args()
#
# checks arguments and options
file_att = args.attachment if args.attachment else False
file_cfg = args.config if args.config else os.path.dirname(__file__) + '/config.json'
file_out = args.output if args.output else None
file_tpl = args.template if args.template else os.path.dirname(__file__) + '/templates/feed2mail.html'
#
# open configuration file
try:
with open( file_cfg,'r') as fh:
config = json.load(fh)
fh.close()
except Exception as e:
sys.stderr.write( '[ERROR] reading configuration file %s\n' % file_cfg )
sys.stderr.write( '[ERROR] %s\n' % str(e) )
sys.exit(1)
#
# overwrite debug config
if args.debug:
config['debug'] = True
#
# open template file
try:
with open(file_tpl,'r') as fh:
tpl = jinja2.Template(fh.read())
fh.close()
except Exception as e:
sys.stderr.write( '[ERROR] reading template file %s\n' % file_tpl )
sys.stderr.write( '[ERROR] %s\n' % str(e) )
sys.exit(1)
#
# walk through each feed sources
if config.get('feeds.sources'):
start = time.time()
data = []
# for each feed
for idx,url in enumerate( config['feeds.sources'] ):
sys.stderr.write( '[%0d/%0d]\tRetrieving %s ...\n' %(idx+1,len(config['feeds.sources']),url) )
# set a feed object
f = utils.feed()
# get feed content from URL and parse according to period
f.get(url)
f.parse( config['feeds.period'] )
if len(f.posts):
data.append( {'title':f.feed.title, 'posts':f.posts} )
# render template with data
html = tpl.render(
data = data,
time = '%.2f' % (time.time()-start),
sources = config['feeds.sources']
)
#
# exit if no feed sources
else:
sys.stderr.write( '[ERROR] no feed sources' )
sys.exit(0)
#
# if output option set then write HTML content to file
if file_out is not None:
try:
with open( file_out,'w') as fh:
fh.write( html.encode('utf8') )
fh.close()
except Exception as e:
sys.stderr.write( '[ERROR] opening output file %s\n' % file_out )
sys.stderr.write( '[ERROR] %s\n' % str(e) )
sys.exit(1)
#
# if email recipient set then prepare to send email
if config.get('email.recipient'):
dt = datetime.datetime.today()
# set email object and its header
e = utils.email(
host=config['smtp.host'],
port=config['smtp.port'],
ssl=config['smtp.ssl'],
user=config['smtp.user'],
password=config['smtp.pass'],
debug=config['debug']
)
e.header( config['email.sender'], config['email.recipient'], '%s %s' % (config['email.title'],dt.strftime('%d/%m/%Y')) )
# fit with HTML in body and attachment
e.body( html.encode('utf8') )
if file_att:
e.attachment( html.encode('utf8'), 'newsletter-%s.html' % dt.strftime('%Y%m%d') )
# finally send email
e.send()
#
# end