get categories and cache
This commit is contained in:
parent
b59d36f2c2
commit
c0dacf4fb7
21
app-cache.go
21
app-cache.go
@ -3,8 +3,9 @@ package main
|
|||||||
import "federated.computer/wp-sync-slowtwitch/services/wordpress"
|
import "federated.computer/wp-sync-slowtwitch/services/wordpress"
|
||||||
|
|
||||||
type AppCache struct {
|
type AppCache struct {
|
||||||
UsersCache map[string]wordpress.UserData
|
UsersCache map[string]wordpress.UserData
|
||||||
TagCache map[string]wordpress.TagData
|
TagCache map[string]wordpress.TagData
|
||||||
|
CategoryCache map[string]wordpress.CategoryData
|
||||||
}
|
}
|
||||||
|
|
||||||
func (appCache *AppCache) GetUser(username string) (wordpress.UserData, bool) {
|
func (appCache *AppCache) GetUser(username string) (wordpress.UserData, bool) {
|
||||||
@ -30,3 +31,19 @@ func (appCache *AppCache) SetTag(tag wordpress.TagData) {
|
|||||||
}
|
}
|
||||||
appCache.TagCache[tag.Name] = tag
|
appCache.TagCache[tag.Name] = tag
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (appCache *AppCache) GetCategory(categoryName string) (wordpress.CategoryData, bool) {
|
||||||
|
if appCache.CategoryCache != nil {
|
||||||
|
category, ok := appCache.CategoryCache[categoryName]
|
||||||
|
return category, ok
|
||||||
|
}
|
||||||
|
|
||||||
|
return wordpress.CategoryData{}, false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (appCache *AppCache) SetCategory(category wordpress.CategoryData) {
|
||||||
|
if appCache.CategoryCache == nil {
|
||||||
|
appCache.CategoryCache = make(map[string]wordpress.CategoryData)
|
||||||
|
}
|
||||||
|
appCache.CategoryCache[category.Name] = category
|
||||||
|
}
|
||||||
|
38
main.go
38
main.go
@ -12,15 +12,33 @@ const wordpressSecret = "S34E keY1 A1uX 6ncs Rx4T f21W"
|
|||||||
var appCache AppCache
|
var appCache AppCache
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
//Get category and cache
|
// TODO Create Category with or without parent + add to cache
|
||||||
//Get single category from cache by name
|
// TODO Use cached data to correctly build post form submissions
|
||||||
//Get photo and cache with URL
|
|
||||||
//Create Category (with parent) + add to cache
|
categoryNames := []string{"testcat", "testcat3", "you wont find me"}
|
||||||
//Upload Photo + add to cache
|
|
||||||
//Use cached data to correctly build post form submissions
|
for _, categoryName := range categoryNames {
|
||||||
createImage := wordpress.CreateImage{
|
categoryData, ok := wordpress.GetCategory(categoryName, baseUrl, wordpressKey, wordpressSecret)
|
||||||
Url: "https://www.slowtwitch.com/articles/images/3/218713-largest_IMG_4527.jpg",
|
|
||||||
|
if ok {
|
||||||
|
appCache.SetCategory(categoryData)
|
||||||
|
fmt.Println(categoryData.Name, categoryData.Id, categoryData.ParentId)
|
||||||
|
} else {
|
||||||
|
fmt.Println("not found on website: ", categoryName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, categoryName := range categoryNames {
|
||||||
|
category, ok := appCache.GetCategory(categoryName)
|
||||||
|
|
||||||
|
if ok {
|
||||||
|
fmt.Println("Found single in cache: ", category.Name)
|
||||||
|
} else {
|
||||||
|
fmt.Println("Not found in cache:", categoryName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, category := range appCache.CategoryCache {
|
||||||
|
fmt.Println("From Cache: ", category.Name)
|
||||||
}
|
}
|
||||||
imageData := createImage.Execute(baseUrl, wordpressKey, wordpressSecret)
|
|
||||||
fmt.Println(imageData.Link)
|
|
||||||
}
|
}
|
||||||
|
28
services/wordpress/get-category.go
Normal file
28
services/wordpress/get-category.go
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package wordpress
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"federated.computer/wp-sync-slowtwitch/utilities"
|
||||||
|
"net/url"
|
||||||
|
)
|
||||||
|
|
||||||
|
type CategoryData struct {
|
||||||
|
Id int `json:"id"`
|
||||||
|
ParentId int `json:"parent_id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
ParentName string `json:"parent_name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetCategory(name, baseUrl, user, pass string) (CategoryData, bool) {
|
||||||
|
endpoint := baseUrl + "categories?search=" + url.QueryEscape(name)
|
||||||
|
response := utilities.GetHttpRequestToWordpress(endpoint, user, pass)
|
||||||
|
var categoryData []CategoryData
|
||||||
|
err := json.Unmarshal(response, &categoryData)
|
||||||
|
utilities.CheckError(err)
|
||||||
|
for _, category := range categoryData {
|
||||||
|
if category.Name == name {
|
||||||
|
return category, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return CategoryData{}, false
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user