File tree Expand file tree Collapse file tree 4 files changed +23
-2
lines changed Expand file tree Collapse file tree 4 files changed +23
-2
lines changed Original file line number Diff line number Diff line change 4
4
from mail import sendmail
5
5
import json
6
6
7
+ # Scraping the webpage and storing the data in a csv
7
8
data = scraper ('http://automatescrape.surge.sh/' )
8
9
write_csv (data )
9
10
11
+ # Reading the scraped data from the csv and preprocessing the data
10
12
participants = read_csv ()
11
13
participants = preprocess (participants )
12
14
15
+ # Getting the list of mails to whom mails have already been sent
13
16
sent_mails = read_file ()
14
- mails = []
15
17
18
+ # Looping over all participants
16
19
for participant in participants :
20
+ # Checking if the participant was sent a mail previously
17
21
if participant ['email' ] not in sent_mails :
22
+ # Generating a message from the template
18
23
msg = _render_template (participant ['name' ], participant ['payment' ])
24
+ # Sending the message to the participant via mail
19
25
response = sendmail (to_email = participant ['email' ], msg = msg )
20
26
if response ['email_status' ] == "Success" :
21
- mails . append ( participant [ ' email' ])
27
+ # if mail was sent successfully append the email to sentmails.txt
22
28
write_file (participant ['email' ])
Original file line number Diff line number Diff line change 2
2
3
3
4
4
def read_csv ():
5
+ # opening the file in read mode
5
6
with open ("studentdetails.csv" , 'r' ) as csv_file :
7
+ # reading the csv
6
8
csv_reader = csv .reader (csv_file , delimiter = ',' )
7
9
data = []
8
10
for row in csv_reader :
@@ -11,18 +13,22 @@ def read_csv():
11
13
12
14
13
15
def write_csv (data ):
16
+ # opening the file in write mode
14
17
with open ("studentdetails.csv" , 'w' ) as csv_file :
18
+ # writing to the csv
15
19
csv_writer = csv .writer (csv_file , delimiter = ',' )
16
20
for row in data :
17
21
csv_writer .writerow (row )
18
22
19
23
20
24
def read_file ():
25
+ # opening the file in read mode and reading lines
21
26
with open ("sentmails.txt" , 'r' ) as f :
22
27
sent_mails = f .readlines ()
23
28
return sent_mails
24
29
25
30
26
31
def write_file (mail ):
32
+ # opening the file in append mode and appending the mail at the end
27
33
with open ("sentmails.txt" , 'a' ) as f :
28
34
f .write (mail + "\n " )
Original file line number Diff line number Diff line change 3
3
4
4
5
5
def _render_template (name , payment_status ):
6
+ # loading the templates folder
6
7
file_loader = FileSystemLoader ('templates' )
7
8
env = Environment (
8
9
loader = file_loader ,
9
10
trim_blocks = True ,
10
11
lstrip_blocks = True ,
11
12
keep_trailing_newline = True ,
12
13
)
14
+ # fetching the template from the FileSystemLoader
13
15
template = env .get_template ('message.txt' )
16
+ # rendering the template
14
17
return template .render (name = name , payment_status = payment_status )
15
18
16
19
17
20
def preprocess (participants ):
21
+ # converting the list of lists to list of dict
18
22
data = []
19
23
for participant in participants :
20
24
data .append ({
Original file line number Diff line number Diff line change 3
3
4
4
5
5
def scraper (url ):
6
+ # sending a get http request to the url
6
7
response = requests .get (url )
8
+ # response: html page
7
9
html = response .text
8
10
data = []
9
11
12
+ # converting the html string to soup object
10
13
soup = bs4 .BeautifulSoup (html )
11
14
li_tags = soup .select ('li' )
12
15
13
16
for i in range (len (li_tags )):
17
+ # selecting all the necessary classes from the li tags
18
+ # Note: .select returns a list of elements
14
19
name = li_tags [i ].select ('.name' )[0 ].text
15
20
dob = li_tags [i ].select ('.dob' )[0 ].text
16
21
email = li_tags [i ].select ('.email' )[0 ].text
You can’t perform that action at this time.
0 commit comments