From 03c1e1c6aebad054b1de386f63cde5fe79c16789 Mon Sep 17 00:00:00 2001 From: Ross Trottier Date: Wed, 8 May 2024 16:06:15 -0600 Subject: [PATCH] sketch of submitting external file to WP --- main.go | 29 +++++++++++++++++------------ services/wordpress/create-image.go | 9 +++++++++ services/wordpress/create-post.go | 1 + 3 files changed, 27 insertions(+), 12 deletions(-) create mode 100644 services/wordpress/create-image.go diff --git a/main.go b/main.go index 2ffdea5..7639c73 100644 --- a/main.go +++ b/main.go @@ -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)) } diff --git a/services/wordpress/create-image.go b/services/wordpress/create-image.go new file mode 100644 index 0000000..5c7192e --- /dev/null +++ b/services/wordpress/create-image.go @@ -0,0 +1,9 @@ +package wordpress + +type CreateImage struct { + Date string `json:"date"` + Title string `json:"title"` +} + +type CreateImageResponse struct { +} diff --git a/services/wordpress/create-post.go b/services/wordpress/create-post.go index c24d3fc..a6c9165 100644 --- a/services/wordpress/create-post.go +++ b/services/wordpress/create-post.go @@ -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 {