-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailrenderer.py
85 lines (69 loc) · 2.75 KB
/
mailrenderer.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
import requests
from PIL import Image
import io
import os
from getnews.getDetail import getDetail
from getnews.getContents import getContents
from bs4 import BeautifulSoup
from headline import getHeadline
from mailsender.gmailsend import sendNewsLetter
## parameters:
# url: path to email
# resize : path to resize - tuple (400,400)
IMG_IDX = 1
def generatePath(imgdir=""):
global IMG_IDX
path = "{}.png".format(IMG_IDX)
IMG_IDX += 1
return os.path.join(imgdir, path), path
def imageRenderer(url, resize, imgdir=""):
savepath, path = generatePath(imgdir)
if not os.path.exists(os.path.join(imgdir, "no.png")):
nopng = Image.open("no.png")
nopng.thumbnail(resize)
nopng.save(os.path.join(imgdir, "no.png"))
if url == "":
return "no.png"
try:
img_data = requests.get(url).content
image = Image.open(io.BytesIO(img_data))
image.thumbnail(resize)
image.save(savepath)
except Exception as e:
return "no.png"
return path
def makeEmailContent(items, templateDirPath, returndir="content"):
headline, now = getHeadline()
mainContent = ""
dataTemplate = ""
dataContent = ""
MAX_CHAR = 150
if not os.path.exists(returndir):
os.makedirs(returndir)
with open(os.path.join(templateDirPath, "main.html"), "r") as f:
mainContent = f.read()
with open(os.path.join(templateDirPath, "data.html"), "r") as f:
dataTemplate = f.read()
soup = BeautifulSoup(dataTemplate, "html.parser")
for item in items:
link = item["link"].strip()
title = item["title"].strip()
detail = getDetail(link)
imgurl = detail["imgPath"]
imgWidth = int(soup.find("img")["width"])
imgHeight = int(soup.find("img")["height"])
imgPath = imageRenderer(imgurl, (imgWidth, imgHeight), returndir)
description = detail["description"]
if len(description) > MAX_CHAR:
description = description[:MAX_CHAR] + " ..."
dataContent += dataTemplate.format(link=link, title=title, imgPath=imgPath, description=description)
with open(os.path.join(returndir, "main.html"), "w", encoding="utf-8") as f:
f.write(mainContent.format(title=headline, dataContent=dataContent).replace("{{", "{").replace("}}", "}"))
return True
def renderMailNews(items, to_mail):
contentDir = "content"
makeEmailContent(items, "mailtemplate", contentDir)
# sendNewsLetter(from_mail='[email protected]',to_mail='[email protected]',subject='메일 테스트',contentDirPath=contentDir)
if __name__ == "__main__":
items = getContents()
makeEmailContent(items, "mailtemplate", "content")