Skip to content

Commit df7d1f4

Browse files
committed
Restructuring
2 parents 365a7c8 + eb421d8 commit df7d1f4

File tree

9 files changed

+117
-362
lines changed

9 files changed

+117
-362
lines changed

automate.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
from scrape import scraper
2-
from csv_utils import read_csv, write_csv, get_unpaid_participants
3-
from generate import _render_template
2+
from file_utils import read_csv, write_csv, read_file, write_file
3+
from generate import _render_template, preprocess
44
from mail import sendmail
55
import json
66

7-
data = scraper('http://scrape.surge.sh/')
8-
write_csv(data, "studentdetails.csv")
7+
data = scraper('http://automatescrape.surge.sh/')
8+
write_csv(data)
99

10-
unpaid_participants, paid_count = get_unpaid_participants("studentdetails.csv")
11-
total_seats = 500
10+
participants = read_csv()
11+
participants = preprocess(participants)
1212

13-
for participant in unpaid_participants:
14-
html = _render_template(participant[0], total_seats-paid_count)
15-
sendmail(to_email=participant[0], html=html)
13+
sent_mails = read_file()
14+
mails = []
15+
16+
for participant in participants:
17+
if participant['email'] not in sent_mails:
18+
msg = _render_template(participant['name'], participant['payment'])
19+
response = sendmail(to_email=participant['email'], msg=msg)
20+
if response['email_status'] == "Success":
21+
mails.append(participant['email'])
22+
write_file(participant['email'])

file_utils.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import csv
2+
3+
4+
def read_csv():
5+
with open("studentdetails.csv", 'r') as csv_file:
6+
csv_reader = csv.reader(csv_file, delimiter=',')
7+
data = []
8+
for row in csv_reader:
9+
data.append(row)
10+
return data
11+
12+
13+
def write_csv(data):
14+
with open("studentdetails.csv", 'w') as csv_file:
15+
csv_writer = csv.writer(csv_file, delimiter=',')
16+
for row in data:
17+
csv_writer.writerow(row)
18+
19+
20+
def read_file():
21+
with open("sentmails.txt", 'r') as f:
22+
sent_mails = f.readlines()
23+
return sent_mails
24+
25+
26+
def write_file(mail):
27+
with open("sentmails.txt", 'a') as f:
28+
f.write(mail + "\n")

generate.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,27 @@
22
import csv
33

44

5-
def _render_template(name, seats):
5+
def _render_template(name, payment_status):
66
file_loader = FileSystemLoader('templates')
77
env = Environment(
88
loader=file_loader,
99
trim_blocks=True,
1010
lstrip_blocks=True,
1111
keep_trailing_newline=True,
1212
)
13-
template = env.get_template('remind.html')
14-
return template.render(name=name, seats=seats)
13+
template = env.get_template('message.txt')
14+
return template.render(name=name, payment_status=payment_status)
15+
16+
17+
def preprocess(participants):
18+
data = []
19+
for participant in participants:
20+
data.append({
21+
"name": participant[0],
22+
"dob": participant[1],
23+
"email": participant[2],
24+
"city": participant[3],
25+
"phone": participant[4],
26+
"payment": participant[5],
27+
})
28+
return data

mail.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@
55
import os
66
import sys
77

8-
def sendmail(to_email, html):
8+
def sendmail(to_email, msg):
99

1010
from_email = ''
1111
msg = MIMEMultipart('alternative')
1212
msg['Subject'] = "Python Automation Workstop"
1313
msg['From'] = from_email
1414
msg['To'] = to_email
1515

16-
body = html
17-
content = MIMEText(body, 'html')
16+
content = msg
1817
msg.attach(content)
1918
response = {}
2019
try:

scrape.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
import bs4
22
import requests
33

4+
45
def scraper(url):
56
response = requests.get(url)
67
html = response.text
78
data = []
89

