added category tree build up and missing category detection
This commit is contained in:
parent
e6e7e46fd1
commit
fbd92489f2
@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/localFileStructure"
|
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/localFileStructure"
|
||||||
|
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/matcher"
|
||||||
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo"
|
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo"
|
||||||
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo/authentication"
|
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo/authentication"
|
||||||
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo/category"
|
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo/category"
|
||||||
@ -29,50 +30,58 @@ func Run() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
//ScanLocalDirectories(context)
|
|
||||||
|
|
||||||
GetAllCategoriesFromServer(context)
|
filesystemNodes := scanLocalDirectories(context)
|
||||||
|
categories := getAllCategoriesFromServer(context)
|
||||||
|
|
||||||
//FindMissingAlbums()
|
synchronizeCategories(filesystemNodes, categories)
|
||||||
//CreateMissingAlbums()
|
|
||||||
//FindMissingImages()
|
|
||||||
//UploadImages()
|
findMissingImages()
|
||||||
|
uploadImages()
|
||||||
|
|
||||||
_ = authentication.Logout(context.Piwigo)
|
_ = authentication.Logout(context.Piwigo)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ScanLocalDirectories(context *AppContext) {
|
func synchronizeCategories(filesystemNodes map[string]*localFileStructure.FilesystemNode, categories map[string]*category.PiwigoCategory) {
|
||||||
|
missingCategories := findMissingCategories(filesystemNodes, categories)
|
||||||
|
createMissingCategories(missingCategories)
|
||||||
|
}
|
||||||
|
|
||||||
|
func scanLocalDirectories(context *AppContext) map[string]*localFileStructure.FilesystemNode {
|
||||||
fileNodes, err := localFileStructure.ScanLocalFileStructure(context.LocalRootPath)
|
fileNodes, err := localFileStructure.ScanLocalFileStructure(context.LocalRootPath)
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
for _, node := range fileNodes {
|
|
||||||
logrus.Debugln("found path entry:", node.Key)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetAllCategoriesFromServer(context *AppContext) {
|
|
||||||
|
|
||||||
err := category.GetAllCategories(context.Piwigo)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
os.Exit(3)
|
os.Exit(3)
|
||||||
}
|
}
|
||||||
|
return fileNodes
|
||||||
}
|
}
|
||||||
|
|
||||||
func FindMissingAlbums() {
|
func getAllCategoriesFromServer(context *AppContext) map[string]*category.PiwigoCategory {
|
||||||
logrus.Warnln("Looking up missing albums (NotImplemented)")
|
|
||||||
|
categories, err := category.GetAllCategories(context.Piwigo)
|
||||||
|
if err != nil {
|
||||||
|
os.Exit(4)
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateMissingAlbums() {
|
return categories
|
||||||
|
}
|
||||||
|
|
||||||
|
func findMissingCategories(fileSystem map[string]*localFileStructure.FilesystemNode, categories map[string]*category.PiwigoCategory) []string {
|
||||||
|
return matcher.FindMissingCategories(fileSystem, categories)
|
||||||
|
}
|
||||||
|
|
||||||
|
func createMissingCategories(categories []string) {
|
||||||
logrus.Warnln("Creating missing albums (NotImplemented)")
|
logrus.Warnln("Creating missing albums (NotImplemented)")
|
||||||
|
for _, c := range categories {
|
||||||
|
logrus.Debug(c)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func FindMissingImages() {
|
func findMissingImages() {
|
||||||
logrus.Warnln("Finding missing images (NotImplemented)")
|
logrus.Warnln("Finding missing images (NotImplemented)")
|
||||||
}
|
}
|
||||||
|
|
||||||
func UploadImages() {
|
func uploadImages() {
|
||||||
logrus.Warnln("Uploading missing images (NotImplemented)")
|
logrus.Warnln("Uploading missing images (NotImplemented)")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,10 +3,13 @@ package localFileStructure
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ScanLocalFileStructure(path string) (map[string]FilesystemNode, error) {
|
func ScanLocalFileStructure(path string) (map[string]*FilesystemNode, error) {
|
||||||
fileMap := make(map[string]FilesystemNode)
|
fileMap := make(map[string]*FilesystemNode)
|
||||||
|
|
||||||
|
relativeRoot := filepath.Base(path)+"/"
|
||||||
|
|
||||||
err := filepath.Walk(path, func(p string, info os.FileInfo, err error) error {
|
err := filepath.Walk(path, func(p string, info os.FileInfo, err error) error {
|
||||||
if path == p {
|
if path == p {
|
||||||
@ -15,8 +18,10 @@ func ScanLocalFileStructure(path string) (map[string]FilesystemNode, error) {
|
|||||||
|
|
||||||
//TODO: Only allow jpg and png files here
|
//TODO: Only allow jpg and png files here
|
||||||
|
|
||||||
fileMap[p] = FilesystemNode{
|
key := strings.Replace(p,relativeRoot,"",1)
|
||||||
Key: p,
|
|
||||||
|
fileMap[p] = &FilesystemNode{
|
||||||
|
Key: key,
|
||||||
Name: info.Name(),
|
Name: info.Name(),
|
||||||
IsDir: info.IsDir(),
|
IsDir: info.IsDir(),
|
||||||
}
|
}
|
||||||
|
27
internal/pkg/matcher/category.go
Normal file
27
internal/pkg/matcher/category.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package matcher
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/localFileStructure"
|
||||||
|
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo/category"
|
||||||
|
)
|
||||||
|
|
||||||
|
func FindMissingCategories(fileSystem map[string]*localFileStructure.FilesystemNode, categories map[string]*category.PiwigoCategory) []string {
|
||||||
|
missingCategories := []string{}
|
||||||
|
for _, file := range fileSystem {
|
||||||
|
if !file.IsDir {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
_, exists := categories[file.Key]
|
||||||
|
|
||||||
|
if !exists {
|
||||||
|
logrus.Infof("Found missing category %s", file.Key)
|
||||||
|
missingCategories = append(missingCategories, file.Key)
|
||||||
|
} else {
|
||||||
|
logrus.Debugf("Found existing category %s", file.Key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return missingCategories
|
||||||
|
}
|
@ -3,16 +3,17 @@ package category
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo"
|
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetAllCategories(context *piwigo.PiwigoContext) error {
|
func GetAllCategories(context *piwigo.PiwigoContext) (map[string]*PiwigoCategory, error) {
|
||||||
logrus.Debugln("Starting GetAllCategories")
|
logrus.Debugln("Starting GetAllCategories")
|
||||||
if context.Cookies == nil {
|
if context.Cookies == nil {
|
||||||
return errors.New("Not logged in and no cookies found! Can not get the category list!")
|
return nil, errors.New("Not logged in and no cookies found! Can not get the category list!")
|
||||||
}
|
}
|
||||||
|
|
||||||
formData := url.Values{}
|
formData := url.Values{}
|
||||||
@ -24,25 +25,61 @@ func GetAllCategories(context *piwigo.PiwigoContext) error {
|
|||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorln("The HTTP request failed with error %s", err)
|
logrus.Errorln("The HTTP request failed with error %s", err)
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var statusResponse getCategoryListResponse
|
var statusResponse getCategoryListResponse
|
||||||
if err := json.NewDecoder(response.Body).Decode(&statusResponse); err != nil {
|
if err := json.NewDecoder(response.Body).Decode(&statusResponse); err != nil {
|
||||||
logrus.Errorln(err)
|
logrus.Errorln(err)
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if statusResponse.Status != "ok" {
|
if statusResponse.Status != "ok" {
|
||||||
logrus.Errorf("Got state %s while loading categories", statusResponse.Status)
|
logrus.Errorf("Got state %s while loading categories", statusResponse.Status)
|
||||||
return errors.New("Could not load categories")
|
return nil, errors.New("Could not load categories")
|
||||||
}
|
}
|
||||||
|
|
||||||
logrus.Infof("Successfully got all categories from %s", context.Url)
|
logrus.Infof("Successfully got all categories from %s", context.Url)
|
||||||
|
|
||||||
for _, category := range statusResponse.Result.Categories {
|
categories := buildCategoryMap(&statusResponse)
|
||||||
logrus.Debugf("Category %d - %s", category.ID, category.Name)
|
buildCategoryKeys(categories)
|
||||||
|
categoryLookups := buildLookupMap(categories)
|
||||||
|
|
||||||
|
return categoryLookups, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
func buildLookupMap(categories map[int]*PiwigoCategory) map[string]*PiwigoCategory {
|
||||||
|
categoryLookups := map[string]*PiwigoCategory{}
|
||||||
|
for _, category := range categories {
|
||||||
|
logrus.Debugf("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]
|
||||||
|
key = fmt.Sprintf("%s/%s", parent.Name, key)
|
||||||
|
parentId = parent.ParentId
|
||||||
|
}
|
||||||
|
|
||||||
|
category.Key = key
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package category
|
|||||||
|
|
||||||
type PiwigoCategory struct {
|
type PiwigoCategory struct {
|
||||||
Id int
|
Id int
|
||||||
|
ParentId int
|
||||||
Name string
|
Name string
|
||||||
Key string
|
Key string
|
||||||
}
|
}
|
||||||
@ -12,20 +13,20 @@ type getCategoryListResponse struct {
|
|||||||
Categories []struct {
|
Categories []struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Comment string `json:"comment"`
|
Comment string `json:"comment,omitempty"`
|
||||||
Permalink interface{} `json:"permalink"`
|
Permalink string `json:"permalink,omitempty"`
|
||||||
Status string `json:"status"`
|
Status string `json:"status,omitempty"`
|
||||||
Uppercats string `json:"uppercats"`
|
Uppercats string `json:"uppercats,omitempty"`
|
||||||
GlobalRank string `json:"global_rank"`
|
GlobalRank string `json:"global_rank,omitempty"`
|
||||||
IDUppercat interface{} `json:"id_uppercat"`
|
IDUppercat int `json:"id_uppercat,string,omitempty"`
|
||||||
NbImages int `json:"nb_images"`
|
NbImages int `json:"nb_images,omitempty"`
|
||||||
TotalNbImages int `json:"total_nb_images"`
|
TotalNbImages int `json:"total_nb_images,omitempty"`
|
||||||
RepresentativePictureID string `json:"representative_picture_id"`
|
RepresentativePictureID string `json:"representative_picture_id,omitempty"`
|
||||||
DateLast interface{} `json:"date_last"`
|
DateLast string `json:"date_last,omitempty"`
|
||||||
MaxDateLast string `json:"max_date_last"`
|
MaxDateLast string `json:"max_date_last,omitempty"`
|
||||||
NbCategories int `json:"nb_categories"`
|
NbCategories int `json:"nb_categories,omitempty"`
|
||||||
URL string `json:"url"`
|
URL string `json:"url,omitempty"`
|
||||||
TnURL string `json:"tn_url"`
|
TnURL string `json:"tn_url,omitempty"`
|
||||||
} `json:"categories"`
|
} `json:"categories"`
|
||||||
} `json:"result"`
|
} `json:"result"`
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user