-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_news.py
More file actions
1374 lines (1130 loc) · 52.5 KB
/
fetch_news.py
File metadata and controls
1374 lines (1130 loc) · 52.5 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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import feedparser
import datetime
import os
import sys
from pathlib import Path
from xml.dom import minidom
import xml.etree.ElementTree as ET
import requests
from bs4 import BeautifulSoup
import time
import re
from concurrent.futures import ThreadPoolExecutor
import concurrent.futures
import json
import hashlib
# srcディレクトリをPythonパスに追加
sys.path.append(os.path.join(os.path.dirname(__file__), 'src'))
# 設定のインポート
from config.archive_config import DEFAULT_SITE_CONFIG
# 取得するRSSフィードのリスト
FEEDS = {
"Tech Blog Weekly": "https://yamadashy.github.io/tech-blog-rss-feed/feeds/rss.xml",
"Zenn": "https://zenn.dev/feed",
"Qiita": "https://qiita.com/popular-items/feed",
"はてなブックマーク - IT(人気)": "http://b.hatena.ne.jp/hotentry/it.rss",
"はてなブックマーク - IT(新着)": "https://b.hatena.ne.jp/entrylist/it.rss",
"DevelopersIO": "https://dev.classmethod.jp/feed/",
"gihyo.jp": "https://gihyo.jp/dev/feed/rss2",
"Publickey": "https://www.publickey1.jp/atom.xml",
"CodeZine": "https://codezine.jp/rss/new/20/index.xml",
"InfoQ Japan": "https://feed.infoq.com/jp",
"connpass - イベント": "https://connpass.com/explore/ja.atom",
"TECH PLAY - イベント": "https://rss.techplay.jp/event/w3c-rss-format/rss.xml",
"O'Reilly Japan - 近刊": "https://www.oreilly.co.jp/catalog/soon.xml"
}
# 除外するドメインのリスト
EXCLUDED_DOMAINS = {
'anond.hatelabo.jp': 'hatena anonymous diary',
'togetter.com': 'togetter'
}
# 各フィードから取得する記事の件数
MAX_ENTRIES = 5
class ThumbnailCache:
"""サムネイルキャッシュを管理するクラス"""
def __init__(self, cache_file_path="thumbnail_cache.json"):
"""
キャッシュクラスの初期化
Args:
cache_file_path (str): キャッシュファイルのパス
"""
self.cache_file_path = cache_file_path
self.cache = self._load_cache()
def _load_cache(self):
"""キャッシュファイルから既存のデータを読み込む"""
try:
if os.path.exists(self.cache_file_path):
with open(self.cache_file_path, 'r', encoding='utf-8') as f:
return json.load(f)
except Exception as e:
print(f"Warning: Failed to load thumbnail cache: {e}")
return {}
def _save_cache(self):
"""キャッシュデータをファイルに保存する"""
try:
with open(self.cache_file_path, 'w', encoding='utf-8') as f:
json.dump(self.cache, f, ensure_ascii=False, indent=2)
except Exception as e:
print(f"Warning: Failed to save thumbnail cache: {e}")
def _get_url_hash(self, url):
"""URLのハッシュ値を生成してキャッシュキーとする"""
return hashlib.md5(url.encode('utf-8')).hexdigest()
def get(self, url):
"""
キャッシュからサムネイルURLを取得
Args:
url (str): 記事URL
Returns:
str or None: キャッシュされたサムネイルURL、またはNone
"""
url_hash = self._get_url_hash(url)
cache_entry = self.cache.get(url_hash)
if cache_entry:
# キャッシュエントリが7日以内なら有効とする
import datetime
cache_time = datetime.datetime.fromisoformat(cache_entry['timestamp'])
now = datetime.datetime.now()
if (now - cache_time).days < 7:
return cache_entry.get('thumbnail_url')
return None
def set(self, url, thumbnail_url):
"""
サムネイルURLをキャッシュに保存
Args:
url (str): 記事URL
thumbnail_url (str or None): サムネイルURL
"""
url_hash = self._get_url_hash(url)
import datetime
self.cache[url_hash] = {
'url': url,
'thumbnail_url': thumbnail_url,
'timestamp': datetime.datetime.now().isoformat()
}
def save(self):
"""キャッシュをファイルに保存"""
self._save_cache()
def cleanup_old_entries(self, days_threshold=30):
"""
古いキャッシュエントリを削除
Args:
days_threshold (int): 削除対象となる日数の閾値
"""
import datetime
now = datetime.datetime.now()
keys_to_remove = []
for key, entry in self.cache.items():
try:
cache_time = datetime.datetime.fromisoformat(entry['timestamp'])
if (now - cache_time).days > days_threshold:
keys_to_remove.append(key)
except Exception:
# 不正なエントリは削除対象に
keys_to_remove.append(key)
for key in keys_to_remove:
del self.cache[key]
if keys_to_remove:
print(f"Cleaned up {len(keys_to_remove)} old cache entries")
def filter_entries_by_domain(entries, domain, label):
filtered_entries = []
excluded_count = 0
for entry in entries:
if hasattr(entry, 'link') and domain in entry.link:
excluded_count += 1
continue
filtered_entries.append(entry)
if excluded_count > 0:
print(f"Excluded {excluded_count} {label} entries")
return filtered_entries
def extract_author_info(entry):
"""RSSエントリーから著者情報を抽出する"""
author = None
# 複数のフィールドから著者情報を取得を試行
if hasattr(entry, 'author') and entry.author:
author = entry.author.strip()
elif hasattr(entry, 'author_detail') and entry.author_detail and entry.author_detail.get('name'):
author = entry.author_detail['name'].strip()
elif hasattr(entry, 'authors') and entry.authors and len(entry.authors) > 0:
first_author = entry.authors[0]
if isinstance(first_author, dict) and first_author.get('name'):
author = first_author['name'].strip()
elif hasattr(first_author, 'name'):
author = first_author.name.strip()
# 著者情報があれば、長すぎる場合は短縮処理
if author:
# O'Reilly Japan等の長い著者情報の短縮処理
if len(author) > 50: # 50文字を超える場合
# "著者名 著 訳者名 訳" パターンの処理
if " 著" in author:
author = author.split(" 著")[0]
# カンマ区切りの複数著者の場合、最初の2名まで
elif "、" in author:
authors = author.split("、")
if len(authors) > 2:
author = "、".join(authors[:2]) + "他"
else:
author = "、".join(authors[:2])
# それでも長い場合は前半50文字+...
if len(author) > 50:
author = author[:47] + "..."
return author
def fetch_feed_entries(feed_url):
"""指定されたURLからRSSフィードのエントリーを取得する"""
try:
feed = feedparser.parse(feed_url)
# 各エントリに著者情報を追加
for entry in feed.entries:
entry.author_info = extract_author_info(entry)
return feed.entries
except Exception as e:
print(f"Error fetching feed from {feed_url}: {e}")
return []
def get_article_thumbnail(url, max_retries=2):
"""記事URLからサムネイル画像URLを取得する"""
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
def validate_image_url(img_url):
"""画像URLが有効かどうかチェック"""
if not img_url or len(img_url) > 2000: # URLが長すぎる場合は除外
return False
if not img_url.startswith(('http://', 'https://')):
return False
# 画像形式のチェック
if any(ext in img_url.lower() for ext in ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.svg']):
return True
# 動的生成画像のパターン(qiita、zennなど)
if any(domain in img_url for domain in ['qiita-user-contents.imgix.net', 'res.cloudinary.com', 'cdn.image.st-hatena.com']):
return True
return False
for attempt in range(max_retries):
try:
response = requests.get(url, headers=headers, timeout=10)
response.raise_for_status()
soup = BeautifulSoup(response.content, 'html.parser')
# Open Graph画像を優先的に取得
og_image = soup.find('meta', property='og:image')
if og_image and og_image.get('content'):
img_url = og_image['content']
if img_url.startswith('//'):
img_url = 'https:' + img_url
elif img_url.startswith('/'):
from urllib.parse import urljoin
img_url = urljoin(url, img_url)
if validate_image_url(img_url):
return img_url
# Twitter Card画像
twitter_image = soup.find('meta', attrs={'name': 'twitter:image'})
if twitter_image and twitter_image.get('content'):
img_url = twitter_image['content']
if img_url.startswith('//'):
img_url = 'https:' + img_url
elif img_url.startswith('/'):
from urllib.parse import urljoin
img_url = urljoin(url, img_url)
if validate_image_url(img_url):
return img_url
# 記事内最初の画像
article_img = soup.find('img')
if article_img and article_img.get('src'):
img_url = article_img['src']
if img_url.startswith('//'):
img_url = 'https:' + img_url
elif img_url.startswith('/'):
from urllib.parse import urljoin
img_url = urljoin(url, img_url)
if validate_image_url(img_url):
return img_url
except Exception as e:
print(f"Attempt {attempt + 1} failed for {url}: {e}")
if attempt < max_retries - 1:
time.sleep(1) # リトライ前に少し待機
continue
return None # 画像が見つからない場合
def deduplicate_events(entries, target_count=10):
"""イベント系エントリーの重複を除去(シリーズ番号違いを統合)し、目標件数を確保"""
if not entries:
return entries
# イベント名の基底部分を抽出するパターン
patterns = [
r'^(.+?)\s*#\d+.*$', # "朝活もくもく会 #19" -> "朝活もくもく会"
r'^(.+?)\s*第\d+回.*$', # "第5回勉強会" -> "勉強会"
r'^(.+?)\s*Vol\.\d+.*$', # "勉強会 Vol.3" -> "勉強会"
r'^(.+?)\s*\(\d+\).*$', # "勉強会(3)" -> "勉強会"
r'^(.+?)\s*-\s*\d+.*$', # "勉強会 - 5" -> "勉強会"
]
# エントリーを基底名でグループ化
event_groups = {}
for entry in entries:
title = entry.title.strip()
base_name = title
# パターンマッチングで基底名を抽出
for pattern in patterns:
match = re.match(pattern, title)
if match:
base_name = match.group(1).strip()
break
# 基底名でグループ化(最新のエントリーを優先)
if base_name not in event_groups:
event_groups[base_name] = entry
else:
# 既存エントリーより新しい場合は置き換え
existing_date = getattr(event_groups[base_name], 'published_parsed', None)
current_date = getattr(entry, 'published_parsed', None)
if current_date and existing_date:
if current_date > existing_date:
event_groups[base_name] = entry
elif current_date and not existing_date:
event_groups[base_name] = entry
# グループ化されたエントリーを返す(元の順序を保持)
deduplicated = []
seen_bases = set()
for entry in entries:
title = entry.title.strip()
base_name = title
for pattern in patterns:
match = re.match(pattern, title)
if match:
base_name = match.group(1).strip()
break
if base_name not in seen_bases:
deduplicated.append(event_groups[base_name])
seen_bases.add(base_name)
# 目標件数に達したら終了
if len(deduplicated) >= target_count:
break
return deduplicated
def deduplicate_urls_across_feeds(all_entries):
"""フィード間でのURL重複を除去し、補填を行う(PRIORITY_FEEDS順で処理)"""
seen_urls = set()
deduplicated_feeds = {}
dedup_stats = {"total_removed": 0, "by_feed": {}}
# PRIORITY_FEEDSの順序でフィードを処理
from src.config.archive_config import DEFAULT_SITE_CONFIG
priority_feeds = DEFAULT_SITE_CONFIG.PRIORITY_FEEDS
# PRIORITY_FEEDSに含まれるフィードから処理
processed_feeds = set()
for feed_name in priority_feeds:
if feed_name in all_entries:
processed_feeds.add(feed_name)
entries = all_entries[feed_name]
if not entries:
deduplicated_feeds[feed_name] = entries
continue
# イベントフィードかどうかで目標件数を決定
target_count = 10 if "イベント" in feed_name else 5
# URL重複除去
unique_entries = []
removed_count = 0
for entry in entries:
if hasattr(entry, 'link') and entry.link not in seen_urls:
seen_urls.add(entry.link)
unique_entries.append(entry)
# 目標件数に達したら終了
if len(unique_entries) >= target_count:
break
else:
removed_count += 1
if hasattr(entry, 'title') and hasattr(entry, 'link'):
print(f"重複除去: [{feed_name}] {entry.title}")
print(f" URL: {entry.link}")
# イベントフィードの場合はさらにイベント重複除去を適用
if "イベント" in feed_name:
unique_entries = deduplicate_events(unique_entries, target_count)
deduplicated_feeds[feed_name] = unique_entries
dedup_stats["by_feed"][feed_name] = removed_count
dedup_stats["total_removed"] += removed_count
# PRIORITY_FEEDSに含まれていないフィードを処理
for feed_name, entries in all_entries.items():
if feed_name not in processed_feeds:
if not entries:
deduplicated_feeds[feed_name] = entries
continue
# イベントフィードかどうかで目標件数を決定
target_count = 10 if "イベント" in feed_name else 5
# URL重複除去
unique_entries = []
removed_count = 0
for entry in entries:
if hasattr(entry, 'link') and entry.link not in seen_urls:
seen_urls.add(entry.link)
unique_entries.append(entry)
# 目標件数に達したら終了
if len(unique_entries) >= target_count:
break
else:
removed_count += 1
if hasattr(entry, 'title') and hasattr(entry, 'link'):
print(f"重複除去: [{feed_name}] {entry.title}")
print(f" URL: {entry.link}")
# イベントフィードの場合はさらにイベント重複除去を適用
if "イベント" in feed_name:
unique_entries = deduplicate_events(unique_entries, target_count)
deduplicated_feeds[feed_name] = unique_entries
dedup_stats["by_feed"][feed_name] = removed_count
dedup_stats["total_removed"] += removed_count
# 重複除去統計を出力
if dedup_stats["total_removed"] > 0:
print(f"URL重複除去統計: 合計{dedup_stats['total_removed']}件を除去")
for feed_name, removed_count in dedup_stats["by_feed"].items():
if removed_count > 0:
print(f" {feed_name}: {removed_count}件")
return deduplicated_feeds
def fetch_all_thumbnails(all_entries, max_workers=10, use_cache=True):
"""全フィードの全記事のサムネイルを並列取得(キャッシュ対応)"""
# 全記事のURLリストを作成
all_urls = []
for entries in all_entries.values():
all_urls.extend([entry.link for entry in entries])
print(f"Fetching thumbnails for {len(all_urls)} articles...")
# キャッシュの初期化
cache = ThumbnailCache() if use_cache else None
thumbnails = {}
urls_to_fetch = []
# キャッシュから取得できるものは先に処理
if cache:
cache_hits = 0
for url in all_urls:
cached_thumbnail = cache.get(url)
if cached_thumbnail is not None:
thumbnails[url] = cached_thumbnail
cache_hits += 1
else:
urls_to_fetch.append(url)
if cache_hits > 0:
print(f"Cache hits: {cache_hits}/{len(all_urls)} thumbnails")
else:
urls_to_fetch = all_urls
# キャッシュにないURLのみ並列取得
if urls_to_fetch:
print(f"Fetching {len(urls_to_fetch)} new thumbnails in parallel...")
with ThreadPoolExecutor(max_workers=max_workers) as executor:
# 未キャッシュのURLに対して並列でサムネイル取得を実行
future_to_url = {
executor.submit(get_article_thumbnail, url): url
for url in urls_to_fetch
}
completed = 0
total = len(urls_to_fetch)
# 完了した処理から順次結果を取得
for future in concurrent.futures.as_completed(future_to_url):
url = future_to_url[future]
completed += 1
try:
thumbnail_url = future.result(timeout=15)
thumbnails[url] = thumbnail_url
# キャッシュに保存
if cache:
cache.set(url, thumbnail_url)
print(f"Progress: {completed}/{total} new thumbnails fetched")
except Exception as e:
print(f"Error fetching thumbnail for {url}: {e}")
thumbnails[url] = None
# エラーの場合もキャッシュに保存(Noneとして)
if cache:
cache.set(url, None)
# キャッシュを保存
if cache:
cache.cleanup_old_entries() # 古いエントリを削除
cache.save()
print("Thumbnail cache updated")
return thumbnails
def generate_html(all_entries, date_str, thumbnails=None):
"""新しいテンプレートシステムを使用してHTMLコンテンツを生成する"""
from src.templates.template_manager import TemplateManager, ContentStructure
# テンプレートマネージャーの初期化
template_manager = TemplateManager()
content_structure = ContentStructure(template_manager)
# 記事カードのHTML生成
entries_html = ""
for feed_name, entries in all_entries.items():
entries_html += f" <h2>{feed_name}</h2>\n"
if not entries:
entries_html += " <p>記事を取得できませんでした。</p>\n"
else:
for entry in entries:
thumbnail_url = thumbnails.get(entry.link) if thumbnails else None
card_html = template_manager.render_card(entry, feed_name, thumbnail_url)
entries_html += card_html
# 記事総数を計算
total_entries = sum(len(entries) for entries in all_entries.values())
# 完全なHTMLページを構築
title = f"今日のテックニュース ({date_str})"
html_content = content_structure.build_html_page(
title=title,
date_str=date_str,
entries_html=entries_html,
total_entries=total_entries,
is_archive=False
)
return html_content
def generate_markdown(all_entries, date_str):
"""取得したエントリーからMarkdownコンテンツを生成する"""
markdown = f"# 今日のテックニュース ({date_str})\n\n"
markdown += f"""📚 [過去のニュースを見る](archives/index.md) | 🎨 [カード表示版を見る]({DEFAULT_SITE_CONFIG.site_url}) | 📡 [RSSフィードを購読]({DEFAULT_SITE_CONFIG.rss_url})
日本の主要な技術系メディアの最新人気エントリーをお届けします。
※毎日JST 7:00に自動更新
## 🎨 カード表示版もあります
GitHub Pages版では各記事がカード形式で見やすく表示されます:
{DEFAULT_SITE_CONFIG.site_url}
---
"""
for feed_name, entries in all_entries.items():
markdown += f"## {feed_name}\n\n"
if not entries:
markdown += "記事を取得できませんでした。\n"
else:
# エントリーはすでにURL重複除去済み
for entry in entries:
title = entry.title
link = entry.link
# シンプルなリンク形式で表示
markdown += f"- [{title}]({link})\n"
markdown += "\n\n---\n"
markdown += "## License\n\nThis project is licensed under the [MIT License](LICENSE).\n"
return markdown
def generate_archive_markdown(all_entries, date_str):
"""アーカイブ用のMarkdownコンテンツを生成する(相対パス修正版)"""
markdown = f"# 今日のテックニュース ({date_str})\n\n"
markdown += f"""📚 [過去のニュースを見る](../../daily_news.md) | 🎨 [カード表示版を見る]({DEFAULT_SITE_CONFIG.site_url}) | 📡 [RSSフィードを購読]({DEFAULT_SITE_CONFIG.rss_url})
日本の主要な技術系メディアの最新人気エントリーをお届けします。
## 🎨 カード表示版もあります
GitHub Pages版では各記事がカード形式で見やすく表示されます:
{DEFAULT_SITE_CONFIG.site_url}
---
"""
for feed_name, entries in all_entries.items():
markdown += f"## {feed_name}\n\n"
if not entries:
markdown += "記事を取得できませんでした。\n"
else:
# エントリーはすでにURL重複除去済み
for entry in entries:
title = entry.title
link = entry.link
# シンプルなリンク形式で表示
markdown += f"- [{title}]({link})\n"
markdown += "\n\n---\n"
markdown += "## License\n\nThis project is licensed under the [MIT License](LICENSE).\n"
return markdown
def generate_archive_html(all_entries, date_str, thumbnails=None):
"""新しいテンプレートシステムを使用してアーカイブHTMLを生成する"""
from src.templates.template_manager import TemplateManager, ContentStructure
# テンプレートマネージャーの初期化
template_manager = TemplateManager()
content_structure = ContentStructure(template_manager)
# 記事カードのHTML生成
entries_html = ""
for feed_name, entries in all_entries.items():
entries_html += f" <h2>{feed_name}</h2>\n"
if not entries:
entries_html += " <p>記事を取得できませんでした。</p>\n"
else:
for entry in entries:
thumbnail_url = thumbnails.get(entry.link) if thumbnails else None
card_html = template_manager.render_card(entry, feed_name, thumbnail_url)
entries_html += card_html
# アーカイブ用HTMLページを構築
title = f"今日のテックニュース ({date_str})"
html_content = content_structure.build_html_page(
title=title,
date_str=date_str,
entries_html=entries_html,
is_archive=True
)
return html_content
def save_to_archive(all_entries, date_obj, thumbnails=None):
"""日付別アーカイブファイルとして保存(MarkdownとHTML両方)"""
year = date_obj.year
month = f"{date_obj.month:02d}"
date_str = date_obj.isoformat()
# ディレクトリ作成
archive_dir = Path(f"archives/{year}/{month}")
archive_dir.mkdir(parents=True, exist_ok=True)
# Markdown版
md_content = generate_archive_markdown(all_entries, date_str)
archive_file = archive_dir / f"{date_str}.md"
if archive_file.exists():
print(f"Overwriting existing archive: {archive_file}")
else:
print(f"Creating new archive: {archive_file}")
with open(archive_file, "w", encoding="utf-8") as f:
f.write(md_content)
# HTML版
html_content = generate_archive_html(all_entries, date_str, thumbnails)
html_file = archive_dir / f"{date_str}.html"
with open(html_file, "w", encoding="utf-8") as f:
f.write(html_content)
print(f"Generated archive files: {archive_file} and {html_file}")
return archive_file
def update_monthly_index(year, month):
"""月別インデックスページを更新(MarkdownとHTML両方)"""
archive_dir = Path(f"archives/{year}/{month:02d}")
if not archive_dir.exists():
return
# その月のファイル一覧を取得
md_files = sorted([f for f in archive_dir.iterdir() if f.suffix == '.md' and f.name != 'index.md'])
# Markdown版
md_content = f"# {year}年{month}月のテックニュース\n\n"
md_content += f"{year}年{month}月に取得したテックニュースの一覧です。\n\n"
for md_file in reversed(md_files): # 新しい順
date_str = md_file.stem
md_content += f"- [{date_str}]({md_file.name})\n"
md_content += f"\n[← {year}年一覧に戻る](../index.md)\n"
with open(archive_dir / "index.md", "w", encoding="utf-8") as f:
f.write(md_content)
# HTML版
html_content = f"""<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{year}年{month}月のテックニュース</title>
<!-- OGP Tags -->
<meta property="og:title" content="{year}年{month}月のテックニュース">
<meta property="og:description" content="日本の主要な技術系メディアの最新人気エントリーを毎日お届けします。">
<meta property="og:type" content="website">
<meta property="og:url" content="{DEFAULT_SITE_CONFIG.site_url}">
<meta property="og:image" content="{DEFAULT_SITE_CONFIG.og_image_url}">
<meta property="og:site_name" content="今日のテックニュース">
<!-- Twitter Card Tags -->
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:creator" content="@unsoluble_sugar">
<meta name="twitter:title" content="{year}年{month}月のテックニュース">
<meta name="twitter:description" content="日本の主要な技術系メディアの最新人気エントリーを毎日お届けします。">
<meta name="twitter:image" content="{DEFAULT_SITE_CONFIG.og_image_url}">
<!-- Favicon Links -->
<link rel="apple-touch-icon" sizes="180x180" href="../../../assets/favicons/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="../../../assets/favicons/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="../../../assets/favicons/favicon-16x16.png">
<link rel="manifest" href="../../../assets/favicons/site.webmanifest">
<link rel="shortcut icon" href="../../../assets/favicons/favicon.ico">
<style>
body {{
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
line-height: 1.6;
color: #333;
}}
h1 {{
color: #1f2328;
}}
ul {{
list-style-type: disc;
padding-left: 2em;
}}
li {{
margin: 8px 0;
}}
a {{
color: #0969da;
text-decoration: none;
}}
a:hover {{
text-decoration: underline;
}}
.back-link {{
margin-top: 30px;
padding-top: 20px;
border-top: 1px solid #e1e5e9;
}}
</style>
</head>
<body>
<h1>{year}年{month}月のテックニュース</h1>
<p>{year}年{month}月に取得したテックニュースの一覧です。</p>
<ul>"""
for md_file in reversed(md_files): # 新しい順
date_str = md_file.stem
html_content += f'\n <li><a href="{date_str}.html">{date_str}</a></li>'
html_content += f"""
</ul>
<div class="back-link">
<p><a href="../index.html">← {year}年一覧に戻る</a></p>
</div>
</body>
</html>"""
with open(archive_dir / "index.html", "w", encoding="utf-8") as f:
f.write(html_content)
def update_yearly_index(year):
"""年別インデックスページを更新(MarkdownとHTML両方)"""
year_dir = Path(f"archives/{year}")
if not year_dir.exists():
return
# その年の月ディレクトリ一覧を取得
month_dirs = sorted([d for d in year_dir.iterdir() if d.is_dir() and d.name.isdigit()])
# Markdown版
md_content = f"# {year}年のテックニュース\n\n"
md_content += f"{year}年に取得したテックニュースの月別一覧です。\n\n"
for month_dir in reversed(month_dirs): # 新しい順
month = int(month_dir.name)
md_content += f"- [{year}年{month}月]({month_dir.name}/index.md)\n"
md_content += f"\n[← アーカイブ一覧に戻る](../index.md)\n"
with open(year_dir / "index.md", "w", encoding="utf-8") as f:
f.write(md_content)
# HTML版
html_content = f"""<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{year}年のテックニュース</title>
<!-- OGP Tags -->
<meta property="og:title" content="{year}年のテックニュース">
<meta property="og:description" content="日本の主要な技術系メディアの最新人気エントリーを毎日お届けします。">
<meta property="og:type" content="website">
<meta property="og:url" content="{DEFAULT_SITE_CONFIG.site_url}">
<meta property="og:image" content="{DEFAULT_SITE_CONFIG.og_image_url}">
<meta property="og:site_name" content="今日のテックニュース">
<!-- Twitter Card Tags -->
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:creator" content="@unsoluble_sugar">
<meta name="twitter:title" content="{year}年のテックニュース">
<meta name="twitter:description" content="日本の主要な技術系メディアの最新人気エントリーを毎日お届けします。">
<meta name="twitter:image" content="{DEFAULT_SITE_CONFIG.og_image_url}">
<!-- Favicon Links -->
<link rel="apple-touch-icon" sizes="180x180" href="../../assets/favicons/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="../../assets/favicons/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="../../assets/favicons/favicon-16x16.png">
<link rel="manifest" href="../../assets/favicons/site.webmanifest">
<link rel="shortcut icon" href="../../assets/favicons/favicon.ico">
<style>
body {{
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
line-height: 1.6;
color: #333;
}}
h1 {{
color: #1f2328;
}}
ul {{
list-style-type: disc;
padding-left: 2em;
}}
li {{
margin: 8px 0;
}}
a {{
color: #0969da;
text-decoration: none;
}}
a:hover {{
text-decoration: underline;
}}
.back-link {{
margin-top: 30px;
padding-top: 20px;
border-top: 1px solid #e1e5e9;
}}
</style>
</head>
<body>
<h1>{year}年のテックニュース</h1>
<p>{year}年に取得したテックニュースの月別一覧です。</p>
<ul>"""
for month_dir in reversed(month_dirs): # 新しい順
month = int(month_dir.name)
html_content += f'\n <li><a href="{month_dir.name}/index.html">{year}年{month}月</a></li>'
html_content += f"""
</ul>
<div class="back-link">
<p><a href="../index.html">← アーカイブ一覧に戻る</a></p>
</div>
</body>
</html>"""
with open(year_dir / "index.html", "w", encoding="utf-8") as f:
f.write(html_content)
def generate_missing_html_archives():
"""既存のMarkdownアーカイブファイルに対応するHTMLファイルが存在しない場合に生成する"""
archives_dir = Path("archives")
if not archives_dir.exists():
return
# 全てのMarkdownアーカイブファイルを検索
md_files = list(archives_dir.glob("**/????-??-??.md"))
for md_file in md_files:
html_file = md_file.with_suffix('.html')
# HTMLファイルが存在しない場合のみ生成
if not html_file.exists():
print(f"Generating missing HTML archive: {html_file}")
# Markdownファイルからコンテンツを読み取り、簡易的にHTMLに変換
try:
with open(md_file, 'r', encoding='utf-8') as f:
md_content = f.read()
# 日付を抽出
date_match = re.search(r'# 今日のテックニュース \((\d{4}-\d{2}-\d{2})\)', md_content)
if date_match:
date_str = date_match.group(1)
# 簡易的なHTML生成(完全な記事リスト無しでも基本構造を生成)
html_content = f"""<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>今日のテックニュース ({date_str})</title>
<!-- OGP Tags -->
<meta property="og:title" content="今日のテックニュース ({date_str})">
<meta property="og:description" content="日本の主要な技術系メディアの最新人気エントリーを毎日お届けします。">
<meta property="og:type" content="website">
<meta property="og:url" content="{DEFAULT_SITE_CONFIG.site_url}">
<meta property="og:image" content="{DEFAULT_SITE_CONFIG.og_image_url}">
<meta property="og:site_name" content="今日のテックニュース">
<!-- Twitter Card Tags -->
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@unsoluble_sugar">
<style>
body {{
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
line-height: 1.6;
color: #333;
}}
h1, h2 {{
color: #1f2328;
}}
a {{
color: #0969da;
text-decoration: none;
}}
a:hover {{
text-decoration: underline;
}}
.rss-info {{
background: #f6f8fa;
padding: 16px;
border-radius: 8px;
margin: 20px 0;
}}
.footer {{
margin-top: 40px;
padding: 20px 0;
border-top: 1px solid #e1e5e9;
text-align: center;
font-size: 14px;
color: #656d76;
}}
.footer a {{
color: #0969da;
text-decoration: none;
}}
.footer a:hover {{
text-decoration: underline;
}}
ul {{
line-height: 1.8;