GitHub Action
commited on
Commit
·
ecf2c66
1
Parent(s):
b32dc9f
Sync from GitHub with Git LFS
Browse files- scripts/publish_to_hashnode.py +23 -30
scripts/publish_to_hashnode.py
CHANGED
|
@@ -47,14 +47,17 @@ def file_hash(path):
|
|
| 47 |
|
| 48 |
|
| 49 |
def graphql_request(query, variables):
|
| 50 |
-
headers = {
|
|
|
|
|
|
|
|
|
|
| 51 |
response = requests.post(API_URL, json={"query": query, "variables": variables}, headers=headers)
|
| 52 |
try:
|
| 53 |
resp_json = response.json()
|
| 54 |
except json.JSONDecodeError:
|
| 55 |
raise Exception(f"GraphQL вернул не JSON: {response.text}")
|
| 56 |
|
| 57 |
-
print("DEBUG: GraphQL response:", json.dumps(resp_json, indent=2))
|
| 58 |
|
| 59 |
if response.status_code != 200:
|
| 60 |
raise Exception(f"GraphQL request failed with {response.status_code}: {response.text}")
|
|
@@ -63,15 +66,14 @@ def graphql_request(query, variables):
|
|
| 63 |
return resp_json
|
| 64 |
|
| 65 |
|
| 66 |
-
|
| 67 |
-
def create_post(title, slug, html):
|
| 68 |
query = """
|
| 69 |
-
mutation
|
| 70 |
-
|
| 71 |
-
|
| 72 |
_id
|
| 73 |
slug
|
| 74 |
-
|
| 75 |
}
|
| 76 |
}
|
| 77 |
}
|
|
@@ -79,24 +81,22 @@ def create_post(title, slug, html):
|
|
| 79 |
variables = {
|
| 80 |
"input": {
|
| 81 |
"title": title,
|
|
|
|
| 82 |
"slug": slug,
|
| 83 |
-
"
|
| 84 |
-
"isPartOfPublication": {
|
| 85 |
-
"publicationId": HASHNODE_PUBLICATION_ID
|
| 86 |
-
}
|
| 87 |
}
|
| 88 |
}
|
| 89 |
-
return graphql_request(query, variables)["data"]["
|
| 90 |
|
| 91 |
|
| 92 |
-
def update_post(post_id, title, slug,
|
| 93 |
query = """
|
| 94 |
-
mutation
|
| 95 |
-
|
| 96 |
-
|
| 97 |
_id
|
| 98 |
slug
|
| 99 |
-
|
| 100 |
}
|
| 101 |
}
|
| 102 |
}
|
|
@@ -105,11 +105,11 @@ def update_post(post_id, title, slug, html):
|
|
| 105 |
"id": post_id,
|
| 106 |
"input": {
|
| 107 |
"title": title,
|
| 108 |
-
"
|
| 109 |
-
"
|
| 110 |
}
|
| 111 |
}
|
| 112 |
-
return graphql_request(query, variables)["data"]["
|
| 113 |
|
| 114 |
|
| 115 |
def main(force=False):
|
|
@@ -130,21 +130,14 @@ def main(force=False):
|
|
| 130 |
md_text = source_link + md_text
|
| 131 |
md_text = convert_md_links(md_text)
|
| 132 |
|
| 133 |
-
# Hashnode принимает Markdown, так что HTML-конвертация не обязательна
|
| 134 |
-
# Но мы можем оставить HTML для единообразия
|
| 135 |
-
html_content = markdown.markdown(
|
| 136 |
-
md_text,
|
| 137 |
-
extensions=["tables", "fenced_code", "codehilite", "toc"]
|
| 138 |
-
)
|
| 139 |
-
|
| 140 |
try:
|
| 141 |
if name in published and "id" in published[name]:
|
| 142 |
post_id = published[name]["id"]
|
| 143 |
post = update_post(post_id, name, slug, md_text)
|
| 144 |
-
print(f"♻ Обновлён пост: {post['
|
| 145 |
else:
|
| 146 |
post = create_post(name, slug, md_text)
|
| 147 |
-
print(f"🆕 Пост опубликован: {post['
|
| 148 |
|
| 149 |
published[name] = {"id": post["_id"], "slug": post["slug"], "hash": h}
|
| 150 |
save_published(published)
|
|
|
|
| 47 |
|
| 48 |
|
| 49 |
def graphql_request(query, variables):
|
| 50 |
+
headers = {
|
| 51 |
+
"Authorization": f"Bearer {HASHNODE_TOKEN}",
|
| 52 |
+
"Content-Type": "application/json"
|
| 53 |
+
}
|
| 54 |
response = requests.post(API_URL, json={"query": query, "variables": variables}, headers=headers)
|
| 55 |
try:
|
| 56 |
resp_json = response.json()
|
| 57 |
except json.JSONDecodeError:
|
| 58 |
raise Exception(f"GraphQL вернул не JSON: {response.text}")
|
| 59 |
|
| 60 |
+
print("DEBUG: GraphQL response:", json.dumps(resp_json, indent=2))
|
| 61 |
|
| 62 |
if response.status_code != 200:
|
| 63 |
raise Exception(f"GraphQL request failed with {response.status_code}: {response.text}")
|
|
|
|
| 66 |
return resp_json
|
| 67 |
|
| 68 |
|
| 69 |
+
def create_post(title, slug, markdown_content):
|
|
|
|
| 70 |
query = """
|
| 71 |
+
mutation CreateDraft($input: CreateDraftInput!) {
|
| 72 |
+
createDraft(input: $input) {
|
| 73 |
+
draft {
|
| 74 |
_id
|
| 75 |
slug
|
| 76 |
+
title
|
| 77 |
}
|
| 78 |
}
|
| 79 |
}
|
|
|
|
| 81 |
variables = {
|
| 82 |
"input": {
|
| 83 |
"title": title,
|
| 84 |
+
"contentMarkdown": markdown_content,
|
| 85 |
"slug": slug,
|
| 86 |
+
"publicationId": HASHNODE_PUBLICATION_ID
|
|
|
|
|
|
|
|
|
|
| 87 |
}
|
| 88 |
}
|
| 89 |
+
return graphql_request(query, variables)["data"]["createDraft"]["draft"]
|
| 90 |
|
| 91 |
|
| 92 |
+
def update_post(post_id, title, slug, markdown_content):
|
| 93 |
query = """
|
| 94 |
+
mutation UpdateDraft($id: ID!, $input: UpdateDraftInput!) {
|
| 95 |
+
updateDraft(id: $id, input: $input) {
|
| 96 |
+
draft {
|
| 97 |
_id
|
| 98 |
slug
|
| 99 |
+
title
|
| 100 |
}
|
| 101 |
}
|
| 102 |
}
|
|
|
|
| 105 |
"id": post_id,
|
| 106 |
"input": {
|
| 107 |
"title": title,
|
| 108 |
+
"contentMarkdown": markdown_content,
|
| 109 |
+
"slug": slug
|
| 110 |
}
|
| 111 |
}
|
| 112 |
+
return graphql_request(query, variables)["data"]["updateDraft"]["draft"]
|
| 113 |
|
| 114 |
|
| 115 |
def main(force=False):
|
|
|
|
| 130 |
md_text = source_link + md_text
|
| 131 |
md_text = convert_md_links(md_text)
|
| 132 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
try:
|
| 134 |
if name in published and "id" in published[name]:
|
| 135 |
post_id = published[name]["id"]
|
| 136 |
post = update_post(post_id, name, slug, md_text)
|
| 137 |
+
print(f"♻ Обновлён пост: https://hashnode.com/@yourusername/{post['slug']}")
|
| 138 |
else:
|
| 139 |
post = create_post(name, slug, md_text)
|
| 140 |
+
print(f"🆕 Пост опубликован: https://hashnode.com/@yourusername/{post['slug']}")
|
| 141 |
|
| 142 |
published[name] = {"id": post["_id"], "slug": post["slug"], "hash": h}
|
| 143 |
save_published(published)
|