forked from bhanu-lab/PythonScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewsPaperSender.py
More file actions
142 lines (121 loc) · 4.93 KB
/
NewsPaperSender.py
File metadata and controls
142 lines (121 loc) · 4.93 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
import requests
#import html5lib
from bs4 import BeautifulSoup
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from string import Template
from email.mime.base import MIMEBase
from email import encoders
import datetime
import os.path as op
import os
import sys
'''
Author: R@jesh
A simple python script to download a News Paper and to send as email attachment to users dialy.
***requirements***
need to install required packages(requests, BeautifulSoup, html5lib) for this script
'''
#URL which contain GDrive link of Hindhu Paper
URL = "https://www.bitul.in/epaper/the-hindu/"
r = requests.get(URL)
soup = BeautifulSoup(r.content, 'html5lib')
f=0
#Checking all the hyperlinks in the site until drive link
for link in soup.find_all('a'):
drivelink = link.get('href')
if "http://www.newspapertoday.xyz" in str(drivelink):
print(drivelink)
f=1
break;
elif "drive.google.com" in str(drivelink):
print(drivelink)
break;
#Now AdFree Hindhu News Paper Link is on drivelink
if f==0:
Id = drivelink[32:65]
file_url = "https://drive.google.com/uc?authuser=0&id="+Id+"&export=download"
else:
file_url = drivelink
r = requests.get(file_url, stream = True)
with open("C:/Users/Rajesh.Gundupalli/Desktop/PythonScripts/resources/NewsPaper/NewsPaper.pdf","wb") as pdf:
for chunk in r.iter_content(chunk_size=1024):
# writing one chunk at a time to pdf file
if chunk:
pdf.write(chunk)
#File Will be Downloaded in the current folder
### ###
#================================================================================================#
### ###
# function for reading names and emails from text file
def get_contacts(file_name):
names = []
emails = []
with open(file_name, mode='r') as contacts_file:
for a_contact in contacts_file:
names.append(a_contact.split()[0])
emails.append(a_contact.split()[1])
return names, emails
# function for reading a message template
def get_message_template(file_name):
with open(file_name, mode='r') as template_file:
template_file_content = template_file.read()
return Template(template_file_content)
# function to get my account details to send mail
def get_my_account_info(file_name):
with open(file_name, mode='r') as my_details:
for my_det in my_details:
my_address = my_det.split()[0]
my_password = my_det.split()[1]
return my_address, my_password
# main function
def main():
my_address, my_password = get_my_account_info('C:/Users/Rajesh.Gundupalli/Desktop/PythonScripts/resources/from_mail.txt') # get my account details
#print(my_address)
#print(my_password)
names, emails = get_contacts('C:/Users/Rajesh.Gundupalli/Desktop/PythonScripts/resources/test_contacts.txt') # read contacts
message_template = get_message_template('C:/Users/Rajesh.Gundupalli/Desktop/PythonScripts/resources/email_template.txt') # read template
# using gmail smtp
server = smtplib.SMTP(host='smtp.gmail.com', port='587')
server.starttls()
# Log into server
server.login(my_address, my_password)
failed_rcpts = {}
for name, email in zip(names, emails):
msg = MIMEMultipart()
message = message_template.substitute(PERSON_NAME=name.title())
print(message)
msg['From'] = my_address
msg['To'] = email
msg['Subject'] = "Email Notification Test "+str(datetime.date.today())
msg.attach(MIMEText(message, 'plain'))
part = MIMEBase('application', "octet-stream")
# adding attachement to the mail message
#Todo - Take Command Line Argument to send files - may be path to files
path = 'C:/Users/Rajesh.Gundupalli/Desktop/PythonScripts/resources/NewsPaper/' #ToDo
files = os.listdir(path) #f.name for f in os.scandir(path) if f.is_file() #os.listdir(path)
# for fname in os.scandir(path):
# if(fname.is_file()):
# print fname
#Attaching all the files in the directory
for name in files:
print(name)
filePath = path + name
attachment = open(filePath, "rb")
part = MIMEBase('application', 'octet-stream')
# To change the payload into encoded form
part.set_payload((attachment).read())
# encode into base64
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % name)
msg.attach(part)
attachment.close()
#send the mail
failed_rcpts = server.sendmail(my_address, email, msg.as_string())
del msg
# failed receipt will be printed here
print(failed_rcpts)
server.quit() # stopping SMTP server
if __name__ == '__main__':
main()