From e6e7e46fd186f3e5cd72516ce27b121700044ab7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Mon, 25 Feb 2019 00:02:59 +0100 Subject: [PATCH] added first query to get categories moved piwigo context to the upper package piwigo --- internal/app/app.go | 18 ++++--- internal/app/types.go | 6 ++- .../piwigo/authentication/authentication.go | 9 ++-- internal/pkg/piwigo/authentication/types.go | 9 ---- internal/pkg/piwigo/category/query.go | 48 +++++++++++++++++++ internal/pkg/piwigo/category/types.go | 30 ++++++++++-- internal/pkg/piwigo/types.go | 10 ++++ 7 files changed, 106 insertions(+), 24 deletions(-) create mode 100644 internal/pkg/piwigo/category/query.go create mode 100644 internal/pkg/piwigo/types.go diff --git a/internal/app/app.go b/internal/app/app.go index 7168214..e61b2ff 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -6,7 +6,9 @@ import ( "fmt" "github.com/sirupsen/logrus" "haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/localFileStructure" + "haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo" "haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo/authentication" + "haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo/category" "os" ) @@ -28,7 +30,8 @@ func Run() { os.Exit(2) } //ScanLocalDirectories(context) - //GetAllCategoriesFromServer() + + GetAllCategoriesFromServer(context) //FindMissingAlbums() //CreateMissingAlbums() @@ -48,10 +51,13 @@ func ScanLocalDirectories(context *AppContext) { } } -func GetAllCategoriesFromServer() { - // get all categories from server and flatten structure to match directory names - // 2018/2018 album blah - logrus.Warnln("Loading all categories from the server (NotImplemented)") +func GetAllCategoriesFromServer(context *AppContext) { + + err := category.GetAllCategories(context.Piwigo) + if err != nil { + os.Exit(3) + } + } func FindMissingAlbums() { @@ -87,7 +93,7 @@ func configureContext() (*AppContext, error) { context := new(AppContext) context.LocalRootPath = *imagesRootPath - context.Piwigo = new(authentication.PiwigoContext) + context.Piwigo = new(piwigo.PiwigoContext) context.Piwigo.Url = fmt.Sprintf("%s/ws.php?format=json", *piwigoUrl) context.Piwigo.Username = *piwigoUser context.Piwigo.Password = *piwigoPassword diff --git a/internal/app/types.go b/internal/app/types.go index 4d69957..9e05b6e 100644 --- a/internal/app/types.go +++ b/internal/app/types.go @@ -1,9 +1,11 @@ package app -import "haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo/authentication" +import ( + "haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo" +) type AppContext struct { - Piwigo *authentication.PiwigoContext + Piwigo *piwigo.PiwigoContext SessionId string LocalRootPath string ChunkSizeBytes int diff --git a/internal/pkg/piwigo/authentication/authentication.go b/internal/pkg/piwigo/authentication/authentication.go index edfa793..cc4e5f6 100644 --- a/internal/pkg/piwigo/authentication/authentication.go +++ b/internal/pkg/piwigo/authentication/authentication.go @@ -5,13 +5,14 @@ import ( "errors" "fmt" "github.com/sirupsen/logrus" + "haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo" "net/http" "net/http/cookiejar" "net/url" "strings" ) -func Login(context *PiwigoContext) error { +func Login(context *piwigo.PiwigoContext) error { logrus.Debugf("Logging in to %s using user %s", context.Url, context.Username) if !strings.HasPrefix(context.Url, "https") { @@ -50,7 +51,7 @@ func Login(context *PiwigoContext) error { return nil } -func Logout(context *PiwigoContext) error { +func Logout(context *piwigo.PiwigoContext) error { logrus.Debugf("Logging out from %s", context.Url) initializeCookieJarIfRequired(context) @@ -80,7 +81,7 @@ func Logout(context *PiwigoContext) error { return nil } -func GetStatus(context *PiwigoContext) (*GetStatusResponse, error) { +func GetStatus(context *piwigo.PiwigoContext) (*GetStatusResponse, error) { logrus.Debugln("Getting current login state...") @@ -112,7 +113,7 @@ func GetStatus(context *PiwigoContext) (*GetStatusResponse, error) { return &statusResponse, nil } -func initializeCookieJarIfRequired(context *PiwigoContext) { +func initializeCookieJarIfRequired(context *piwigo.PiwigoContext) { if context.Cookies != nil { return } diff --git a/internal/pkg/piwigo/authentication/types.go b/internal/pkg/piwigo/authentication/types.go index 21990bb..2a4be07 100644 --- a/internal/pkg/piwigo/authentication/types.go +++ b/internal/pkg/piwigo/authentication/types.go @@ -1,14 +1,5 @@ package authentication -import "net/http/cookiejar" - -type PiwigoContext struct { - Url string - Username string - Password string - Cookies *cookiejar.Jar -} - type LoginResponse struct { Status string `json:"stat"` Result bool `json:"result"` diff --git a/internal/pkg/piwigo/category/query.go b/internal/pkg/piwigo/category/query.go new file mode 100644 index 0000000..28dfa2b --- /dev/null +++ b/internal/pkg/piwigo/category/query.go @@ -0,0 +1,48 @@ +package category + +import ( + "encoding/json" + "errors" + "github.com/sirupsen/logrus" + "haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo" + "net/http" + "net/url" +) + +func GetAllCategories(context *piwigo.PiwigoContext) error { + logrus.Debugln("Starting GetAllCategories") + if context.Cookies == nil { + return errors.New("Not logged in and no cookies found! Can not get the category list!") + } + + formData := url.Values{} + formData.Set("method", "pwg.categories.getList") + formData.Set("recursive", "true") + + client := http.Client{Jar: context.Cookies} + response, err := client.PostForm(context.Url, formData) + + if err != nil { + logrus.Errorln("The HTTP request failed with error %s", err) + return err + } + + var statusResponse getCategoryListResponse + if err := json.NewDecoder(response.Body).Decode(&statusResponse); err != nil { + logrus.Errorln(err) + return err + } + + if statusResponse.Status != "ok" { + logrus.Errorf("Got state %s while loading categories", statusResponse.Status) + return errors.New("Could not load categories") + } + + logrus.Infof("Successfully got all categories from %s", context.Url) + + for _, category := range statusResponse.Result.Categories { + logrus.Debugf("Category %d - %s", category.ID, category.Name) + } + + return nil +} diff --git a/internal/pkg/piwigo/category/types.go b/internal/pkg/piwigo/category/types.go index 8ba83a0..4e4a84f 100644 --- a/internal/pkg/piwigo/category/types.go +++ b/internal/pkg/piwigo/category/types.go @@ -1,7 +1,31 @@ package category type PiwigoCategory struct { - id int - name string - key string + Id 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"` + Permalink interface{} `json:"permalink"` + Status string `json:"status"` + Uppercats string `json:"uppercats"` + GlobalRank string `json:"global_rank"` + IDUppercat interface{} `json:"id_uppercat"` + NbImages int `json:"nb_images"` + TotalNbImages int `json:"total_nb_images"` + RepresentativePictureID string `json:"representative_picture_id"` + DateLast interface{} `json:"date_last"` + MaxDateLast string `json:"max_date_last"` + NbCategories int `json:"nb_categories"` + URL string `json:"url"` + TnURL string `json:"tn_url"` + } `json:"categories"` + } `json:"result"` } diff --git a/internal/pkg/piwigo/types.go b/internal/pkg/piwigo/types.go new file mode 100644 index 0000000..106ae0a --- /dev/null +++ b/internal/pkg/piwigo/types.go @@ -0,0 +1,10 @@ +package piwigo + +import "net/http/cookiejar" + +type PiwigoContext struct { + Url string + Username string + Password string + Cookies *cookiejar.Jar +}