74 lines
1.7 KiB
Go
74 lines
1.7 KiB
Go
// notes dumps the /msg/pms/ inbox listing and, if an argument is given,
|
|
// prints the full body of that note id. Requires FA_A / FA_B in env.
|
|
//
|
|
// go run ./examples/notes # list inbox
|
|
// go run ./examples/notes 131012623 # read one note
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"strconv"
|
|
|
|
fa "git.anthrove.art/public/go-fa-api"
|
|
)
|
|
|
|
func main() {
|
|
a, b := os.Getenv("FA_A"), os.Getenv("FA_B")
|
|
if a == "" || b == "" {
|
|
log.Fatal("FA_A and FA_B must be set notes require login")
|
|
}
|
|
client := fa.New(
|
|
fa.WithCookies(fa.Cookies{A: a, B: b}),
|
|
fa.WithCloudflare(fa.CFCookies{Clearance: os.Getenv("CF_CLEARANCE")}),
|
|
fa.WithUserAgent(envOr("FA_UA", "go-fa-api-example/0.1")),
|
|
)
|
|
ctx := context.Background()
|
|
|
|
if len(os.Args) >= 2 {
|
|
id, err := strconv.ParseInt(os.Args[1], 10, 64)
|
|
if err != nil {
|
|
log.Fatalf("invalid note id: %v", err)
|
|
}
|
|
n, err := client.GetNote(ctx, fa.NoteID(id))
|
|
if err != nil {
|
|
log.Fatalf("GetNote: %v", err)
|
|
}
|
|
fmt.Printf("Subject: %s\nFrom: %s (@%s)\nTo: %s (@%s)\nSent: %s\n\n%s\n",
|
|
n.Subject,
|
|
n.From.DisplayName, n.From.Name,
|
|
n.To.DisplayName, n.To.Name,
|
|
n.SentAt.Format("2006-01-02 15:04"),
|
|
n.BodyText)
|
|
return
|
|
}
|
|
|
|
count := 0
|
|
for np, err := range client.Notes(ctx, fa.ListOptions{MaxPages: 1}) {
|
|
if err != nil {
|
|
log.Fatalf("Notes: %v", err)
|
|
}
|
|
count++
|
|
unread := " "
|
|
if np.Unread {
|
|
unread = "*"
|
|
}
|
|
from := np.Sender.DisplayName
|
|
if from == "" {
|
|
from = np.Sender.Name
|
|
}
|
|
fmt.Printf("[%s] [%d] %s from %s (%s)\n",
|
|
unread, np.ID, np.Subject, from, np.SentAt.Format("2006-01-02 15:04"))
|
|
}
|
|
fmt.Printf("\n%d notes on first page\n", count)
|
|
}
|
|
|
|
func envOr(key, fallback string) string {
|
|
if v := os.Getenv(key); v != "" {
|
|
return v
|
|
}
|
|
return fallback
|
|
}
|