GitHub Action
commited on
Commit
·
ce09a51
1
Parent(s):
aebb950
Sync from GitHub with Git LFS
Browse files- scripts/publish_to_blogger.py +95 -94
scripts/publish_to_blogger.py
CHANGED
|
@@ -1,99 +1,100 @@
|
|
| 1 |
-
|
| 2 |
import os
|
|
|
|
| 3 |
import hashlib
|
| 4 |
-
import
|
| 5 |
from googleapiclient.discovery import build
|
| 6 |
from google.oauth2.credentials import Credentials
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
try:
|
| 26 |
-
with open(JSON_FILE, 'r', encoding='utf-8') as f:
|
| 27 |
-
published = json.load(f)
|
| 28 |
-
print(f"✅ Загружен список опубликованных постов: {list(published.keys())}")
|
| 29 |
-
except json.JSONDecodeError:
|
| 30 |
-
print("⚠ published_posts.json пустой или поврежден — начинаем с нуля.")
|
| 31 |
-
published = {}
|
| 32 |
-
else:
|
| 33 |
-
published = {}
|
| 34 |
print("⚠ published_posts.json не найден — начинаем с нуля.")
|
| 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 |
-
print(f"
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
#
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
import os
|
| 3 |
+
import json
|
| 4 |
import hashlib
|
| 5 |
+
import time
|
| 6 |
from googleapiclient.discovery import build
|
| 7 |
from google.oauth2.credentials import Credentials
|
| 8 |
+
import argparse
|
| 9 |
+
|
| 10 |
+
# === Настройки ===
|
| 11 |
+
SITE_DIR = "site" # HTML-файлы после mkdocs build
|
| 12 |
+
PUBLISHED_FILE = "published_posts.json"
|
| 13 |
+
SLEEP_BETWEEN_POSTS = 60 # секунд
|
| 14 |
+
BLOG_ID = os.environ.get("BLOG_ID")
|
| 15 |
+
TOKEN_FILE = os.environ.get("TOKEN_FILE", "token.pkl")
|
| 16 |
+
|
| 17 |
+
# === Утилиты ===
|
| 18 |
+
def md5sum(path):
|
| 19 |
+
with open(path, "rb") as f:
|
| 20 |
+
return hashlib.md5(f.read()).hexdigest()
|
| 21 |
+
|
| 22 |
+
def load_published():
|
| 23 |
+
if os.path.exists(PUBLISHED_FILE):
|
| 24 |
+
with open(PUBLISHED_FILE, "r", encoding="utf-8") as f:
|
| 25 |
+
return json.load(f)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
print("⚠ published_posts.json не найден — начинаем с нуля.")
|
| 27 |
+
return {}
|
| 28 |
+
|
| 29 |
+
def save_published(data):
|
| 30 |
+
with open(PUBLISHED_FILE, "w", encoding="utf-8") as f:
|
| 31 |
+
json.dump(data, f, ensure_ascii=False, indent=2)
|
| 32 |
+
|
| 33 |
+
def get_service():
|
| 34 |
+
creds = Credentials.from_authorized_user_file(TOKEN_FILE)
|
| 35 |
+
return build("blogger", "v3", credentials=creds)
|
| 36 |
+
|
| 37 |
+
# === Основная логика ===
|
| 38 |
+
def publish_site():
|
| 39 |
+
service = get_service()
|
| 40 |
+
published = load_published()
|
| 41 |
+
|
| 42 |
+
# собираем все html-файлы
|
| 43 |
+
html_files = []
|
| 44 |
+
for root, _, files in os.walk(SITE_DIR):
|
| 45 |
+
for name in files:
|
| 46 |
+
if name.endswith(".html"):
|
| 47 |
+
path = os.path.join(root, name)
|
| 48 |
+
rel_path = os.path.relpath(path, SITE_DIR) # ключ
|
| 49 |
+
html_files.append((rel_path, path))
|
| 50 |
+
|
| 51 |
+
for rel_path, path in html_files:
|
| 52 |
+
content_hash = md5sum(path)
|
| 53 |
+
post_meta = published.get(rel_path)
|
| 54 |
+
|
| 55 |
+
# читаем содержимое html
|
| 56 |
+
with open(path, "r", encoding="utf-8") as f:
|
| 57 |
+
html_content = f.read()
|
| 58 |
+
|
| 59 |
+
if post_meta and post_meta["hash"] == content_hash:
|
| 60 |
+
print(f"⏭ Пропускаем {rel_path} — без изменений")
|
| 61 |
+
continue
|
| 62 |
+
|
| 63 |
+
title = os.path.splitext(os.path.basename(rel_path))[0]
|
| 64 |
+
|
| 65 |
+
if post_meta: # обновление поста
|
| 66 |
+
post_id = post_meta["id"]
|
| 67 |
+
print(f"🔄 Обновляем пост {rel_path}")
|
| 68 |
+
post = (
|
| 69 |
+
service.posts()
|
| 70 |
+
.update(
|
| 71 |
+
blogId=BLOG_ID,
|
| 72 |
+
postId=post_id,
|
| 73 |
+
body={"title": title, "content": html_content},
|
| 74 |
+
)
|
| 75 |
+
.execute()
|
| 76 |
+
)
|
| 77 |
+
else: # новый пост
|
| 78 |
+
print(f"🆕 Публикуем новый пост {rel_path}")
|
| 79 |
+
post = (
|
| 80 |
+
service.posts()
|
| 81 |
+
.insert(
|
| 82 |
+
blogId=BLOG_ID,
|
| 83 |
+
body={"title": title, "content": html_content},
|
| 84 |
+
)
|
| 85 |
+
.execute()
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
url = post.get("url")
|
| 89 |
+
post_id = post.get("id")
|
| 90 |
+
print(f"✅ {rel_path} → {url}")
|
| 91 |
+
|
| 92 |
+
# сохраняем метаданные
|
| 93 |
+
published[rel_path] = {"id": post_id, "hash": content_hash}
|
| 94 |
+
save_published(published)
|
| 95 |
+
|
| 96 |
+
print(f"⏱ Пауза {SLEEP_BETWEEN_POSTS} секунд перед следующим постом…")
|
| 97 |
+
time.sleep(SLEEP_BETWEEN_POSTS)
|
| 98 |
+
|
| 99 |
+
if __name__ == "__main__":
|
| 100 |
+
publish_site()
|