Refactor code structure for improved readability and maintainability
This commit is contained in:
parent
e8957e6f3b
commit
991367be56
4 changed files with 5426 additions and 13 deletions
4060
apps/backend/caratteristiche.xml
Normal file
4060
apps/backend/caratteristiche.xml
Normal file
File diff suppressed because it is too large
Load diff
1288
apps/backend/categorie.xml
Normal file
1288
apps/backend/categorie.xml
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -46,7 +46,12 @@ func (m *MiogestHandler) GetAnnunci() typesdefs.AnnunciXML {
|
|||
}
|
||||
|
||||
func (m *MiogestHandler) GetAnnunciFromMiogest() {
|
||||
m.AnnunciXML = utils.GetDataXML[typesdefs.AnnunciXML]("http://partner.miogest.com/agenzie/infoalloggi.xml")
|
||||
var err error
|
||||
m.AnnunciXML, err = utils.GetDataXML[typesdefs.AnnunciXML]("http://partner.miogest.com/agenzie/infoalloggi.xml")
|
||||
if err != nil {
|
||||
log.Printf("Failed to get annunci: %v", err)
|
||||
return
|
||||
}
|
||||
m.annunci_cache_expires = time.Now().Add(1 * time.Hour)
|
||||
}
|
||||
|
||||
|
|
@ -71,7 +76,12 @@ func (m *MiogestHandler) GetCategorie() []typesdefs.Categoria {
|
|||
}
|
||||
|
||||
func (m *MiogestHandler) GenerateCaratteristiche() {
|
||||
var result = utils.GetDataXML[typesdefs.DefaultCaratteristiche]("https://www.miogest.com/apps/revo.aspx?tipo=schede")
|
||||
//https://www.miogest.com/apps/revo.aspx?tipo=schede
|
||||
var result, err = utils.GetXMLFromFile[typesdefs.DefaultCaratteristiche]("./caratteristiche.xml")
|
||||
if err != nil {
|
||||
log.Printf("Failed to get caratteristiche: %v", err)
|
||||
return
|
||||
}
|
||||
var array typesdefs.ListaCaratteristiche
|
||||
for _, tag := range result.Scheda {
|
||||
var c = typesdefs.Caratteristica{Id: tag.Id, Tagxml: tag.Tagxml}
|
||||
|
|
@ -82,7 +92,12 @@ func (m *MiogestHandler) GenerateCaratteristiche() {
|
|||
}
|
||||
|
||||
func (m *MiogestHandler) GenerateCategorie() {
|
||||
var result = utils.GetDataXML[typesdefs.DefaultCategories]("https://www.miogest.com/apps/revo.aspx?tipo=categorie")
|
||||
//https://www.miogest.com/apps/revo.aspx?tipo=categorie
|
||||
var result, err = utils.GetXMLFromFile[typesdefs.DefaultCategories]("./categorie.xml")
|
||||
if err != nil {
|
||||
log.Printf("Failed to get categorie: %v", err)
|
||||
return
|
||||
}
|
||||
var listaCategorie []typesdefs.Categoria
|
||||
for _, elem := range result.Cat {
|
||||
var c = typesdefs.Categoria{Id: elem.ID, Nome: elem.Nome}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import (
|
|||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Ptr[T any](v T) *T { return &v }
|
||||
|
|
@ -39,19 +40,68 @@ func fetchXML(url string) ([]byte, error) {
|
|||
return data, nil
|
||||
}
|
||||
|
||||
func GetDataXML[T any](url string) T {
|
||||
xmlBytes, err := fetchXML(url)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to get XML: %v", err)
|
||||
panic(err)
|
||||
}
|
||||
func GetXMLFromFile[T any](filePath string) (T, error) {
|
||||
var result T
|
||||
err = xml.Unmarshal(xmlBytes, &result)
|
||||
xmlFile, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to unmarshal XML: %v", err)
|
||||
panic(err)
|
||||
return result, fmt.Errorf("failed to open file: %w", err)
|
||||
}
|
||||
return result
|
||||
defer xmlFile.Close()
|
||||
|
||||
byteValue, err := io.ReadAll(xmlFile)
|
||||
if err != nil {
|
||||
return result, fmt.Errorf("failed to read file: %w", err)
|
||||
}
|
||||
err = xml.Unmarshal(byteValue, &result)
|
||||
if err != nil {
|
||||
return result, fmt.Errorf("failed to unmarshal XML: %w", err)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func GetDataXML[T any](url string) (T, error) {
|
||||
var result T
|
||||
client := &http.Client{
|
||||
Timeout: 30 * time.Second,
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return result, fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
|
||||
// Add browser-like headers to avoid 403
|
||||
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
|
||||
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
|
||||
req.Header.Set("Accept-Language", "en-US,en;q=0.9,it;q=0.8")
|
||||
req.Header.Set("Accept-Encoding", "gzip, deflate")
|
||||
req.Header.Set("Connection", "keep-alive")
|
||||
req.Header.Set("Referer", "https://www.miogest.com/")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
log.Printf("Resp %v", resp)
|
||||
if err != nil {
|
||||
|
||||
return result, fmt.Errorf("Failed to fetch url %s: %w", url, err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
|
||||
log.Printf("Fetch XML error: status error: %d", resp.StatusCode)
|
||||
return result, fmt.Errorf("Failed to fetch url %s: status error: %d", url, resp.StatusCode)
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return result, fmt.Errorf("failed to read response: %w", err)
|
||||
}
|
||||
|
||||
err = xml.Unmarshal(body, &result)
|
||||
if err != nil {
|
||||
return result, fmt.Errorf("failed to unmarshal XML: %w", err)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func SafeRemoveAll(path string) error {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue