11"""Notify upload-post.com about newly added blog posts.
22
33Reads a newline-separated list of added markdown paths from the env var
4- ADDED_FILES, parses Hugo front matter, and POSTs a formatted message per file
5- to the upload-post.com text endpoint .
4+ ADDED_FILES, parses Hugo front matter, and posts a formatted message per file
5+ via the official upload-post Python SDK .
66"""
77
88import os
99import sys
1010from pathlib import Path
1111
12- import requests
1312import yaml
13+ from upload_post import UploadPostClient
1414
15- API_URL = "https://api.upload-post.com/api/upload_text"
1615SITE_BASE = "https://2rebcat.github.io"
1716BLOG_ROOT = Path ("content/blog" )
1817INDEX_NAME = "_index.md"
@@ -43,15 +42,6 @@ def post_url_for(path: Path) -> str:
4342 return f"{ SITE_BASE } /blog/{ topic } /{ slug } /"
4443
4544
46- def upload (message : str , api_key : str , user : str , platforms : list [str ]) -> None :
47- headers = {"Authorization" : f"Apikey { api_key } " }
48- fields = [("user" , user ), ("title" , message )]
49- fields .extend (("platform[]" , p ) for p in platforms )
50- resp = requests .post (API_URL , headers = headers , files = fields , timeout = 30 )
51- print (f" status={ resp .status_code } body={ resp .text [:500 ]} " )
52- resp .raise_for_status ()
53-
54-
5545def main () -> int :
5646 api_key = os .environ ["UPLOAD_POST_API_KEY" ].strip ()
5747 user = os .environ ["UPLOAD_POST_USER" ].strip ()
@@ -61,12 +51,14 @@ def main() -> int:
6151 if not user :
6252 print ("UPLOAD_POST_USER is empty." , file = sys .stderr )
6353 return 1
64- print ( f"Using upload-post user=' { user } ' (length= { len ( user ) } )" )
54+
6555 platforms = [
6656 p .strip ()
6757 for p in os .environ .get ("UPLOAD_POST_PLATFORMS" , "x" ).split ("," )
6858 if p .strip ()
6959 ]
60+ print (f"user={ user !r} platforms={ platforms } " )
61+
7062 added = [
7163 line .strip ()
7264 for line in os .environ .get ("ADDED_FILES" , "" ).splitlines ()
@@ -91,6 +83,8 @@ def main() -> int:
9183 return 0
9284
9385 print (f"Found { len (targets )} new post(s): { [str (p ) for p in targets ]} " )
86+
87+ client = UploadPostClient (api_key )
9488 failures = 0
9589 for path in targets :
9690 try :
@@ -103,7 +97,12 @@ def main() -> int:
10397 url = post_url_for (path )
10498 message = build_message (title , summary , url )
10599 print (f"Posting for { path } :" )
106- upload (message , api_key , user , platforms )
100+ response = client .upload_text (
101+ title = message ,
102+ user = user ,
103+ platforms = platforms ,
104+ )
105+ print (f" response={ response } " )
107106 except Exception as e :
108107 failures += 1
109108 print (f" ERROR for { path } : { e } " , file = sys .stderr )
0 commit comments