merged piwigo category code

This commit is contained in:
Philipp Häfelfinger 2019-03-11 21:45:46 +01:00
parent b089bf34ee
commit a194e2f5ae
6 changed files with 163 additions and 176 deletions

View File

@ -4,19 +4,19 @@ import (
"errors"
"fmt"
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/localFileStructure"
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo/category"
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo"
"github.com/sirupsen/logrus"
"path/filepath"
"sort"
)
func getAllCategoriesFromServer(context *appContext) (map[string]*category.PiwigoCategory, error) {
func getAllCategoriesFromServer(context *appContext) (map[string]*piwigo.PiwigoCategory, error) {
logrus.Debugln("Starting GetAllCategories")
categories, err := category.GetAllCategories(context.Piwigo)
categories, err := piwigo.GetAllCategories(context.Piwigo)
return categories, err
}
func synchronizeCategories(context *appContext, filesystemNodes map[string]*localFileStructure.FilesystemNode, existingCategories map[string]*category.PiwigoCategory) error {
func synchronizeCategories(context *appContext, filesystemNodes map[string]*localFileStructure.FilesystemNode, existingCategories map[string]*piwigo.PiwigoCategory) error {
logrus.Infoln("Synchronizing categories...")
missingCategories := findMissingCategories(filesystemNodes, existingCategories)
@ -29,7 +29,7 @@ func synchronizeCategories(context *appContext, filesystemNodes map[string]*loca
return createMissingCategories(context, missingCategories, existingCategories)
}
func findMissingCategories(fileSystem map[string]*localFileStructure.FilesystemNode, existingCategories map[string]*category.PiwigoCategory) []string {
func findMissingCategories(fileSystem map[string]*localFileStructure.FilesystemNode, existingCategories map[string]*piwigo.PiwigoCategory) []string {
missingCategories := make([]string, 0, len(fileSystem))
for _, file := range fileSystem {
@ -50,7 +50,7 @@ func findMissingCategories(fileSystem map[string]*localFileStructure.FilesystemN
return missingCategories
}
func createMissingCategories(context *appContext, missingCategories []string, existingCategories map[string]*category.PiwigoCategory) error {
func createMissingCategories(context *appContext, missingCategories []string, existingCategories map[string]*piwigo.PiwigoCategory) error {
// we sort them to make sure the categories gets created
// in the right order and we have the parent available while creating the children
sort.Strings(missingCategories)
@ -66,12 +66,12 @@ func createMissingCategories(context *appContext, missingCategories []string, ex
}
// create category on piwigo
id, err := category.CreateCategory(context.Piwigo, parentId, name)
id, err := piwigo.CreateCategory(context.Piwigo, parentId, name)
if err != nil {
return errors.New(fmt.Sprintf("Could not create category on piwigo: %s", err))
}
newCategory := category.PiwigoCategory{Id: id, Name: name, ParentId: parentId, Key: categoryKey}
newCategory := piwigo.PiwigoCategory{Id: id, Name: name, ParentId: parentId, Key: categoryKey}
logrus.Println(newCategory)
existingCategories[newCategory.Key] = &newCategory
}
@ -79,7 +79,7 @@ func createMissingCategories(context *appContext, missingCategories []string, ex
return nil
}
func getNameAndParentId(category string, categories map[string]*category.PiwigoCategory) (string, int, error) {
func getNameAndParentId(category string, categories map[string]*piwigo.PiwigoCategory) (string, int, error) {
parentKey := filepath.Dir(category)
_, name := filepath.Split(category)
if name == category {

View File

@ -3,12 +3,11 @@ package app
import (
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/localFileStructure"
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo"
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo/category"
"github.com/sirupsen/logrus"
"sort"
)
func synchronizeImages(context *appContext, fileSystem map[string]*localFileStructure.FilesystemNode, existingCategories map[string]*category.PiwigoCategory) error {
func synchronizeImages(context *appContext, fileSystem map[string]*localFileStructure.FilesystemNode, existingCategories map[string]*piwigo.PiwigoCategory) error {
imageFiles, err := localFileStructure.GetImageList(fileSystem)
if err != nil {
@ -58,7 +57,7 @@ func findMissingImages(context *appContext, imageFiles []*localFileStructure.Ima
return missingFiles, nil
}
func uploadImages(context *appContext, missingFiles []*localFileStructure.ImageNode, existingCategories map[string]*category.PiwigoCategory) error {
func uploadImages(context *appContext, missingFiles []*localFileStructure.ImageNode, existingCategories map[string]*piwigo.PiwigoCategory) error {
// We sort the files by path to populate per category and not random by file
sort.Slice(missingFiles, func(i, j int) bool {

View File

@ -0,0 +1,152 @@
package piwigo
import (
"encoding/json"
"errors"
"fmt"
"github.com/sirupsen/logrus"
"net/url"
"os"
)
type PiwigoCategory struct {
Id int
ParentId int
Name string
Key string
}
type getCategoryListResponse struct {
Status string `json:"stat"`
Result struct {
Categories []struct {
ID int `json:"id"`
Name string `json:"name"`
Comment string `json:"comment,omitempty"`
Permalink string `json:"permalink,omitempty"`
Status string `json:"status,omitempty"`
Uppercats string `json:"uppercats,omitempty"`
GlobalRank string `json:"global_rank,omitempty"`
IDUppercat int `json:"id_uppercat,string,omitempty"`
NbImages int `json:"nb_images,omitempty"`
TotalNbImages int `json:"total_nb_images,omitempty"`
RepresentativePictureID string `json:"representative_picture_id,omitempty"`
DateLast string `json:"date_last,omitempty"`
MaxDateLast string `json:"max_date_last,omitempty"`
NbCategories int `json:"nb_categories,omitempty"`
URL string `json:"url,omitempty"`
TnURL string `json:"tn_url,omitempty"`
} `json:"categories"`
} `json:"result"`
}
type createCategoryResponse struct {
Status string `json:"stat"`
Err int `json:"err"`
Message string `json:"message"`
Result struct {
Info string `json:"info"`
ID int `json:"id"`
} `json:"result"`
}
func GetAllCategories(context *PiwigoContext) (map[string]*PiwigoCategory, error) {
formData := url.Values{}
formData.Set("method", "pwg.categories.getList")
formData.Set("recursive", "true")
response, err := context.PostForm(formData)
if err != nil {
return nil, err
}
defer response.Body.Close()
var statusResponse getCategoryListResponse
if err := json.NewDecoder(response.Body).Decode(&statusResponse); err != nil {
logrus.Errorln(err)
return nil, err
}
if statusResponse.Status != "ok" {
logrus.Errorf("Got state %s while loading categories", statusResponse.Status)
return nil, errors.New("Could not load categories")
}
logrus.Infof("Successfully got all categories")
categories := buildCategoryMap(&statusResponse)
buildCategoryKeys(categories)
categoryLookups := buildLookupMap(categories)
return categoryLookups, nil
}
func buildLookupMap(categories map[int]*PiwigoCategory) map[string]*PiwigoCategory {
categoryLookups := map[string]*PiwigoCategory{}
for _, category := range categories {
logrus.Debugf("Loaded existing category %s", category.Key)
categoryLookups[category.Key] = category
}
return categoryLookups
}
func buildCategoryMap(statusResponse *getCategoryListResponse) map[int]*PiwigoCategory {
categories := map[int]*PiwigoCategory{}
for _, category := range statusResponse.Result.Categories {
categories[category.ID] = &PiwigoCategory{Id: category.ID, ParentId: category.IDUppercat, Name: category.Name, Key: category.Name}
}
return categories
}
func buildCategoryKeys(categories map[int]*PiwigoCategory) {
for _, category := range categories {
if category.ParentId == 0 {
category.Key = category.Name
continue
}
key := category.Name
parentId := category.ParentId
for parentId != 0 {
parent := categories[parentId]
// as we build the category as a directory hierarchy,
// we have to use the path separator to construct the path key
key = fmt.Sprintf("%s%c%s", parent.Name, os.PathSeparator, key)
parentId = parent.ParentId
}
category.Key = key
}
}
func CreateCategory(context *PiwigoContext, parentId int, name string) (int, error) {
formData := url.Values{}
formData.Set("method", "pwg.categories.add")
formData.Set("name", name)
// we only submit the parentid if there is one.
if parentId > 0 {
formData.Set("parent", fmt.Sprint(parentId))
}
response, err := context.PostForm(formData)
if err != nil {
return 0, err
}
defer response.Body.Close()
var createResponse createCategoryResponse
if err := json.NewDecoder(response.Body).Decode(&createResponse); err != nil {
logrus.Errorln(err)
return 0, err
}
if createResponse.Status != "ok" {
logrus.Errorf("Got state %s while loading categories", createResponse.Status)
return 0, errors.New("Could not create category")
}
logrus.Infof("Successfully got all categories from %s", context.Url)
return createResponse.Result.ID, nil
}

View File

@ -1,42 +0,0 @@
package category
import (
"encoding/json"
"errors"
"fmt"
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo"
"github.com/sirupsen/logrus"
"net/url"
)
func CreateCategory(context *piwigo.PiwigoContext, parentId int, name string) (int, error) {
formData := url.Values{}
formData.Set("method", "pwg.categories.add")
formData.Set("name", name)
// we only submit the parentid if there is one.
if parentId > 0 {
formData.Set("parent", fmt.Sprint(parentId))
}
response, err := context.PostForm(formData)
if err != nil {
return 0, err
}
defer response.Body.Close()
var createResponse createCategoryResponse
if err := json.NewDecoder(response.Body).Decode(&createResponse); err != nil {
logrus.Errorln(err)
return 0, err
}
if createResponse.Status != "ok" {
logrus.Errorf("Got state %s while loading categories", createResponse.Status)
return 0, errors.New("Could not create category")
}
logrus.Infof("Successfully got all categories from %s", context.Url)
return createResponse.Result.ID, nil
}

View File

@ -1,80 +0,0 @@
package category
import (
"encoding/json"
"errors"
"fmt"
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo"
"github.com/sirupsen/logrus"
"net/url"
"os"
)
func GetAllCategories(context *piwigo.PiwigoContext) (map[string]*PiwigoCategory, error) {
formData := url.Values{}
formData.Set("method", "pwg.categories.getList")
formData.Set("recursive", "true")
response, err := context.PostForm(formData)
if err != nil {
return nil, err
}
defer response.Body.Close()
var statusResponse getCategoryListResponse
if err := json.NewDecoder(response.Body).Decode(&statusResponse); err != nil {
logrus.Errorln(err)
return nil, err
}
if statusResponse.Status != "ok" {
logrus.Errorf("Got state %s while loading categories", statusResponse.Status)
return nil, errors.New("Could not load categories")
}
logrus.Infof("Successfully got all categories")
categories := buildCategoryMap(&statusResponse)
buildCategoryKeys(categories)
categoryLookups := buildLookupMap(categories)
return categoryLookups, nil
}
func buildLookupMap(categories map[int]*PiwigoCategory) map[string]*PiwigoCategory {
categoryLookups := map[string]*PiwigoCategory{}
for _, category := range categories {
logrus.Debugf("Loaded existing category %s", category.Key)
categoryLookups[category.Key] = category
}
return categoryLookups
}
func buildCategoryMap(statusResponse *getCategoryListResponse) map[int]*PiwigoCategory {
categories := map[int]*PiwigoCategory{}
for _, category := range statusResponse.Result.Categories {
categories[category.ID] = &PiwigoCategory{Id: category.ID, ParentId: category.IDUppercat, Name: category.Name, Key: category.Name}
}
return categories
}
func buildCategoryKeys(categories map[int]*PiwigoCategory) {
for _, category := range categories {
if category.ParentId == 0 {
category.Key = category.Name
continue
}
key := category.Name
parentId := category.ParentId
for parentId != 0 {
parent := categories[parentId]
// as we build the category as a directory hierarchy,
// we have to use the path separator to construct the path key
key = fmt.Sprintf("%s%c%s", parent.Name, os.PathSeparator, key)
parentId = parent.ParentId
}
category.Key = key
}
}

View File

@ -1,42 +0,0 @@
package category
type PiwigoCategory struct {
Id int
ParentId int
Name string
Key string
}
type getCategoryListResponse struct {
Status string `json:"stat"`
Result struct {
Categories []struct {
ID int `json:"id"`
Name string `json:"name"`
Comment string `json:"comment,omitempty"`
Permalink string `json:"permalink,omitempty"`
Status string `json:"status,omitempty"`
Uppercats string `json:"uppercats,omitempty"`
GlobalRank string `json:"global_rank,omitempty"`
IDUppercat int `json:"id_uppercat,string,omitempty"`
NbImages int `json:"nb_images,omitempty"`
TotalNbImages int `json:"total_nb_images,omitempty"`
RepresentativePictureID string `json:"representative_picture_id,omitempty"`
DateLast string `json:"date_last,omitempty"`
MaxDateLast string `json:"max_date_last,omitempty"`
NbCategories int `json:"nb_categories,omitempty"`
URL string `json:"url,omitempty"`
TnURL string `json:"tn_url,omitempty"`
} `json:"categories"`
} `json:"result"`
}
type createCategoryResponse struct {
Status string `json:"stat"`
Err int `json:"err"`
Message string `json:"message"`
Result struct {
Info string `json:"info"`
ID int `json:"id"`
} `json:"result"`
}