-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
376 lines (332 loc) · 11.1 KB
/
Copy pathmain.py
File metadata and controls
376 lines (332 loc) · 11.1 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import feedparser
from datetime import datetime, timedelta
import locale
from dateutil import parser
# Türkçe tarih biçimi için locale ayarını yapalım (gerekirse)
try:
locale.setlocale(locale.LC_TIME, "tr_TR.UTF-8")
except locale.Error:
try:
# Windows sistemlerde farklı kodlama gerekebilir
locale.setlocale(locale.LC_TIME, "turkish")
except locale.Error:
print(
"Türkçe locale ayarı yapılamadı. Tarihler İngilizce görüntülenebilir."
)
# Türkçe ay isimleri
TURKCE_AYLAR = {
"January": "Ocak",
"February": "Şubat",
"March": "Mart",
"April": "Nisan",
"May": "Mayıs",
"June": "Haziran",
"July": "Temmuz",
"August": "Ağustos",
"September": "Eylül",
"October": "Ekim",
"November": "Kasım",
"December": "Aralık",
}
def turkce_tarih_formatla(tarih):
"""İngilizce tarih stringini Türkçe'ye çevirir"""
if not tarih:
return "Tarih bilgisi bulunmuyor."
try:
ingilizce_tarih = tarih.strftime("%d %B %Y %H:%M")
# Ayı Türkçe'ye çevirme
for ing_ay, tr_ay in TURKCE_AYLAR.items():
ingilizce_tarih = ingilizce_tarih.replace(ing_ay, tr_ay)
return ingilizce_tarih
except:
return tarih # Hata durumunda orijinali döndür
def haftalik_bulten_olustur(rss_url):
"""
Verilen RSS URL'sinden son haftanın gönderilerini çekerek haftalık bülten oluşturur.
Args:
rss_url (str): RSS feed'inin URL'si.
Returns:
str: Haftalık bültenin metin formatındaki içeriği.
"""
feed = feedparser.parse(rss_url)
if feed.bozo == 1:
return (
f"RSS feed'i ayrıştırılırken bir hata oluştu: {feed.bozo_exception}"
)
# Saat dilimi bilgisi olmayan (naive) bir datetime nesnesi oluştur
hafta_once = datetime.now().replace(tzinfo=None) - timedelta(
days=30
) # 7 günden 30 güne değiştirildi
# HTML formatında içerik oluşturmaya başlıyoruz
bulten_icerigi = """<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Aylık Bülten</title>
<style>
body {
background: #eee;
font-family: Arial, sans-serif;
font-size: 16px;
color: #111;
}
header {
border-bottom: 1px solid #ddd;
padding-bottom: 30px;
margin-bottom: 30px;
text-align: center;
}
.container {
background: #fff;
width: 600px;
margin: 0 auto;
padding: 30px;
}
.content {
padding: 10px 0;
}
.post {
margin-bottom: 30px;
padding-bottom: 20px;
border-bottom: 1px solid #eee;
}
.post h2 {
margin-top: 0;
color: #333;
}
.post-meta {
color: #777;
font-size: 14px;
margin-bottom: 10px;
}
.post-link {
margin-bottom: 10px;
}
.post-summary {
line-height: 1.6;
}
.btn {
display: inline-block;
background: #4a6fa5;
color: white;
padding: 10px 20px;
text-decoration: none;
border-radius: 5px;
font-weight: bold;
margin-top: 10px;
}
.btn:hover {
background: #245091;
}
.subscriber-details {
margin-top: 30px;
padding: 15px;
background: #f9f9f9;
border-radius: 5px;
}
footer {
text-align: center;
margin-top: 30px;
padding-top: 20px;
border-top: 1px solid #ddd;
font-size: 14px;
color: #777;
}
</style>
</head>
<body>
<section class="container">
<header>
<h1>Aylık Bülten</h1>
<p>{{ Date "02 January 2006" }} tarihli bülten</p>
<!-- Listmonk şablonunda burada abonelerin ismi görünebilir -->
<p>Merhaba {{ .Subscriber.FirstName }}!</p>
</header>
<div class="content">
{{ template "content" . }}
"""
son_haftanin_gonderileri = []
for entry in feed.entries:
tarih = None
if hasattr(entry, "published"):
tarih = parser.parse(entry.published)
elif hasattr(entry, "updated"):
tarih = parser.parse(entry.updated)
elif hasattr(entry, "date"):
tarih = parser.parse(entry.date)
# Saat dilimi bilgisini kaldırarak karşılaştırma yapabilir hale getir
if tarih:
tarih = tarih.replace(tzinfo=None)
if tarih and tarih > hafta_once:
son_haftanin_gonderileri.append(entry)
if not son_haftanin_gonderileri:
return "Son bir ay içinde yeni gönderi bulunamadı."
def tarihe_gore_sirala(gonderi):
tarih_str = getattr(
gonderi,
"published",
getattr(
gonderi,
"updated",
getattr(gonderi, "date", datetime.min.isoformat()),
),
)
try:
tarih = parser.parse(tarih_str)
if tarih.tzinfo: # Timezone bilgisi varsa kaldır
tarih = tarih.replace(tzinfo=None)
return tarih
except:
return datetime.min # Hata durumunda en eski tarih
# İçerik kısmını oluşturuyoruz
content = ""
for gonderi in sorted(
son_haftanin_gonderileri, key=tarihe_gore_sirala, reverse=True
):
baslik = gonderi.title
link = gonderi.link
ozet = getattr(gonderi, "summary", "Özet bulunmuyor.")
tarih_bilgisi = None
if hasattr(gonderi, "published"):
tarih = parser.parse(gonderi.published)
tarih_bilgisi = turkce_tarih_formatla(tarih)
elif hasattr(gonderi, "updated"):
tarih = parser.parse(gonderi.updated)
tarih_bilgisi = turkce_tarih_formatla(tarih)
elif hasattr(gonderi, "date"):
tarih = parser.parse(gonderi.date)
tarih_bilgisi = turkce_tarih_formatla(tarih)
else:
tarih_bilgisi = "Tarih bilgisi bulunmuyor."
content += f"""
<div class="post">
<h2>{baslik}</h2>
<div class="post-meta">Yayın Tarihi: {tarih_bilgisi}</div>
<div class="post-link">Link: <a href="{{ TrackLink \"{link}\" }}">{link}</a></div>
<div class="post-summary">{ozet}</div>
</div>
"""
# Şablonu tamamlayalım
bulten_icerigi_sonu = """
</div>
<footer>
<p>Bu bülten <a href="https://yuceltoluyag.dev">Yücel Toluyağ</a> tarafından gönderilmiştir.</p>
<p>Aboneliğinizi yönetmek için <a href="{{ UnsubscribeURL }}">buraya tıklayın</a>.</p>
{{ TrackView }}
</footer>
</section>
</body>
</html>
"""
# "content" bölümü için Listmonk şablonu
content_template = f"""
<!-- Bülten içeriği başlangıcı -->
{content}
<!-- Bülten içeriği sonu -->
"""
# Tam şablonu döndür
full_template = bulten_icerigi + bulten_icerigi_sonu
return full_template
if __name__ == "__main__":
rss_url = "https://yuceltoluyag.dev/feeds/all.atom.xml" # Kendi RSS URL'nizi buraya yapıştırın
haftalik_bulten = haftalik_bulten_olustur(rss_url)
# Listmonk şablonu içerik bölümü için ayrı bir dosya
content_template = """
<!-- Bülten içeriği buraya gelecek -->
{% for gonderi in gonderi_listesi %}
<div class="post">
<h2>{{ gonderi.baslik }}</h2>
<div class="post-meta">Yayın Tarihi: {{ gonderi.tarih }}</div>
<div class="post-link">Link: <a href="{{ TrackLink \"{{ gonderi.link }}\" }}">{{ gonderi.link }}</a></div>
<div class="post-summary">{{ gonderi.ozet }}</div>
</div>
{% endfor %}
"""
# Gerçek gönderilerle dolu içerik şablonu oluşturma
real_content = ""
son_haftanin_gonderileri = []
feed = feedparser.parse(rss_url)
hafta_once = datetime.now().replace(tzinfo=None) - timedelta(days=30)
# Sıralama fonksiyonu
def tarihe_gore_sirala(gonderi):
tarih_str = getattr(
gonderi,
"published",
getattr(
gonderi,
"updated",
getattr(gonderi, "date", datetime.min.isoformat()),
),
)
try:
tarih = parser.parse(tarih_str)
if tarih.tzinfo: # Timezone bilgisi varsa kaldır
tarih = tarih.replace(tzinfo=None)
return tarih
except:
return datetime.min # Hata durumunda en eski tarih
# Gönderileri topla
for entry in feed.entries:
tarih = None
if hasattr(entry, "published"):
tarih = parser.parse(entry.published)
elif hasattr(entry, "updated"):
tarih = parser.parse(entry.updated)
elif hasattr(entry, "date"):
tarih = parser.parse(entry.date)
if tarih:
tarih = tarih.replace(tzinfo=None)
if tarih and tarih > hafta_once:
son_haftanin_gonderileri.append(entry)
# İçerik oluştur
for gonderi in sorted(
son_haftanin_gonderileri, key=tarihe_gore_sirala, reverse=True
):
baslik = gonderi.title
link = gonderi.link
ozet = getattr(gonderi, "summary", "Özet bulunmuyor.")
tarih_bilgisi = None
if hasattr(gonderi, "published"):
tarih = parser.parse(gonderi.published)
tarih_bilgisi = turkce_tarih_formatla(tarih)
elif hasattr(gonderi, "updated"):
tarih = parser.parse(gonderi.updated)
tarih_bilgisi = turkce_tarih_formatla(tarih)
elif hasattr(gonderi, "date"):
tarih = parser.parse(gonderi.date)
tarih_bilgisi = turkce_tarih_formatla(tarih)
else:
tarih_bilgisi = "Tarih bilgisi bulunmuyor."
real_content += f"""
<div class="post">
<h2>{baslik}</h2>
<div class="post-meta">Yayın Tarihi: {tarih_bilgisi}</div>
<div class="post-summary">{ozet}</div>
<a href="{link}@TrackLink" class="btn">Devamını Oku</a>
</div>
"""
# Abone bilgileri bölümü ekleyelim
real_content += """
<!-- Abone bilgileri örneği - Listmonk ile çalışacak -->
<div class="subscriber-details">
<h3>Sevgili {{ .Subscriber.FirstName }} {{ .Subscriber.LastName }},</h3>
<p>Bu bülteni {{ .Subscriber.Email }} adresine gönderiyoruz.</p>
<p>{{ Date "02 Ocak 2006" }} tarihinde güncellenmiştir.</p>
<p><a href="{{ MessageURL }}" style="color:#4a6fa5;">Tarayıcıda görüntüle</a> |
<a href="{{ UnsubscribeURL }}" style="color:#4a6fa5;">Aboneliği yönet</a></p>
</div>
"""
# İçerik dosyasını da kaydet
with open("bulten_content.html", "w", encoding="utf-8") as dosya:
dosya.write(real_content)
print("Gerçek içerik 'bulten_content.html' dosyasına kaydedildi.")
# HTML formatında bülteni kaydet
with open("aylik_bulten.html", "w", encoding="utf-8") as dosya:
dosya.write(haftalik_bulten)
print(
"\nAylık bülten 'aylik_bulten.html' dosyasına Listmonk uyumlu şablon olarak kaydedildi."
)
# İsterseniz Listmonk'a yükleyebileceğiniz içerik şablonunu da kaydetmek için:
with open("bulten_template.html", "w", encoding="utf-8") as dosya:
dosya.write(content_template)
print("Bülten içerik şablonu 'bulten_template.html' dosyasına kaydedildi.")