related images and posts
This commit is contained in:
parent
5119490b3d
commit
c62667578c
2
main.go
2
main.go
@ -6,10 +6,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// WP Config
|
||||||
const baseUrl = "https://slowtwitch.cloud/"
|
const baseUrl = "https://slowtwitch.cloud/"
|
||||||
const wordpressKey = "admin@slowtwitch.cloud"
|
const wordpressKey = "admin@slowtwitch.cloud"
|
||||||
const wordpressSecret = "6zY7 xsKZ dGIt l1Lp ypIK 6TWh"
|
const wordpressSecret = "6zY7 xsKZ dGIt l1Lp ypIK 6TWh"
|
||||||
|
|
||||||
|
// DB Config
|
||||||
const slowtwitchAdminUser = "admin"
|
const slowtwitchAdminUser = "admin"
|
||||||
const slowtwitchAdminPass = "yxnh93Ybbz2Nm8#mp28zCVv"
|
const slowtwitchAdminPass = "yxnh93Ybbz2Nm8#mp28zCVv"
|
||||||
const slowtwitchDbName = "slowtwitch"
|
const slowtwitchDbName = "slowtwitch"
|
||||||
|
@ -18,7 +18,7 @@ type MigratePosts struct {
|
|||||||
WordpressPassword string
|
WordpressPassword string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (migration MigratePosts) Execute() {
|
func (migration MigratePosts) Execute() []PostResult {
|
||||||
slowtwitchPostIds, err := slowtwitch.GetAllPostIds(migration.SlowtwitchDatabase)
|
slowtwitchPostIds, err := slowtwitch.GetAllPostIds(migration.SlowtwitchDatabase)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("Could not migrate posts: " + err.Error())
|
panic("Could not migrate posts: " + err.Error())
|
||||||
@ -36,7 +36,7 @@ func (migration MigratePosts) Execute() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
slowtwitchPostIdsForMigration := getPostIdsThatNeedMigration(slowtwitchPostIds, migratedPostIds)
|
slowtwitchPostIdsForMigration := getPostIdsThatNeedMigration(slowtwitchPostIds, migratedPostIds)
|
||||||
|
var postResults []PostResult
|
||||||
for _, postId := range slowtwitchPostIdsForMigration {
|
for _, postId := range slowtwitchPostIdsForMigration {
|
||||||
errorMessage := ""
|
errorMessage := ""
|
||||||
//migrate
|
//migrate
|
||||||
@ -220,21 +220,42 @@ func (migration MigratePosts) Execute() {
|
|||||||
fmt.Println("Error creating redirect for", postId, ":"+postRedirectErr.Error())
|
fmt.Println("Error creating redirect for", postId, ":"+postRedirectErr.Error())
|
||||||
}
|
}
|
||||||
fmt.Println("Successfully created post and result for", postId)
|
fmt.Println("Successfully created post and result for", postId)
|
||||||
// TODO Update advanced Custom Fields with images
|
updateAcfImages(imageResults, post.Id, migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword)
|
||||||
//Install ACF and create field
|
postResults = append(postResults, postResult)
|
||||||
/*
|
|
||||||
"acf": {
|
|
||||||
"post_images": [...ids]
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
// TODO Update related posts (get from post results db) as second loop
|
// Update related posts once work is done
|
||||||
//Install ACF and create field
|
for _, postResult := range postResults {
|
||||||
/*
|
if postResult.IsSuccess {
|
||||||
"acf": {
|
var relatedWordpressIds []int
|
||||||
"related_posts": [...ids]
|
|
||||||
|
relatedSlowtwitchIds, slowtwitchIdsErr := slowtwitch.GetRelatedArticleIds(postResult.SlowtwitchId, migration.SlowtwitchDatabase)
|
||||||
|
|
||||||
|
if slowtwitchIdsErr != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, slowtwitchRelatedId := range relatedSlowtwitchIds {
|
||||||
|
wordpressRelatedId, wordpressIdErr := GetWordpressPostIdBySlowtwitchPostId(slowtwitchRelatedId, migration.ResultsDatabase)
|
||||||
|
if wordpressIdErr != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
relatedSlowtwitchIds = append(relatedWordpressIds, wordpressRelatedId)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(relatedWordpressIds) > 0 {
|
||||||
|
updateWordpressRelatedPosts := wordpress.UpdateAcfRelatedPosts{
|
||||||
|
Acf: wordpress.AcfRelatedPosts{
|
||||||
|
PostIds: relatedWordpressIds,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
updateRelatedPostErr := updateWordpressRelatedPosts.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword, postResult.WordpressId)
|
||||||
|
if updateRelatedPostErr != nil {
|
||||||
|
fmt.Println("Error updating wordpressRelatedPosts", updateRelatedPostErr.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
*/
|
}
|
||||||
|
|
||||||
|
return postResults
|
||||||
}
|
}
|
||||||
|
|
||||||
func getPostIdsThatNeedMigration(slowtwitchPostIds, migratedPostIds []int) []int {
|
func getPostIdsThatNeedMigration(slowtwitchPostIds, migratedPostIds []int) []int {
|
||||||
@ -285,3 +306,29 @@ func createImageFailureResult(url string, resultsDatabase *sql.DB) {
|
|||||||
fmt.Println("Failed to create failure result: ", err)
|
fmt.Println("Failed to create failure result: ", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getSuccessfulWordpressImageIds(imageResults []ImageResult) []int {
|
||||||
|
var output []int
|
||||||
|
|
||||||
|
for _, imageResult := range imageResults {
|
||||||
|
output = append(output, imageResult.WordpressId)
|
||||||
|
}
|
||||||
|
|
||||||
|
return output
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateAcfImages(imageResults []ImageResult, postId int, baseUrl, user, pass string) {
|
||||||
|
if len(imageResults) > 0 {
|
||||||
|
wordpressImageIds := getSuccessfulWordpressImageIds(imageResults)
|
||||||
|
updateAcfImages := wordpress.UpdateAcfImages{
|
||||||
|
Acf: wordpress.AcfImages{
|
||||||
|
PostImages: wordpressImageIds,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
acfImagesErr := updateAcfImages.Execute(baseUrl, user, pass, postId)
|
||||||
|
|
||||||
|
if acfImagesErr != nil {
|
||||||
|
fmt.Println("Error updating acf images for post", postId, ":", acfImagesErr.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -24,3 +24,12 @@ func CreatePostResult(parameters PostResult, db *sql.DB) (int, error) {
|
|||||||
return int(id), nil
|
return int(id), nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetWordpressPostIdBySlowtwitchPostId(slowtwitchPostId int, db *sql.DB) (int, error) {
|
||||||
|
var wordpressId int
|
||||||
|
|
||||||
|
post := db.QueryRow("select WordpressId from PostResults where SlowtwitchId = (?)", slowtwitchPostId)
|
||||||
|
err := post.Scan(&wordpressId)
|
||||||
|
|
||||||
|
return wordpressId, err
|
||||||
|
}
|
||||||
|
26
services/slowtwitch/get-related-post-ids.go
Normal file
26
services/slowtwitch/get-related-post-ids.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package slowtwitch
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetRelatedArticleIds(postId int, db *sql.DB) ([]int, error) {
|
||||||
|
var spaceSeparatedIds string
|
||||||
|
rows := db.QueryRow("SELECT RelatedArticles FROM slowtwitch.glinks_Links where ID = ?", postId)
|
||||||
|
err := rows.Scan(&spaceSeparatedIds)
|
||||||
|
if err != nil {
|
||||||
|
return make([]int, 0), err
|
||||||
|
}
|
||||||
|
|
||||||
|
var ids []int
|
||||||
|
idsAsStrings := strings.Split(spaceSeparatedIds, " ")
|
||||||
|
for _, stringId := range idsAsStrings {
|
||||||
|
id, err := strconv.Atoi(stringId)
|
||||||
|
if err == nil {
|
||||||
|
ids = append(ids, id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ids, nil
|
||||||
|
}
|
20
services/wordpress/update-acf-images.go
Normal file
20
services/wordpress/update-acf-images.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package wordpress
|
||||||
|
|
||||||
|
import (
|
||||||
|
"federated.computer/wp-sync-slowtwitch/utilities"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UpdateAcfImages struct {
|
||||||
|
Acf AcfImages `json:"acf"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type AcfImages struct {
|
||||||
|
PostImages []int `json:"post_images"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (parameters *UpdateAcfImages) Execute(baseUrl, user, pass string, postId int) error {
|
||||||
|
endpoint := baseUrl + "wp-json/wp/v2/posts/" + strconv.Itoa(postId)
|
||||||
|
_, err := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters)
|
||||||
|
return err
|
||||||
|
}
|
20
services/wordpress/update-acf-related-posts.go
Normal file
20
services/wordpress/update-acf-related-posts.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package wordpress
|
||||||
|
|
||||||
|
import (
|
||||||
|
"federated.computer/wp-sync-slowtwitch/utilities"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UpdateAcfRelatedPosts struct {
|
||||||
|
Acf AcfRelatedPosts `json:"acf"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type AcfRelatedPosts struct {
|
||||||
|
PostIds []int `json:"related_posts"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (parameters *UpdateAcfRelatedPosts) Execute(baseUrl, user, pass string, postId int) error {
|
||||||
|
endpoint := baseUrl + "wp-json/wp/v2/posts/" + strconv.Itoa(postId)
|
||||||
|
_, err := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters)
|
||||||
|
return err
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user