sketch of submitting external file to WP

This commit is contained in:
Ross Trottier 2024-05-08 16:06:15 -06:00
parent 6f1b4a7fe9
commit 03c1e1c6ae
3 changed files with 27 additions and 12 deletions

29
main.go
View File

@ -1,8 +1,11 @@
package main
import (
"federated.computer/wp-sync-slowtwitch/services/wordpress"
"bytes"
"federated.computer/wp-sync-slowtwitch/utilities"
"fmt"
"io"
"net/http"
)
const baseUrl = "https://go-api-playground.local/wp-json/wp/v2/"
@ -14,19 +17,21 @@ var appCache AppCache
func main() {
//Get category and cache
//Get single category from cache by name
//Get tag and cache
//Get single tag from cache by name
//Get photo and cache with URL
//Create Category (with parent) + add to cache
//Create Tag + add to cache
//Upload Photo + add to cache
//Use cached data to correctly build post form submissions
tag, ok := wordpress.GetTag("golang-tag", baseUrl, wordpressKey, wordpressSecret)
if ok {
appCache.SetTag(tag)
fmt.Println("Found tag:", tag.Name)
} else {
fmt.Println("Could not find tag.")
}
resp, err := http.Get("https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjPm2ryEM65R2LDKe1ov_2ThSg1Po44Pe6ybLGa9hEeKU9qLptSeYVc_ahRzA3ZiKqm73fdgYGCq2ZsfJTLPKjfCqkNtKF6xYwYfRTje3o2jH9VEKSvLyWZwCctQGHC7zREUBnJFZlI0A/s1600/Dogs+Puppies+Background+wallpaper+%25284%2529.jpg")
utilities.CheckError(err)
defer utilities.CloseBodyAndCheckError(resp.Body)
body, err := io.ReadAll(resp.Body)
utilities.CheckError(err)
request, err := http.NewRequest("POST", "https://go-api-playground.local/wp-json/wp/v2/media", bytes.NewReader(body))
request.Header.Set("Content-Disposition", `attachment;filename="Dogs+Puppies+Background+wallpaper+%25284%2529.jpg"`)
request.SetBasicAuth(wordpressKey, wordpressSecret)
rsp, err := http.DefaultClient.Do(request)
utilities.CheckError(err)
result, err := io.ReadAll(rsp.Body)
utilities.CheckError(err)
fmt.Println(rsp.StatusCode, string(result))
}

View File

@ -0,0 +1,9 @@
package wordpress
type CreateImage struct {
Date string `json:"date"`
Title string `json:"title"`
}
type CreateImageResponse struct {
}

View File

@ -15,6 +15,7 @@ type CreatePost struct {
Status string `json:"status"`
Categories []int `json:"categories"`
Slug string `json:"slug"`
Date string `json:"date"`
}
type CreatePostResponse struct {