added first query to get categories

moved piwigo context to the upper package piwigo
This commit is contained in:
Philipp Häfelfinger 2019-02-25 00:02:59 +01:00
parent f65698c587
commit e6e7e46fd1
7 changed files with 106 additions and 24 deletions

View File

@ -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

View File

@ -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

View File

@ -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
}

View File

@ -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"`

View File

@ -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
}

View File

@ -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"`
}

View File

@ -0,0 +1,10 @@
package piwigo
import "net/http/cookiejar"
type PiwigoContext struct {
Url string
Username string
Password string
Cookies *cookiejar.Jar
}