package main import ( "context" "flag" "fmt" "log/slog" "math/rand/v2" "os" "time" "github.com/bluesky-social/indigo/api/atproto" "github.com/bluesky-social/indigo/api/bsky" "github.com/bluesky-social/indigo/atproto/atclient" "github.com/bluesky-social/indigo/atproto/syntax" "github.com/bluesky-social/indigo/lex/util" "woof.codingpa.ws/internal/bluesky" "woof.codingpa.ws/woofer" ) var flagForce = flag.Bool("force", false, "forces posting") const PostProbability = 0.1 func main() { flag.Parse() err := run(context.Background()) if err != nil { slog.Error("running", "error", err) os.Exit(1) } } const PostThreshold = time.Hour * 18 func run(ctx context.Context) error { client, err := bluesky.New(ctx) if err != nil { return fmt.Errorf("creating bluesky client: %w", err) } if !*flagForce { latestPost, err := getLatestPost(ctx, client) if err != nil { return fmt.Errorf("getting latest post: %w", err) } if latestPost != nil { lastPostedAt, err := syntax.ParseDatetimeTime(latestPost.Post.IndexedAt) if err != nil { return fmt.Errorf("parsing time of latest post %s: %w", latestPost.Post.Uri, err) } if time.Since(lastPostedAt) < PostThreshold { slog.Info("skipping", "last_posted_at", lastPostedAt.String()) return nil } } slog.Info("heads or tails...") if rand.Float64() >= PostProbability { slog.Info("heads, not posting!") return nil } } message := woofer.Woof() slog.Info("tails, mmhh tails :3", "posting", message) if err := createRecord(ctx, client, message); err != nil { return fmt.Errorf("creating post record: %w", err) } return nil } func createRecord(ctx context.Context, client *atclient.APIClient, message string) error { input := &atproto.RepoCreateRecord_Input{ Collection: "app.bsky.feed.post", Repo: client.AccountDID.String(), Record: &util.LexiconTypeDecoder{ Val: &bsky.FeedPost{ CreatedAt: syntax.DatetimeNow().String(), Langs: []string{"en"}, Text: message, }, }, } _, err := atproto.RepoCreateRecord(ctx, client, input) return err } func getLatestPost(ctx context.Context, client *atclient.APIClient) (*bsky.FeedDefs_FeedViewPost, error) { output, err := bsky.FeedGetAuthorFeed(ctx, client, client.AccountDID.String(), "", "posts_no_replies", false, 1) if err != nil { return nil, err } if len(output.Feed) == 0 { return nil, nil } return output.Feed[0], nil }