expand a preview card for YouTube links

A message containing a YouTube link now carries a preview card. Discord
and X build their own card from the URL in the text, so the work is on
the bluesky side: the new preview.go asks YouTube's oEmbed endpoint for
the title, channel and thumbnail and posts them as an
app.bsky.embed.external.

Links are found by pulling candidates out of the text and parsing them
with net/url rather than by matching a URL shaped regexp, so a host like
youtube.com.example.invalid is not mistaken for the real thing.

Bluesky allows one embed per post, so a message with both attachments
and a link keeps the attachments. A failed oEmbed lookup only costs the
card, not the post.

Downloading the thumbnail wants the attachment download path, so it is
split into fetch and downloadImage, and both now go through a client
with a timeout: the event loop is single threaded and a hung request
would stall every later message.

The Dockerfile built main.go by name, which stops working now that
package main spans two files; it builds the package instead.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-26 11:56:49 +09:00
parent 1c2ab29404
commit fd14c07ae0
8 changed files with 202 additions and 23 deletions

76
main.go
View File

@@ -11,7 +11,9 @@ import (
"net/http"
"os"
"path"
"regexp"
"strings"
"time"
"tweetdistributor/discord"
"tweetdistributor/output"
"tweetdistributor/store"
@@ -28,6 +30,17 @@ const maxImagesPerPost = 4
// Bluesky rejects blobs over 2,000,000 bytes; Twitter allows up to 5MB.
const maxImageBytes = 2_000_000
// urlPattern picks candidate links out of message text; each one is parsed
// properly before it is judged to be a YouTube link.
var urlPattern = regexp.MustCompile(`https?://[^\s<>"']+`)
// trimURL drops the punctuation that ends the sentence rather than the URL.
func trimURL(match string) string {
return strings.TrimRight(match, ".,!?、。)]}>")
}
var httpClient = &http.Client{Timeout: 30 * time.Second}
// shrinkImage re-encodes (and if necessary downscales) an image until it
// fits within maxImageBytes. Images already small enough pass through
// untouched.
@@ -69,29 +82,44 @@ func shrinkImage(img output.Image) (output.Image, error) {
return output.Image{}, fmt.Errorf("%s could not be shrunk below %d bytes", img.Filename, maxImageBytes)
}
// fetch GETs url and returns its body.
func fetch(url string) ([]byte, error) {
resp, err := httpClient.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("status %s", resp.Status)
}
return data, nil
}
// downloadImage fetches an image and shrinks it to a postable size.
func downloadImage(url, filename, contentType string) (output.Image, error) {
data, err := fetch(url)
if err != nil {
return output.Image{}, fmt.Errorf("downloading %s: %w", filename, err)
}
return shrinkImage(output.Image{
Data: data,
ContentType: contentType,
Filename: filename,
})
}
func downloadImages(attachments []*discordgo.MessageAttachment) ([]output.Image, error) {
var images []output.Image
for _, attachment := range attachments {
if !strings.HasPrefix(attachment.ContentType, "image/") {
continue
}
resp, err := http.Get(attachment.URL)
if err != nil {
return nil, fmt.Errorf("downloading %s: %w", attachment.Filename, err)
}
data, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, fmt.Errorf("downloading %s: %w", attachment.Filename, err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("downloading %s: status %s", attachment.Filename, resp.Status)
}
img, err := shrinkImage(output.Image{
Data: data,
ContentType: attachment.ContentType,
Filename: attachment.Filename,
})
img, err := downloadImage(attachment.URL, attachment.Filename, attachment.ContentType)
if err != nil {
return nil, err
}
@@ -132,6 +160,15 @@ func (dist *distributor) created(event discord.Event) {
return
}
var preview *output.Preview
if videoURL := findYouTubeURL(event.Content); videoURL != "" {
preview, err = youtubePreview(videoURL)
if err != nil {
// The post is still worth making without its card.
fmt.Fprintln(os.Stderr, err)
}
}
// A reply to a message we never distributed becomes a top level post.
var parents store.Refs
if event.ReplyToID != "" {
@@ -141,8 +178,9 @@ func (dist *distributor) created(event discord.Event) {
refs := store.Refs{}
for _, out := range dist.outputs {
post := output.Post{
Text: event.Content,
Images: images,
Text: event.Content,
Images: images,
Preview: preview,
}
if parent, ok := parents[out.GetName()]; ok && !parent.IsZero() {
post.ReplyTo = &parent