def download_by_link(self, link): content = urllib2.urlopen(link).read() # html, link link_on_file, filename = self.get_filename(content) # html fullname = self.get_dir_name() + filename if self.is_new_version(fullname): with open(fullname, 'w') as fd: content = urllib2.urlopen(link_on_file).read() # fd.write(content) return self.get_prepared_files(fullname)
def get_filename(self, text): pattern = r'(?P<link>http:\/\/archive\.sport\-express\.ru\/pdf\/(?P<filename>[0-9]+\.zip))' match = re.search(pattern, text) return match.group('link'), match.group('filename')
class SportExpress(): def get_from(self): return 'username@mail.ru' def get_title(self): return 'Sport Express subscribe' def get_text(self): return "Good morning! Don't be lazy - read newspaper!" def get_prepared_files(self, archive): directory_to_extract = archive[:archive.rfind('.')] zipfile.ZipFile(archive).extractall(directory_to_extract) # res = glob.glob(directory_to_extract+'/*.*') return res def get_dir_name(self): dir_name = 'archive/sport-express/' if not os.path.exists(dir_name): os.mkdir(dir_name) return dir_name def is_new_version(self, filename): return os.path.exists(self.get_dir_name() + filename)
import email, email. encoders, email.mime.text, email.mime.base class MessageBase: def __init__(self, subscriber): self.__to_addresses = subscriber.get_subscribers() self.__from_address = subscriber.get_from() self.__message = self.__create_message(subscriber.get_title(), subscriber.get_text())
def __create_message(self, subject, text): email_msg = email.MIMEMultipart.MIMEMultipart('mixed') email_msg['Subject'] = subject email_msg['From'] = self.get_from() email_msg['To'] = ', '.join(self.get_to()) email_msg.attach(email.mime.text.MIMEText(text,'text')) return email_msg
def attach_file(self, filename, file_link, file_type = 'unknown'): file_message = email.mime.base.MIMEBase('application', file_type) file_message.set_payload(file(file_link).read()) email.encoders.encode_base64(file_message) file_message.add_header('Content-Disposition','attachment;filename='+filename) self.__message.attach(file_message) return True
def get_text(self): return self.__message.as_string()
import smtplib from message import MessageBase class SmtpBase: def __init__(self, serverhost): self.__smtp_server = serverhost def open(self): self.__server = smtplib.SMTP(self.__smtp_server) self.__server.login('<enter your smtp login>', '<enter your smtp password>') def close(self): self.__server.quit()
def send_mail(self, message): for message_to in message.get_to(): self.__server.sendmail(message.get_from(), message_to, message.get_text())
def __enter__(self): self.open() return self def __exit__(self, type, value, traceback): if type: print '%s: %s %s' % (type, value, traceback) self.close()
from subscriber_sex import SportExpress from smtp import SmtpBase from message import MessageBase b = SportExpress() filenames = b.download_by_link('http://www.sport-express.ru') msg = MessageBase(b) for file in filenames: msg.attach_file(file[file.rfind('/')+1:], file, 'pdf') with SmtpBase('smtp.yandex.ru') as s: s.send_mail(msg)
Source: https://habr.com/ru/post/150940/