Spaces:
Running
Running
Update news.py
Browse files
news.py
CHANGED
|
@@ -3,7 +3,7 @@ import time
|
|
| 3 |
import json
|
| 4 |
import requests
|
| 5 |
import hashlib
|
| 6 |
-
from datetime import datetime
|
| 7 |
from bs4 import BeautifulSoup
|
| 8 |
import xml.etree.ElementTree as ET
|
| 9 |
|
|
@@ -28,8 +28,10 @@ HEADERS_POST = {
|
|
| 28 |
RSS_URL = "https://www.nippon.com/ja/rss-all/"
|
| 29 |
|
| 30 |
# ===== 設定 =====
|
| 31 |
-
INTERVAL_SECONDS = 60 * 60 * 12 # 12時間
|
| 32 |
SENT_LOG_FILE = "sent_nippon_news.json"
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
|
| 35 |
# ===== Utils =====
|
|
@@ -45,8 +47,19 @@ def save_sent_log(sent_set):
|
|
| 45 |
json.dump(list(sent_set), f, ensure_ascii=False, indent=2)
|
| 46 |
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
def hash_link(link: str) -> str:
|
| 49 |
-
"""リンクをハッシュ化(重複防止用)"""
|
| 50 |
return hashlib.sha256(link.encode("utf-8")).hexdigest()
|
| 51 |
|
| 52 |
|
|
@@ -82,7 +95,6 @@ def fetch_rss_items():
|
|
| 82 |
link = item.findtext("link", "").strip()
|
| 83 |
description_raw = item.findtext("description", "").strip()
|
| 84 |
|
| 85 |
-
# CDATA 内のHTMLをテキスト化
|
| 86 |
soup = BeautifulSoup(description_raw, "lxml")
|
| 87 |
description = soup.get_text(strip=True)
|
| 88 |
|
|
@@ -97,42 +109,52 @@ def fetch_rss_items():
|
|
| 97 |
|
| 98 |
|
| 99 |
# ===== Main =====
|
| 100 |
-
def
|
| 101 |
sent_log = load_sent_log()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
while True:
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
f"{item['description']}"
|
| 118 |
-
)
|
| 119 |
-
|
| 120 |
-
send_to_channel(message)
|
| 121 |
-
sent_log.add(link_hash)
|
| 122 |
-
new_count += 1
|
| 123 |
-
|
| 124 |
-
time.sleep(1) # 連投防止
|
| 125 |
-
|
| 126 |
-
if new_count > 0:
|
| 127 |
-
save_sent_log(sent_log)
|
| 128 |
-
print(f"{new_count} 件のニュースを送信しました")
|
| 129 |
-
else:
|
| 130 |
-
print("新しいニュースはありません")
|
| 131 |
-
|
| 132 |
-
except Exception as e:
|
| 133 |
-
print("エラー:", e)
|
| 134 |
-
|
| 135 |
-
time.sleep(INTERVAL_SECONDS)
|
| 136 |
|
| 137 |
|
| 138 |
if __name__ == "__main__":
|
|
|
|
| 3 |
import json
|
| 4 |
import requests
|
| 5 |
import hashlib
|
| 6 |
+
from datetime import datetime
|
| 7 |
from bs4 import BeautifulSoup
|
| 8 |
import xml.etree.ElementTree as ET
|
| 9 |
|
|
|
|
| 28 |
RSS_URL = "https://www.nippon.com/ja/rss-all/"
|
| 29 |
|
| 30 |
# ===== 設定 =====
|
|
|
|
| 31 |
SENT_LOG_FILE = "sent_nippon_news.json"
|
| 32 |
+
LAST_RUN_FILE = "last_run.json"
|
| 33 |
+
|
| 34 |
+
TARGET_TIMES = {"06:30", "18:30"} # 朝・夕方
|
| 35 |
|
| 36 |
|
| 37 |
# ===== Utils =====
|
|
|
|
| 47 |
json.dump(list(sent_set), f, ensure_ascii=False, indent=2)
|
| 48 |
|
| 49 |
|
| 50 |
+
def load_last_run():
|
| 51 |
+
if not os.path.exists(LAST_RUN_FILE):
|
| 52 |
+
return ""
|
| 53 |
+
with open(LAST_RUN_FILE, "r", encoding="utf-8") as f:
|
| 54 |
+
return json.load(f).get("last_run", "")
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def save_last_run(value):
|
| 58 |
+
with open(LAST_RUN_FILE, "w", encoding="utf-8") as f:
|
| 59 |
+
json.dump({"last_run": value}, f)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
def hash_link(link: str) -> str:
|
|
|
|
| 63 |
return hashlib.sha256(link.encode("utf-8")).hexdigest()
|
| 64 |
|
| 65 |
|
|
|
|
| 95 |
link = item.findtext("link", "").strip()
|
| 96 |
description_raw = item.findtext("description", "").strip()
|
| 97 |
|
|
|
|
| 98 |
soup = BeautifulSoup(description_raw, "lxml")
|
| 99 |
description = soup.get_text(strip=True)
|
| 100 |
|
|
|
|
| 109 |
|
| 110 |
|
| 111 |
# ===== Main =====
|
| 112 |
+
def run_job():
|
| 113 |
sent_log = load_sent_log()
|
| 114 |
+
items = fetch_rss_items()
|
| 115 |
+
new_count = 0
|
| 116 |
+
|
| 117 |
+
for item in items:
|
| 118 |
+
link_hash = hash_link(item["link"])
|
| 119 |
+
if link_hash in sent_log:
|
| 120 |
+
continue
|
| 121 |
+
|
| 122 |
+
message = (
|
| 123 |
+
f"<link type=\"url\" value=\"{item['link']}\">"
|
| 124 |
+
f"{item['title']}"
|
| 125 |
+
f"</link>\n\n"
|
| 126 |
+
f"{item['description']}"
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
send_to_channel(message)
|
| 130 |
+
sent_log.add(link_hash)
|
| 131 |
+
new_count += 1
|
| 132 |
+
time.sleep(1)
|
| 133 |
+
|
| 134 |
+
if new_count > 0:
|
| 135 |
+
save_sent_log(sent_log)
|
| 136 |
+
print(f"{new_count} 件のニュースを送信しました")
|
| 137 |
+
else:
|
| 138 |
+
print("新しいニュースはありません")
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
def main():
|
| 142 |
+
last_run = load_last_run()
|
| 143 |
|
| 144 |
while True:
|
| 145 |
+
now = datetime.now()
|
| 146 |
+
now_key = now.strftime("%Y-%m-%d %H:%M")
|
| 147 |
+
now_time = now.strftime("%H:%M")
|
| 148 |
+
|
| 149 |
+
if now_time in TARGET_TIMES and last_run != now_key:
|
| 150 |
+
print(f"{now_time} 実行開始")
|
| 151 |
+
try:
|
| 152 |
+
run_job()
|
| 153 |
+
save_last_run(now_key)
|
| 154 |
+
except Exception as e:
|
| 155 |
+
print("エラー:", e)
|
| 156 |
+
|
| 157 |
+
time.sleep(60) # 1分ごとにチェック
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
|
| 160 |
if __name__ == "__main__":
|