From 05fb63e633baaeb712288e563759dfbd08fda581 Mon Sep 17 00:00:00 2001 From: Abner Coimbre Date: Sun, 25 Jan 2026 09:21:01 -0800 Subject: [PATCH] Implement new bloggify() --- main.go | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/main.go b/main.go index e9ac310..45d5749 100644 --- a/main.go +++ b/main.go @@ -190,6 +190,16 @@ func sendTest(cfg *Config, markdownFile string) { return } + /* Convert Markdown to Blog Post */ + /* *** */ + url, valid := bloggify(cfg, subject, body, false) + if !valid { + fmt.Printf("Error converting '%s' into a blog post\n", markdownFile) + return + } + + body = fmt.Sprintf("[View Online](%s)\n\n%s", url, body) + /* Convert Markdown to HTML */ /* *** */ html := blackfriday.Run([]byte(body)) @@ -208,6 +218,79 @@ func sendTest(cfg *Config, markdownFile string) { os.WriteFile(sha1File, []byte(sumString), 0666) } +func bloggify(cfg *Config, subject, body string, isLive bool) (url string, valid bool) { + if !isLive { + return "https://handmadecities.com/meetup-invite-test", true + } + + /* Prepare request payload */ + /* *** */ + payload := map[string]string{ + "subject": subject, + "body": body, + } + + payloadBytes, err := json.Marshal(payload) + if err != nil { + fmt.Printf("Error encoding bloggify payload: %v\n", err) + return "", false + } + + /* Prepare request */ + /* *** */ + req, err := http.NewRequest("POST", "https://deploy.handmadecities.com/v1/bloggify", bytes.NewReader(payloadBytes)) + if err != nil { + fmt.Printf("Error creating bloggify request: %v\n", err) + return "", false + } + + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Authorization", cfg.HMC.SharedSecret) + + /* Execute request */ + /* *** */ + client := http.Client{} + resp, err := client.Do(req) + if err != nil { + fmt.Printf("Error making bloggify request: %v\n", err) + return "", false + } + defer resp.Body.Close() + + /* Fail if status code is not in the 2xx range */ + /* *** */ + if resp.StatusCode < 200 || resp.StatusCode >= 300 { + fmt.Printf("Bloggify error: HTTP status code %d\n", resp.StatusCode) + return "", false + } + + /* Read response's body */ + /* *** */ + respBody, err := ioutil.ReadAll(resp.Body) + if err != nil { + fmt.Printf("Error reading bloggify response body: %v\n", err) + return "", false + } + + /* Parse JSON response */ + /* *** */ + var data map[string]interface{} + if err := json.Unmarshal(respBody, &data); err != nil { + fmt.Printf("Error unmarshalling bloggify JSON: %v\n", err) + return "", false + } + + /* Extract the URL from the response */ + /* *** */ + url, ok := data["url"].(string) + if !ok || url == "" { + fmt.Println("Bloggify response format error: 'url' field not found or empty") + return "", false + } + + return url, true +} + func sendNews(cfg *Config, markdownFile string) { /* Open and parse Markdown file */ /* *** */ @@ -228,6 +311,16 @@ func sendNews(cfg *Config, markdownFile string) { return } + /* Convert Markdown to Blog Post */ + /* *** */ + url, valid := bloggify(cfg, subject, body, true) + if !valid { + fmt.Printf("Error converting '%s' into a blog post\n", markdownFile) + return + } + + body = fmt.Sprintf("[View Online](%s)\n\n%s", url, body) + /* Convert Markdown to HTML */ /* *** */ html := blackfriday.Run([]byte(body))