feat: update GetAnnuncioParsed and ParseAnnunci methods for improved filtering and error handling
This commit is contained in:
parent
4524f1b4bc
commit
8b2de9472e
3 changed files with 60 additions and 43 deletions
|
|
@ -102,39 +102,32 @@ func (m *MiogestHandler) GetAnnunciParsed(i *ImageRefManager) typesdefs.AnnunciP
|
||||||
}
|
}
|
||||||
|
|
||||||
// versione per singolo annuncio
|
// versione per singolo annuncio
|
||||||
func (m *MiogestHandler) GetAnnuncioParsed(cod string, i *ImageRefManager) typesdefs.AnnuncioParsed {
|
func (m *MiogestHandler) GetAnnuncioParsed(codFilter string, i *ImageRefManager) typesdefs.AnnunciParsed {
|
||||||
data := m.ParseAnnunci2(cod, i)
|
data := m.ParseAnnunci(codFilter, i)
|
||||||
if len(data.Annuncio) == 0 {
|
if len(data.Annuncio) == 0 {
|
||||||
return typesdefs.AnnuncioParsed{}
|
|
||||||
}
|
|
||||||
return data.Annuncio[0]
|
|
||||||
|
|
||||||
}
|
|
||||||
func (m *MiogestHandler) ParseAnnunci2(codFilter string, i *ImageRefManager) typesdefs.AnnunciParsed {
|
|
||||||
annunci := m.GetAnnunci()
|
|
||||||
|
|
||||||
if codFilter != "" {
|
|
||||||
if len(annunci.Annuncio) == 0 {
|
|
||||||
return typesdefs.AnnunciParsed{}
|
return typesdefs.AnnunciParsed{}
|
||||||
}
|
}
|
||||||
if slices.ContainsFunc(annunci.Annuncio, func(a typesdefs.AnnuncioXML) bool {
|
return data
|
||||||
|
|
||||||
return *a.Codice == codFilter
|
|
||||||
}) {
|
|
||||||
|
|
||||||
annunci.Annuncio = []typesdefs.AnnuncioXML{annunci.Annuncio[slices.IndexFunc(annunci.Annuncio, func(a typesdefs.AnnuncioXML) bool {
|
|
||||||
return *a.Codice == codFilter
|
|
||||||
})]}
|
|
||||||
//go on
|
|
||||||
} else {
|
|
||||||
return typesdefs.AnnunciParsed{}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return typesdefs.AnnunciParsed{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MiogestHandler) ParseAnnunci(codFilter string, i *ImageRefManager) typesdefs.AnnunciParsed {
|
func (m *MiogestHandler) ParseAnnunci(codFilter string, i *ImageRefManager) typesdefs.AnnunciParsed {
|
||||||
annunci := m.GetAnnunci()
|
annunci := m.GetAnnunci()
|
||||||
|
|
||||||
|
if len(annunci.Annuncio) == 0 {
|
||||||
|
return typesdefs.AnnunciParsed{}
|
||||||
|
}
|
||||||
|
|
||||||
|
if codFilter != "" {
|
||||||
|
idx := slices.IndexFunc(annunci.Annuncio, func(a typesdefs.AnnuncioXML) bool {
|
||||||
|
return *a.Codice == codFilter
|
||||||
|
})
|
||||||
|
if idx == -1 {
|
||||||
|
return typesdefs.AnnunciParsed{}
|
||||||
|
}
|
||||||
|
annunci.Annuncio = []typesdefs.AnnuncioXML{annunci.Annuncio[idx]}
|
||||||
|
}
|
||||||
|
|
||||||
image_codes_to_process := []string{}
|
image_codes_to_process := []string{}
|
||||||
if config.Cfg.Images.Enabled {
|
if config.Cfg.Images.Enabled {
|
||||||
var err error
|
var err error
|
||||||
|
|
@ -142,16 +135,26 @@ func (m *MiogestHandler) ParseAnnunci(codFilter string, i *ImageRefManager) type
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to find images to update: %v", err.Error())
|
log.Fatalf("Failed to find images to update: %v", err.Error())
|
||||||
}
|
}
|
||||||
|
if codFilter == "" {
|
||||||
|
// if processing all, also check for missing folders
|
||||||
missing_codes, err := i.MissingImageFolders()
|
missing_codes, err := i.MissingImageFolders()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to find missing image folders: %v", err.Error())
|
log.Fatalf("Failed to find missing image folders: %v", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(missing_codes) > 0 {
|
if len(missing_codes) > 0 {
|
||||||
|
|
||||||
image_codes_to_process = append(image_codes_to_process, missing_codes...)
|
image_codes_to_process = append(image_codes_to_process, missing_codes...)
|
||||||
image_codes_to_process = slices.Compact(image_codes_to_process)
|
image_codes_to_process = slices.Compact(image_codes_to_process)
|
||||||
|
|
||||||
}
|
}
|
||||||
// Clear images folder of codes that need to be updated
|
} else {
|
||||||
|
// if processing single codice, ensure it's in the list
|
||||||
|
if !slices.Contains(image_codes_to_process, codFilter) {
|
||||||
|
image_codes_to_process = append(image_codes_to_process, codFilter)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Clears images folder of codes that need to be updated
|
||||||
if err := i.ClearImages(image_codes_to_process); err != nil {
|
if err := i.ClearImages(image_codes_to_process); err != nil {
|
||||||
log.Fatalf("Failed to clear images: %v", err.Error())
|
log.Fatalf("Failed to clear images: %v", err.Error())
|
||||||
}
|
}
|
||||||
|
|
@ -284,9 +287,17 @@ func extractData_update(annuncio *typesdefs.AnnuncioXML, updateImages bool, m *M
|
||||||
folders := utils.GetPathDirsArray(filepath.Join(config.Cfg.Images.Path, tmp.Codice))
|
folders := utils.GetPathDirsArray(filepath.Join(config.Cfg.Images.Path, tmp.Codice))
|
||||||
for _, folder := range folders {
|
for _, folder := range folders {
|
||||||
folderpath := filepath.Join(filepath.Join(config.Cfg.Images.Path, tmp.Codice), folder)
|
folderpath := filepath.Join(filepath.Join(config.Cfg.Images.Path, tmp.Codice), folder)
|
||||||
|
exists, err := utils.PathExists(folderpath)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to check if folder exists: %v", err)
|
||||||
|
}
|
||||||
|
if !exists {
|
||||||
|
// something went wrong, skip
|
||||||
|
continue
|
||||||
|
}
|
||||||
files, err := os.ReadDir(folderpath)
|
files, err := os.ReadDir(folderpath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
log.Fatalf("Failed to read dir: %v", err)
|
||||||
}
|
}
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
if strings.Contains(file.Name(), "webp") && !strings.HasPrefix(file.Name(), "thumbnail") {
|
if strings.Contains(file.Name(), "webp") && !strings.HasPrefix(file.Name(), "thumbnail") {
|
||||||
|
|
|
||||||
|
|
@ -18,14 +18,16 @@ func AnnunciGetActive() ([]string, error) {
|
||||||
return annunci, nil
|
return annunci, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func AnnunciInsert(annunci typesdefs.AnnunciParsed) error {
|
func AnnunciInsert(annunci typesdefs.AnnunciParsed, resetAllBeforeUpdate bool) error {
|
||||||
|
var err error
|
||||||
|
if resetAllBeforeUpdate {
|
||||||
//SET ALL WEB TO FALSE
|
//SET ALL WEB TO FALSE
|
||||||
_, err := Db.Dbpool.Exec(Db.Ctx, "UPDATE public.annunci SET web = false, homepage = false, stato='Sospeso'")
|
_, err = Db.Dbpool.Exec(Db.Ctx, "UPDATE public.annunci SET web = false, homepage = false, stato='Sospeso'")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to set all web to false: %w", err)
|
return fmt.Errorf("failed to set all web to false: %w", err)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
//GET ALL CODICI
|
//GET ALL CODICI
|
||||||
var codici []string
|
var codici []string
|
||||||
err = pgxscan.Select(Db.Ctx, Db.Dbpool, &codici, "SELECT DISTINCT codice FROM public.annunci")
|
err = pgxscan.Select(Db.Ctx, Db.Dbpool, &codici, "SELECT DISTINCT codice FROM public.annunci")
|
||||||
|
|
|
||||||
|
|
@ -124,7 +124,7 @@ func (s *Server) SetupRoutes() *echo.Echo {
|
||||||
})
|
})
|
||||||
e.GET("/update", func(c echo.Context) error {
|
e.GET("/update", func(c echo.Context) error {
|
||||||
var data = s.miogest.GetAnnunciParsed(s.imageManager)
|
var data = s.miogest.GetAnnunciParsed(s.imageManager)
|
||||||
err := queries.AnnunciInsert(data)
|
err := queries.AnnunciInsert(data, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.String(http.StatusInternalServerError, err.Error())
|
return c.String(http.StatusInternalServerError, err.Error())
|
||||||
}
|
}
|
||||||
|
|
@ -134,7 +134,11 @@ func (s *Server) SetupRoutes() *echo.Echo {
|
||||||
|
|
||||||
cod := c.Param("cod")
|
cod := c.Param("cod")
|
||||||
|
|
||||||
var _ = s.miogest.GetAnnuncioParsed(cod, s.imageManager)
|
var data = s.miogest.GetAnnuncioParsed(cod, s.imageManager)
|
||||||
|
err := queries.AnnunciInsert(data, false)
|
||||||
|
if err != nil {
|
||||||
|
return c.String(http.StatusInternalServerError, err.Error())
|
||||||
|
}
|
||||||
return c.String(http.StatusOK, "Updated")
|
return c.String(http.StatusOK, "Updated")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue