aboutsummaryrefslogtreecommitdiffstats
path: root/cheetah-bot.go
blob: e605a680a5aa6800bf2c9d795174be2d4bd65b4a (plain) (blame)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// this is about the sloppiest code i have ever written in my life, i would not
// recommend running this yourself.
package main

import (
	"bufio"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"time"

	/*
		// imgur stuff
		"github.com/koffeinsource/go-imgur"
		"github.com/koffeinsource/go-klogger"
		"io/ioutil"
		"log"
		"net/http"
	*/

	"github.com/turnage/graw/reddit"

	"github.com/McKael/madon"
)

func main() {
	// prepare day file
	df, err := os.OpenFile("/var/local/cheetahs/day.txt", os.O_RDWR|os.O_CREATE, 0644)
	if err != nil {
		fmt.Println("Failed to open file: ", err)
		os.Exit(1)
	}
	defer df.Close()

	// read in current day
	var day int
	_, err = fmt.Fscanf(df, "%d", &day)
	if err != nil {
		if err == io.EOF {
			// default to day 1 if day file was just created by os.O_CREATE
			day = 1
		} else {
			fmt.Println("Failed to read day: ", err)
			os.Exit(1)
		}
	}

	filename := fmt.Sprintf("/var/local/cheetahs/D%03d.jpg", day)
	descriptionFilename := fmt.Sprintf("/var/local/cheetahs/D%03d.desc", day)

	description, err := ioutil.ReadFile(descriptionFilename)
	if err != nil {
		fmt.Println("Failed to read description: ", err)
		os.Exit(1)
	}

	reddit, err := reddit.NewBotFromAgentFile("/var/local/cheetahs/cheetah-reddit.agent", 0)
	if err != nil {
		fmt.Println("Failed to create bot handle: ", err)
		os.Exit(1)
	}

	mastoToken := new(madon.UserToken)
	var mastoId, mastoSecret string

	mf, err := os.Open("/var/local/cheetahs/cheetah-masto.agent")
	if err != nil {
		fmt.Println("Failed to open file: ", err)
		os.Exit(1)
	}
	defer mf.Close()
	_, err = fmt.Fscanf(mf, "App ID: %s\nApp Secret: %s\nUser Token: %s", &mastoId, &mastoSecret, &mastoToken.AccessToken)
	if err != nil {
		fmt.Println("Failed to read mastodon authentication: ", err)
		os.Exit(1)
	}

	masto, err := madon.RestoreApp("cheetah-bot", "botsin.space", mastoId, mastoSecret, mastoToken)

	/*
		// create imgur client and upload to imgur
		imgur := new(imgur.Client)
		imgur.HTTPClient = new(http.Client)
		imgur.Log = new(klogger.CLILogger)
		log.SetOutput(ioutil.Discard)
		imgur.ImgurClientID = "9657b96ca0b7f38" // definitely not a random client id pulled off of github
		imgurStruct, st, err := imgur.UploadImageFromFile(filename, "", "", "")
		if st != 200 || err != nil {
			fmt.Printf("Failed to upload to imgur: %v (status %v)\n", err, st)
			os.Exit(1)
		}
		imgurImage := imgurStruct.Link
	*/

	mastoImage, err := masto.UploadMedia(filename, "", "")
	if err != nil {
		fmt.Println("Failed to upload image to mastodon: ", err)
		os.Exit(1)
	}

	titleReddit := fmt.Sprintf("Posting cheetah pics every day until I run out — Day %d — %s", day, description)
	titleMasto := fmt.Sprintf("Day %d: %s\n#cheetah\n", day, description)

	curYear, curMonth, curDay := time.Now().Date()
	titleGemini := "# Cheetah a Day\n\n"
	linkGemini := fmt.Sprintf("=> D%03d.jpg %04d-%02d-%02d - Day %[4]d: %s\n", curYear, curMonth, curDay, day, description)

	// do the final culminating posts
	geminiOrig, err := os.Open("/var/local/cheetahs/cheetahs.gmi")
	if err != nil {
		fmt.Println("Failed to open file: ", err)
		os.Exit(1)
	}
	defer geminiOrig.Close()
	geminiNew, err := os.OpenFile("/var/local/cheetahs/cheetahs.gmi.new", os.O_RDWR|os.O_CREATE, 0666)
	if err != nil {
		fmt.Println("Failed to open file: ", err)
		os.Exit(1)
	}
	defer geminiNew.Close()
	reader := bufio.NewReader(geminiOrig)
	writer := bufio.NewWriter(geminiNew)
	io.WriteString(geminiNew, titleGemini)
	io.WriteString(geminiNew, linkGemini)
	_, _ = reader.ReadString('\n')
	_, _ = reader.ReadString('\n')
	_, err = io.Copy(writer, reader)
	if err != nil {
		fmt.Println("Failed to update gemini file: ", err)
		os.Exit(1)
	}

	//reddit.PostLink("nytpu", titleReddit, imgurImage)
	reddit.PostLink("Cheetahs", titleReddit, mastoImage.URL)
	masto.PostStatus(titleMasto, 0, []int64{mastoImage.ID}, false, "", "public")

	// write day.txt for next day
	df.Seek(0, io.SeekStart)
	_, err = fmt.Fprintf(df, "%03d\n", day+1)
	if err != nil {
		fmt.Printf("Failed to update file to day %03d: %v", day+1, err)
		os.Exit(1)
	}

	err = os.Remove(filename)
	if err != nil {
		fmt.Printf("Failed to remove \"%v\": %v", filename, err)
	}
	err = os.Remove(descriptionFilename)
	if err != nil {
		fmt.Printf("Failed to remove \"%v\": %v", filename, err)
	}
}