this repo has no description

feat(mostliked): use post text and alt text to detect language, fallback to langs

also, drop utility function for creating the draft post

+30 -19
+30 -19
cmd/mostliked/mostliked.go
··· 25 25 const MinLikes = 5 26 26 27 27 type DraftPost struct { 28 - Text string 29 28 Language lingua.Language 30 29 Created int64 31 30 Likes int64 ··· 61 60 } 62 61 } 63 62 64 - func createDraftPost(commit jetstream.Commit) (DraftPost, error) { 65 - var post appbsky.FeedPost 66 - if err := json.Unmarshal(commit.Record, &post); err != nil { 67 - log.Println("error parsing appbsky.FeedPost") 68 - return DraftPost{}, err 69 - } 70 - draft := DraftPost{ 71 - Text: post.Text, 72 - Created: safeTimestamp(post.CreatedAt), 73 - Likes: 0, 74 - } 75 - return draft, nil 76 - } 77 - 78 63 func trimPostsTable(ctx context.Context, queries *mostliked.Queries) { 79 64 ticker := time.NewTicker(1 * time.Minute) 80 65 defer ticker.Stop() ··· 90 75 } 91 76 } 92 77 78 + func findDetectableText(post appbsky.FeedPost) string { 79 + // if we have text, detect against that 80 + // no text but we do have images, detect against first alt text 81 + 82 + if post.Text != "" { 83 + return post.Text 84 + } else if post.Embed.EmbedImages != nil { 85 + for _, image := range post.Embed.EmbedImages.Images { 86 + if image.Alt != "" { 87 + return image.Alt 88 + } 89 + } 90 + } 91 + return "" 92 + } 93 + 93 94 func processEvents(events <-chan []byte) { 94 95 ctx := context.Background() 95 96 ··· 137 138 continue 138 139 } 139 140 if commit.Collection == "app.bsky.feed.post" { 141 + var post appbsky.FeedPost 140 142 postUri := fmt.Sprintf("at://%s/%s/%s", event.Did, commit.Collection, commit.RKey) 141 - draftPost, err := createDraftPost(commit) 142 - if err != nil { 143 + if err := json.Unmarshal(commit.Record, &post); err != nil { 144 + log.Println("error parsing appbsky.FeedPost") 143 145 continue 144 146 } 145 - language, _ := detector.DetectLanguageOf(draftPost.Text) 146 - draftPost.Language = language 147 + draftPost := DraftPost{ 148 + Created: safeTimestamp(post.CreatedAt), 149 + Likes: 0, 150 + } 151 + if text := findDetectableText(post); text != "" { 152 + language, _ := detector.DetectLanguageOf(text) 153 + draftPost.Language = language 154 + } else if len(post.Langs) > 0 { 155 + iso := lingua.GetIsoCode639_1FromValue(post.Langs[0]) 156 + draftPost.Language = lingua.GetLanguageFromIsoCode639_1(iso) 157 + } 147 158 drafts.Set(postUri, draftPost, 30*time.Minute) 148 159 continue 149 160 } else if commit.Collection == "app.bsky.feed.like" {