910
soup = bs4.BeautifulSoup(html)
1011
li_tags = soup.select('li')
11-
12+
1213
for i in range(len(li_tags)):
13-
name = li_tags[i].select('#name_'+str(i+1))[0].text
14-
dob = li_tags[i].select('#dob_'+str(i+1))[0].text
15-
city = li_tags[i].select('#city_'+str(i+1))[0].text
16-
email = li_tags[i].select('#email_'+str(i+1))[0].text
17-
phone = li_tags[i].select('#phone_'+str(i+1))[0].text
18-
payment = li_tags[i].select('#payment_'+str(i+1))[0].text
19-
data.append([name, dob, city, email, phone, payment])
20-
14+
name = li_tags[i].select('.name')[0].text
15+
dob = li_tags[i].select('.dob')[0].text
16+
email = li_tags[i].select('.email')[0].text
17+
if li_tags[i].select('.city'):
18+
city = li_tags[i].select('.city')[0].text
19+
else:
20+
city = ""
21+
if li_tags[i].select('.phone'):
22+
phone = li_tags[i].select('.phone')[0].text
23+
else:
24+
phone = ""
25+
payment = li_tags[i].select('.payment')[0].text
26+
data.append([name, dob, email, city, phone, payment])
27+
2128
return data

sentmails.txt

Lines changed: 9 additions & 0 deletions

studentdetails.csv

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
1-
Name,DOB,City,Email,Phone,Payment
2-
Akshay,09-10-1999,Mumbai,[email protected],9699923802,paid
3-
Karan,15-06-1999,Mumbai,[email protected],7715905009,unpaid
4-
Rushang,04-11-1999,Mumbai,[email protected],9619243034,paid
5-
Aditya,10-06-1999,Mumbai,[email protected],7977475158,unpaid
6-
Hetal,24-06-1999,Mumbai,[email protected],9819746588,paid
1+
Name,DOB,Email,City,Phone,Payment
2+
Keith Morrow,1982-07-08,[email protected],,,unpaid
3+
Bonnie Fisher,2009-02-09,[email protected],Garciaborough,001-320-174-1751,unpaid
4+
Michelle Holmes,1999-08-21,[email protected],East Troyton,+1-322-770-8979x66613,unpaid
5+
Thomas Miller,1998-03-19,[email protected],Port Mary,,unpaid
6+
Amy Hebert,2006-10-30,[email protected],,,unpaid
7+
Destiny Anderson,2011-12-01,[email protected],Roberttown,862.685.7973,unpaid
8+
Sarah Woods,2010-10-03,[email protected],,,unpaid
9+
Robert Christensen,1993-08-28,[email protected],,(849)051-7160x16652,unpaid
10+
Bethany Frye,2010-10-16,[email protected],,,unpaid
11+
Olivia Fisher,1999-08-04,[email protected],,+1-774-119-6430x21642,unpaid
12+
Darren Cole,2014-01-29,[email protected],Lisaport,854.462.3147,paid
13+
Steven Ramirez,1976-02-01,[email protected],Foleymouth,(175)534-2434x428,paid
14+
Austin Gross,2006-12-06,[email protected],South Adamberg,001-411-678-0856x51513,unpaid
15+
Charles Myers,1997-10-05,[email protected],,(456)053-5875x607,unpaid
16+
Lonnie Green,2000-01-19,[email protected],,,paid
17+
Chloe Blevins,1982-02-15,[email protected],Lake Tammy,(552)363-2280,paid
18+
Joel Smith,2015-11-19,[email protected],New Anthonyhaven,,unpaid
19+
Stephanie Craig,2011-10-26,[email protected],,+1-263-404-9039,unpaid
20+
Christopher Potts,2011-07-06,[email protected],Jonesbury,,unpaid
21+
Mr. Daniel Williams II,2012-10-07,[email protected],Jaychester,,unpaid
22+
Brenda Hampton,1986-01-04,[email protected],North Matthewport,,unpaid

templates/message.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Hi {{name}},
2+
{% if payment == 'paid' %}
3+
This mail is to confirm your payment for the event. Attached with this mail is the pass which will be needed as proof for confirmation.
4+
{% else %}
5+
We noticed you have registered for our event. But, we haven't recieved any payments. Please make sure to confirm your presence by paying a refundable deposit.
6+
{% endif %}
7+
Thanks

0 commit comments

Comments
 (0)