50 lines
1.1 KiB
Go
50 lines
1.1 KiB
Go
package placement
|
|
|
|
import (
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
var threadsPostURLRe = regexp.MustCompile(`(?i)threads\.(?:com|net)/@([^/]+)/post/([^/?#]+)`)
|
|
|
|
type ParsedThreadsPost struct {
|
|
Permalink string
|
|
ExternalID string
|
|
Author string
|
|
Text string
|
|
}
|
|
|
|
func ParseThreadsPostFromWebResult(title, snippet, url string) (ParsedThreadsPost, bool) {
|
|
permalink := normalizeThreadsPermalink(url)
|
|
if permalink == "" {
|
|
return ParsedThreadsPost{}, false
|
|
}
|
|
match := threadsPostURLRe.FindStringSubmatch(permalink)
|
|
if len(match) < 3 {
|
|
return ParsedThreadsPost{}, false
|
|
}
|
|
text := strings.TrimSpace(strings.Join([]string{strings.TrimSpace(title), strings.TrimSpace(snippet)}, " — "))
|
|
if len([]rune(text)) < 8 {
|
|
return ParsedThreadsPost{}, false
|
|
}
|
|
return ParsedThreadsPost{
|
|
Permalink: permalink,
|
|
ExternalID: match[2],
|
|
Author: match[1],
|
|
Text: text,
|
|
}, true
|
|
}
|
|
|
|
func normalizeThreadsPermalink(raw string) string {
|
|
raw = strings.TrimSpace(raw)
|
|
if raw == "" {
|
|
return ""
|
|
}
|
|
raw = strings.Split(raw, "?")[0]
|
|
raw = strings.Split(raw, "#")[0]
|
|
if threadsPostURLRe.MatchString(raw) {
|
|
return raw
|
|
}
|
|
return ""
|
|
}
|