GitHub Action commited on
Commit ·
1b40fd9
1
Parent(s): 43fcc2a
Sync from GitHub with Git LFS
Browse files- scripts/publish_to_blogger.py +62 -37
scripts/publish_to_blogger.py
CHANGED
|
@@ -1,8 +1,16 @@
|
|
| 1 |
-
import
|
| 2 |
import json
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
import markdown2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Загружаем токен
|
| 8 |
TOKEN_FILE = os.environ.get('TOKEN_FILE', 'token.pkl')
|
|
@@ -12,56 +20,73 @@ with open(TOKEN_FILE, 'rb') as f:
|
|
| 12 |
service = build('blogger', 'v3', credentials=creds)
|
| 13 |
BLOG_ID = os.environ['BLOG_ID']
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
JSON_FILE = os.path.join(SCRIPT_DIR, 'published_posts.json')
|
| 18 |
-
|
| 19 |
-
# Безопасная загрузка JSON
|
| 20 |
-
if os.path.exists(JSON_FILE) and os.path.getsize(JSON_FILE) > 0:
|
| 21 |
try:
|
| 22 |
with open(JSON_FILE, 'r', encoding='utf-8') as f:
|
| 23 |
published = json.load(f)
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
published = {}
|
| 26 |
else:
|
| 27 |
published = {}
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
|
| 34 |
-
|
| 35 |
-
if filename.endswith('.md'):
|
| 36 |
-
with open(os.path.join(POSTS_DIR, filename), 'r', encoding='utf-8') as f:
|
| 37 |
md_content = f.read()
|
| 38 |
-
html_content = markdown2.markdown(md_content)
|
| 39 |
|
| 40 |
-
|
| 41 |
-
content_hash =
|
| 42 |
|
| 43 |
-
#
|
| 44 |
if title in published and published[title]['hash'] == content_hash:
|
| 45 |
-
print(f"
|
| 46 |
continue
|
| 47 |
|
| 48 |
post = {
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 51 |
}
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
import json
|
| 3 |
+
import os
|
| 4 |
+
import hashlib
|
| 5 |
import markdown2
|
| 6 |
+
from googleapiclient.discovery import build
|
| 7 |
+
from google.oauth2.credentials import Credentials
|
| 8 |
+
from googleapiclient.errors import HttpError
|
| 9 |
+
import pickle
|
| 10 |
+
|
| 11 |
+
# Файлы
|
| 12 |
+
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 13 |
+
JSON_FILE = os.path.join(SCRIPT_DIR, "published_posts.json")
|
| 14 |
|
| 15 |
# Загружаем токен
|
| 16 |
TOKEN_FILE = os.environ.get('TOKEN_FILE', 'token.pkl')
|
|
|
|
| 20 |
service = build('blogger', 'v3', credentials=creds)
|
| 21 |
BLOG_ID = os.environ['BLOG_ID']
|
| 22 |
|
| 23 |
+
# Загружаем список опубликованных постов
|
| 24 |
+
if os.path.exists(JSON_FILE):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
try:
|
| 26 |
with open(JSON_FILE, 'r', encoding='utf-8') as f:
|
| 27 |
published = json.load(f)
|
| 28 |
+
print(f"Успешно загружен список опубликованных постов: {published}")
|
| 29 |
+
except json.JSONDecodeError:
|
| 30 |
+
print("published_posts.json пустой или поврежден — начинаем с нуля.")
|
| 31 |
published = {}
|
| 32 |
else:
|
| 33 |
published = {}
|
| 34 |
|
| 35 |
+
# Обход markdown файлов
|
| 36 |
+
for root, _, files in os.walk("docs"):
|
| 37 |
+
for filename in files:
|
| 38 |
+
if not filename.endswith(".md"):
|
| 39 |
+
continue
|
| 40 |
|
| 41 |
+
path = os.path.join(root, filename)
|
| 42 |
+
title = os.path.splitext(filename)[0]
|
| 43 |
|
| 44 |
+
with open(path, 'r', encoding='utf-8') as f:
|
|
|
|
|
|
|
| 45 |
md_content = f.read()
|
|
|
|
| 46 |
|
| 47 |
+
html_content = markdown2.markdown(md_content)
|
| 48 |
+
content_hash = hashlib.md5(md_content.encode('utf-8')).hexdigest()
|
| 49 |
|
| 50 |
+
# Пропускаем если ничего не изменилось
|
| 51 |
if title in published and published[title]['hash'] == content_hash:
|
| 52 |
+
print(f"Без изменений: {title}")
|
| 53 |
continue
|
| 54 |
|
| 55 |
post = {
|
| 56 |
+
"kind": "blogger#post",
|
| 57 |
+
"title": title,
|
| 58 |
+
"content": html_content
|
| 59 |
}
|
| 60 |
|
| 61 |
+
try:
|
| 62 |
+
if title in published:
|
| 63 |
+
# обновляем
|
| 64 |
+
post_id = published[title]['id']
|
| 65 |
+
updated_post = service.posts().update(
|
| 66 |
+
blogId=BLOG_ID, postId=post_id, body=post
|
| 67 |
+
).execute()
|
| 68 |
+
print(f"Пост обновлён: {updated_post['url']}")
|
| 69 |
+
published[title] = {"id": post_id, "hash": content_hash}
|
| 70 |
+
else:
|
| 71 |
+
# публикуем новый
|
| 72 |
+
new_post = service.posts().insert(
|
| 73 |
+
blogId=BLOG_ID, body=post, isDraft=False
|
| 74 |
+
).execute()
|
| 75 |
+
print(f"Пост опубликован: {new_post['url']}")
|
| 76 |
+
published[title] = {"id": new_post['id'], "hash": content_hash}
|
| 77 |
+
|
| 78 |
+
# 💾 сохраняем прогресс
|
| 79 |
+
with open(JSON_FILE, 'w', encoding='utf-8') as f:
|
| 80 |
+
json.dump(published, f, ensure_ascii=False, indent=2)
|
| 81 |
+
|
| 82 |
+
# 🕒 пауза в 10 мин.
|
| 83 |
+
print("Пауза 10 мин. перед следующим постом...")
|
| 84 |
+
time.sleep(600)
|
| 85 |
|
| 86 |
+
except HttpError as e:
|
| 87 |
+
if e.resp.status == 403 and "quotaExceeded" in str(e):
|
| 88 |
+
print("⚠ Достигнут лимит Blogger API. Засыпаем на 1 час...")
|
| 89 |
+
time.sleep(1 * 3600)
|
| 90 |
+
continue
|
| 91 |
+
else:
|
| 92 |
+
raise
|