aboutsummaryrefslogtreecommitdiff
path: root/web/mastodon/examples/photoupload.go
blob: 0bd3edc91deb64b614a1f816615811f63d68e2b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//go:build ignore

package main

import (
	"bufio"
	"context"
	"fmt"
	"log"
	"os"
	"strings"

	"within.website/x/web/mastodon"
)

func promptInput(prompt string) (string, error) {
	fmt.Fprint(os.Stderr, prompt)
	fmt.Fprint(os.Stderr, ": ")
	reader := bufio.NewReader(os.Stdin)
	// ReadString will block until the delimiter is entered
	input, err := reader.ReadString('\n')
	if err != nil {
		return "", err
	}

	// remove the delimeter from the string
	input = strings.TrimSuffix(input, "\n")
	return input, nil
}

func main() {
	instance, err := promptInput("URL of the mastodon server (incl https://)")
	if err != nil {
		log.Fatal(err)
	}

	token, err := promptInput("Mastodon token")
	if err != nil {
		log.Fatal(err)
	}

	cli, err := mastodon.Authenticated("Xe/x test", "https://within.website/.x.botinfo", instance, token)
	if err != nil {
		log.Fatal(err)
	}

	if err := cli.VerifyCredentials(context.Background()); err != nil {
		log.Fatal(err)
	}

	fmt.Println("your token works!")

	fname, err := promptInput("path to a jpeg")
	if err != nil {
		log.Fatal(err)
	}

	fin, err := os.Open(fname)
	if err != nil {
		log.Fatal(err)
	}
	defer fin.Close()

	description, err := promptInput("description of image")
	if err != nil {
		log.Fatal(err)
	}

	att, err := cli.UploadMedia(context.Background(), fin, fname, description, "")
	if err != nil {
		log.Fatal(err)
	}

	statusText, err := promptInput("toot")
	if err != nil {
		log.Fatal(err)
	}

	st, err := cli.CreateStatus(context.Background(), mastodon.CreateStatusParams{
		Status:   statusText,
		MediaIDs: []string{att.ID},
	})
	if err != nil {
		log.Fatal(err)
	}

	log.Println(st.URL)
}