GitHub Action
commited on
Commit
·
15fb820
1
Parent(s):
5ac3c13
Sync from GitHub with Git LFS
Browse files- scripts/publish_to_spec.py +0 -131
scripts/publish_to_spec.py
DELETED
@@ -1,131 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import json
|
3 |
-
import hashlib
|
4 |
-
import time
|
5 |
-
from pathlib import Path
|
6 |
-
import requests
|
7 |
-
|
8 |
-
# -----------------------------
|
9 |
-
# Конфигурация
|
10 |
-
# -----------------------------
|
11 |
-
HASHNODE_TOKEN = os.environ["HASHNODE_TOKEN"]
|
12 |
-
HASHNODE_PUBLICATION_ID = os.environ["HASHNODE_SPEC_ID"]
|
13 |
-
API_URL = "https://gql.hashnode.com"
|
14 |
-
|
15 |
-
PUBLISHED_FILE = "published_docs.json"
|
16 |
-
|
17 |
-
# Список файлов для публикации: путь -> title/slug
|
18 |
-
FILES_TO_PUBLISH = {
|
19 |
-
"agents/tools/db_structure.sql": {
|
20 |
-
"title": "Database Structure (CCore)",
|
21 |
-
"slug": "database",
|
22 |
-
},
|
23 |
-
"docs/HMP-agent-REPL-cycle.md": {
|
24 |
-
"title": "HMP Agent REPL Cycle",
|
25 |
-
"slug": "hmp-agent-repl-cycle",
|
26 |
-
},
|
27 |
-
}
|
28 |
-
|
29 |
-
# -----------------------------
|
30 |
-
# Функции
|
31 |
-
# -----------------------------
|
32 |
-
def file_hash(content: str) -> str:
|
33 |
-
return hashlib.md5(content.encode("utf-8")).hexdigest()
|
34 |
-
|
35 |
-
def load_published() -> dict:
|
36 |
-
if Path(PUBLISHED_FILE).exists():
|
37 |
-
with open(PUBLISHED_FILE, encoding="utf-8") as f:
|
38 |
-
return json.load(f)
|
39 |
-
return {}
|
40 |
-
|
41 |
-
def save_published(data: dict):
|
42 |
-
with open(PUBLISHED_FILE, "w", encoding="utf-8") as f:
|
43 |
-
json.dump(data, f, ensure_ascii=False, indent=2)
|
44 |
-
|
45 |
-
def graphql_request(query: str, variables: dict) -> dict:
|
46 |
-
headers = {
|
47 |
-
"Authorization": f"Bearer {HASHNODE_TOKEN}",
|
48 |
-
"Content-Type": "application/json"
|
49 |
-
}
|
50 |
-
resp = requests.post(API_URL, json={"query": query, "variables": variables}, headers=headers)
|
51 |
-
data = resp.json()
|
52 |
-
if "errors" in data:
|
53 |
-
raise Exception(f"GraphQL errors: {data['errors']}")
|
54 |
-
return data
|
55 |
-
|
56 |
-
def create_draft(title: str, slug: str, content: str) -> dict:
|
57 |
-
query = """
|
58 |
-
mutation CreateDraft($input: CreateDraftInput!) {
|
59 |
-
createDraft(input: $input) { draft { id slug title } }
|
60 |
-
}
|
61 |
-
"""
|
62 |
-
variables = {
|
63 |
-
"input": {
|
64 |
-
"title": title,
|
65 |
-
"contentMarkdown": content,
|
66 |
-
"publicationId": HASHNODE_PUBLICATION_ID
|
67 |
-
}
|
68 |
-
}
|
69 |
-
return graphql_request(query, variables)["data"]["createDraft"]["draft"]
|
70 |
-
|
71 |
-
def update_post(post_id: str, title: str, content: str) -> dict:
|
72 |
-
query = """
|
73 |
-
mutation UpdatePost($input: UpdatePostInput!) {
|
74 |
-
updatePost(input: $input) { post { id slug title } }
|
75 |
-
}
|
76 |
-
"""
|
77 |
-
variables = {"input": {"id": post_id, "title": title, "contentMarkdown": content}}
|
78 |
-
return graphql_request(query, variables)["data"]["updatePost"]["post"]
|
79 |
-
|
80 |
-
def publish_draft(draft_id: str) -> dict:
|
81 |
-
query = """
|
82 |
-
mutation PublishDraft($input: PublishDraftInput!) {
|
83 |
-
publishDraft(input: $input) { post { id slug url } }
|
84 |
-
}
|
85 |
-
"""
|
86 |
-
variables = {"input": {"draftId": draft_id}}
|
87 |
-
return graphql_request(query, variables)["data"]["publishDraft"]["post"]
|
88 |
-
|
89 |
-
# -----------------------------
|
90 |
-
# Главная функция
|
91 |
-
# -----------------------------
|
92 |
-
def main():
|
93 |
-
published = load_published()
|
94 |
-
|
95 |
-
for path, info in FILES_TO_PUBLISH.items():
|
96 |
-
p = Path(path)
|
97 |
-
if not p.exists():
|
98 |
-
print(f"❌ Файл не найден: {path}")
|
99 |
-
continue
|
100 |
-
|
101 |
-
# Формируем содержимое поста с ссылкой на исходный файл
|
102 |
-
content = f"Источник: [{p.name}](https://github.com/kagvi13/HMP/blob/main/{path})\n\n"
|
103 |
-
content += p.read_text(encoding="utf-8")
|
104 |
-
h = file_hash(content)
|
105 |
-
name = p.stem
|
106 |
-
|
107 |
-
# Проверка изменений
|
108 |
-
if name in published and published[name].get("hash") == h:
|
109 |
-
print(f"✅ {name} без изменений — пропускаем")
|
110 |
-
continue
|
111 |
-
|
112 |
-
try:
|
113 |
-
if name in published and "id" in published[name]:
|
114 |
-
post = update_post(published[name]["id"], info["title"], content)
|
115 |
-
print(f"♻ Обновлён пост: {post['slug']}")
|
116 |
-
else:
|
117 |
-
draft = create_draft(info["title"], info["slug"], content)
|
118 |
-
post = publish_draft(draft["id"])
|
119 |
-
print(f"🆕 Опубликован пост: {post['slug']}")
|
120 |
-
|
121 |
-
# Обновляем локальный JSON
|
122 |
-
published[name] = {"id": post["id"], "hash": h}
|
123 |
-
save_published(published)
|
124 |
-
time.sleep(5)
|
125 |
-
|
126 |
-
except Exception as e:
|
127 |
-
print(f"❌ Ошибка публикации {name}: {e}")
|
128 |
-
break
|
129 |
-
|
130 |
-
if __name__ == "__main__":
|
131 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|