Something went wrong. Try again.
code for barking about things from a raspberry pi 1 b+.
Something went wrong. Try again.
woof.codingpa.ws main.go
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101package 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}