finished first version
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
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") // name of config file (without extension)
|
||||
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
|
||||
}
|
||||
|
||||
cli := pexels.NewClient(viper.GetString("PexelsAPIKey"))
|
||||
ps, err := cli.PhotoService.Search(context.Background(), &pexels.PhotoParams{
|
||||
Query: "neoclassical architecture",
|
||||
PerPage: 80,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
ps.Page = rand.Intn(79)
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
photoIndex := rand.Intn(79)
|
||||
photo := ps.Photos[photoIndex]
|
||||
|
||||
// fmt.Println("Source Medium URL:", ps.Photos[0].Src.Medium)
|
||||
photoURL := photo.Src.Original
|
||||
mime := strings.Split(photoURL, "/")
|
||||
ext := mime[len(mime)-1]
|
||||
fileName := "images/" + fmt.Sprint(photo.ID) + "." + fmt.Sprint(ext)
|
||||
os.Mkdir("images", 0700)
|
||||
err = downloadFile(photoURL, fileName)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println(ps.TotalResults, "Original SRC URL: ", photoURL)
|
||||
|
||||
post := "Taken from " + photo.URL + "\n#neoclassical"
|
||||
|
||||
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)
|
||||
}
|
||||
var attachmentArr []mastodon.ID
|
||||
attachmentArr = append(attachmentArr, attachment.ID)
|
||||
status := &mastodon.Toot{
|
||||
Status: post,
|
||||
MediaIDs: attachmentArr,
|
||||
}
|
||||
c.PostStatus(context.Background(), status)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user