diff options
| author | Paul Buetow <paul@buetow.org> | 2024-10-19 21:02:14 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-10-19 21:02:14 +0300 |
| commit | e02dcb943263da2fa701a7aba96b5dc41c14ffe0 (patch) | |
| tree | ca34f499a37f39a65bb239f632057698141e1dec /internal/platforms/linkedin | |
| parent | c072a8a06c9634948497f4ef5e69b832c80be195 (diff) | |
fix linkedin escapes
Diffstat (limited to 'internal/platforms/linkedin')
| -rw-r--r-- | internal/platforms/linkedin/escapes.go | 39 | ||||
| -rw-r--r-- | internal/platforms/linkedin/escapes_test.go | 15 | ||||
| -rw-r--r-- | internal/platforms/linkedin/linkedin.go | 13 |
3 files changed, 62 insertions, 5 deletions
diff --git a/internal/platforms/linkedin/escapes.go b/internal/platforms/linkedin/escapes.go new file mode 100644 index 0000000..5f803a5 --- /dev/null +++ b/internal/platforms/linkedin/escapes.go @@ -0,0 +1,39 @@ +package linkedin + +import ( + "strings" +) + +// https://learn.microsoft.com/en-us/linkedin/marketing/community-management/shares/little-text-format?view=li-lms-2024-01#language-grammar +func escapeLinkedInText(input string) string { + var builder strings.Builder + + reservedChars := map[rune]string{ + '"': "\\\"", + '|': "\\|", + '{': "\\{", + '}': "\\}", + // '@': "\\@", + '[': "\\[", + ']': "\\]", + '(': "\\(", + ')': "\\)", + '<': "\\<", + '>': "\\>", + //'#': "\\#", + '\\': "\\\\", + '*': "\\*", + '_': "\\_", + '~': "\\~", + } + + for _, char := range input { + if escapeSeq, ok := reservedChars[char]; ok { + builder.WriteString(escapeSeq) + } else { + builder.WriteRune(char) + } + } + + return builder.String() +} diff --git a/internal/platforms/linkedin/escapes_test.go b/internal/platforms/linkedin/escapes_test.go new file mode 100644 index 0000000..b1f9203 --- /dev/null +++ b/internal/platforms/linkedin/escapes_test.go @@ -0,0 +1,15 @@ +package linkedin + +import ( + "testing" +) + +func TestLinkedInEscapes(t *testing.T) { + var ( + input = `This is a test message with special characters: " {} @ [] () <> # \ * _ ~ |` + expected = `This is a test message with special characters: \" \{\} @ \[\] \(\) \<\> # \\ \* \_ \~ \|` + ) + if escaped := escapeLinkedInText(input); escaped != expected { + t.Errorf("expected '%s' but got '%s'", expected, escaped) + } +} diff --git a/internal/platforms/linkedin/linkedin.go b/internal/platforms/linkedin/linkedin.go index c283fbc..f6d89f5 100644 --- a/internal/platforms/linkedin/linkedin.go +++ b/internal/platforms/linkedin/linkedin.go @@ -14,6 +14,7 @@ import ( "codeberg.org/snonux/gos/internal/entry" "codeberg.org/snonux/gos/internal/platforms/linkedin/oauth2" "codeberg.org/snonux/gos/internal/prompt" + "github.com/fatih/color" ) var errUnauthorized = errors.New("unauthorized access, refresh or create token?") @@ -58,7 +59,7 @@ func callLinkedInAPI(ctx context.Context, personID, accessToken, content string) post := map[string]interface{}{ "author": fmt.Sprintf("urn:li:person:%s", personID), - "commentary": content, // TODO: Can't post (...) paretenthesis? escape them? TEST AGAIN! + "commentary": escapeLinkedInText(content), "visibility": "PUBLIC", "distribution": map[string]interface{}{ "feedDistribution": "MAIN_FEED", @@ -88,15 +89,17 @@ func callLinkedInAPI(ctx context.Context, personID, accessToken, content string) return fmt.Errorf("Error sending request: %w", err) } defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + if err != nil { + return err + } + color.Cyan(string(body)) if resp.StatusCode != http.StatusCreated { - body, _ := io.ReadAll(resp.Body) - err = fmt.Errorf("failed to post to LinkedIn. Status: %s\n%s\n", resp.Status, body) + err = fmt.Errorf("failed to post to LinkedIn. Status: %s\n", resp.Status) if resp.StatusCode == http.StatusUnauthorized { err = errors.Join(err, errUnauthorized) } } return err } - -// TODO: Implement Gemini output? |
