You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
3.8 KiB

package main
import (
"context"
"errors"
"fmt"
"io"
"log"
"math/rand"
"net/http"
"os"
"strings"
"time"
"github.com/kosa3/pexels-go"
"github.com/mattn/go-mastodon"
"github.com/spf13/viper"
)
func main() {
architectureBot()
}
func architectureBot() {
viper.AddConfigPath(".")
viper.AddConfigPath("..")
viper.SetConfigName("architectureBot.yaml")
viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("fatal error config file: %w", err))
}
type fedi struct {
clientID string
clientSecret string
email string
password string
}
r := rand.New(rand.NewSource(time.Now().Unix()))
queryArr := viper.GetStringSlice("queries")
queryArrLen := len(queryArr)
queryIndex := r.Int31n(int32(queryArrLen))
query := queryArr[queryIndex]
apiKey := viper.GetString("PexelsAPIKey")
cli := pexels.NewClient(apiKey)
ps, err := cli.PhotoService.Search(context.Background(), &pexels.PhotoParams{
Query: query,
PerPage: 80,
Page: rand.Intn(100),
})
if err != nil {
log.Fatal(err)
}
photoIndex := rand.Intn(79)
photo := ps.Photos[photoIndex]
hashtagString := "#"
hashtagArr := strings.Split(query, " ")
for _, v := range hashtagArr {
hashtagString += v
}
// fmt.Println(hashtagString)
// fmt.Println("Source Medium URL:", ps.Photos[0].Src.Medium)
photoURL := photo.Src.Original
mime := strings.Split(photoURL, "/")
name := mime[len(mime)-1]
fileName := "images/" + fmt.Sprint(name)
os.Mkdir("images", 0700)
err = downloadFile(photoURL, fileName)
if err != nil {
log.Fatal(err)
}
post := hashtagString + "\n" + photo.URL
domain := "https://" + viper.GetString("domain")
if !viper.IsSet("NCD.ClientID") {
app, err := mastodon.RegisterApp(context.Background(), &mastodon.AppConfig{
Server: domain,
ClientName: viper.GetString("botName"),
Scopes: "read write follow",
})
if err != nil {
log.Fatal(err)
}
viper.Set("NCD.ClientID", app.ClientID)
viper.Set("NCD.ClientSecret", app.ClientSecret)
viper.WriteConfig()
}
ncdConfig := fedi{
clientID: viper.GetString("NCD.ClientID"),
clientSecret: viper.GetString("NCD.ClientSecret"),
email: viper.GetString("NCD.Email"),
password: viper.GetString("NCD.password"),
}
c := mastodon.NewClient(&mastodon.Config{
Server: domain,
ClientID: ncdConfig.clientID,
ClientSecret: ncdConfig.clientSecret,
})
err2 := c.Authenticate(context.Background(), ncdConfig.email, ncdConfig.password)
if err2 != nil {
log.Fatal(err)
}
attachment, err4 := c.UploadMedia(context.Background(), fileName)
if err4 != nil {
fmt.Println(err4)
}
f, err := os.OpenFile("architectureBot.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
log.SetOutput(f)
log.Print("page: ", fmt.Sprint(ps.Page))
log.Print("hashtag: ", hashtagString)
log.Print("Index: ", photoIndex)
log.Print("name: ", fmt.Sprint(name))
var attachmentArr []mastodon.ID
attachmentArr = append(attachmentArr, attachment.ID)
status := &mastodon.Toot{
Status: post,
MediaIDs: attachmentArr,
}
c.PostStatus(context.Background(), status)
defer f.Close()
os.Remove(fileName)
}
func downloadFile(URL, fileName string) error {
//Get the response bytes from the url
response, err := http.Get(URL)
if err != nil {
return err
}
defer response.Body.Close()
if response.StatusCode != 200 {
return errors.New("received non 200 response code")
}
//Create a empty file
file, err := os.Create(fileName)
if err != nil {
return err
}
defer file.Close()
//Write the bytes to the fiel
_, err = io.Copy(file, response.Body)
if err != nil {
return err
}
return nil
}