-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Expand file tree
/
Copy pathconstruct_email.py
More file actions
137 lines (124 loc) · 4.19 KB
/
construct_email.py
File metadata and controls
137 lines (124 loc) · 4.19 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
from .protocol import Paper
import math
framework = """
<!DOCTYPE HTML>
<html>
<head>
<style>
.star-wrapper {
font-size: 1.3em; /* 调整星星大小 */
line-height: 1; /* 确保垂直对齐 */
display: inline-flex;
align-items: center; /* 保持对齐 */
}
.half-star {
display: inline-block;
width: 0.5em; /* 半颗星的宽度 */
overflow: hidden;
white-space: nowrap;
vertical-align: middle;
}
.full-star {
vertical-align: middle;
}
</style>
</head>
<body>
<div>
__CONTENT__
</div>
<br><br>
<div>
To unsubscribe, remove your email in your Github Action setting.
</div>
</body>
</html>
"""
def get_empty_html():
block_template = """
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="font-family: Arial, sans-serif; border: 1px solid #ddd; border-radius: 8px; padding: 16px; background-color: #f9f9f9;">
<tr>
<td style="font-size: 20px; font-weight: bold; color: #333;">
No Papers Today. Take a Rest!
</td>
</tr>
</table>
"""
return block_template
def get_block_html(title:str, authors:str, rate:str, tldr:str, pdf_url:str, affiliations:str=None,published_date: str = None):
block_template = """
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="font-family: Arial, sans-serif; border: 1px solid #ddd; border-radius: 8px; padding: 16px; background-color: #f9f9f9;">
<tr>
<td style="font-size: 20px; font-weight: bold; color: #333;">
{title}
</td>
</tr>
<tr>
<td style="font-size: 14px; color: #666; padding: 8px 0;">
{authors}
<br>
<i>{affiliations}</i>
</td>
</tr>
<tr>
<td style="font-size: 14px; color: #333; padding: 8px 0;">
<strong>Date:</strong> {published_date}
</td>
</tr>
<tr>
<td style="font-size: 14px; color: #333; padding: 8px 0;">
<strong>Relevance:</strong> {rate}
</td>
</tr>
<tr>
<td style="font-size: 14px; color: #333; padding: 8px 0;">
<strong>TLDR:</strong> {tldr}
</td>
</tr>
<tr>
<td style="padding: 8px 0;">
<a href="{pdf_url}" style="display: inline-block; text-decoration: none; font-size: 14px; font-weight: bold; color: #fff; background-color: #d9534f; padding: 8px 16px; border-radius: 4px;">PDF</a>
</td>
</tr>
</table>
"""
return block_template.format(title=title, authors=authors,rate=rate, tldr=tldr, pdf_url=pdf_url, affiliations=affiliations,published_date=published_date or "Unknown")
def get_stars(score:float):
full_star = '<span class="full-star">⭐</span>'
half_star = '<span class="half-star">⭐</span>'
low = 6
high = 8
if score <= low:
return ''
elif score >= high:
return full_star * 5
else:
interval = (high-low) / 10
star_num = math.ceil((score-low) / interval)
full_star_num = int(star_num/2)
half_star_num = star_num - full_star_num * 2
return '<div class="star-wrapper">'+full_star * full_star_num + half_star * half_star_num + '</div>'
def render_email(papers:list[Paper]) -> str:
parts = []
if len(papers) == 0 :
return framework.replace('__CONTENT__', get_empty_html())
for p in papers:
#rate = get_stars(p.score)
rate = round(p.score, 1) if p.score is not None else 'Unknown'
author_list = [a for a in p.authors]
num_authors = len(author_list)
if num_authors <= 5:
authors = ', '.join(author_list)
else:
authors = ', '.join(author_list[:3] + ['...'] + author_list[-2:])
if p.affiliations is not None:
affiliations = p.affiliations[:5]
affiliations = ', '.join(affiliations)
if len(p.affiliations) > 5:
affiliations += ', ...'
else:
affiliations = 'Unknown Affiliation'
published_date = p.published_date or "Unknown"
parts.append(get_block_html(p.title, authors, rate, p.tldr, p.pdf_url, affiliations,published_date,))
content = '<br>' + '</br><br>'.join(parts) + '</br>'
return framework.replace('__CONTENT__', content)