forked from spikexty/zotero-arxiv-daily
-
Notifications
You must be signed in to change notification settings - Fork 0
/
construct_email.py
130 lines (119 loc) · 4.02 KB
/
construct_email.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
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
import arxiv
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>
"""
return block_template
def get_block_html(title:str, authors:str, rate:str,arxiv_id:str, abstract:str, pdf_url:str, code_url:str=None):
code = f'<a href="{code_url}" style="display: inline-block; text-decoration: none; font-size: 14px; font-weight: bold; color: #fff; background-color: #5bc0de; padding: 8px 16px; border-radius: 4px; margin-left: 8px;">Code</a>' if code_url else ''
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}
</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>arXiv ID:</strong> {arxiv_id}
</td>
</tr>
<tr>
<td style="font-size: 14px; color: #333; padding: 8px 0;">
<strong>TLDR:</strong> {abstract}
</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>
{code}
</td>
</tr>
</table>
"""
return block_template.format(title=title, authors=authors,rate=rate,arxiv_id=arxiv_id, abstract=abstract, pdf_url=pdf_url, code=code)
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[arxiv.Result]):
parts = []
if len(papers) == 0 :
return framework.replace('__CONTENT__', get_empty_html())
for p in papers:
# crop the abstract
'''
summary = p.summary
summary = summary[:min(600, len(summary))]
if len(summary) == 600:
summary += '...'
'''
rate = get_stars(p.score)
authors = [a.name for a in p.authors[:5]]
authors = ', '.join(authors)
if len(p.authors) > 5:
authors += ', ...'
parts.append(get_block_html(p.title, authors,rate,p.arxiv_id ,p.tldr, p.pdf_url, p.code_url))
content = '<br>' + '</br><br>'.join(parts) + '</br>'
return framework.replace('__CONTENT__', content)