feat: add Facebook cache invalidation functions and update server routes
This commit is contained in:
parent
a40e32978a
commit
2d0745f9ec
2 changed files with 71 additions and 1 deletions
58
apps/backend/facebook.go
Normal file
58
apps/backend/facebook.go
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"backend/queries"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
//curl -i -X POST \
|
||||||
|
// "https://graph.facebook.com/v23.0/?scrape=true&id=https%3A%2F%2Fwww.infoalloggi.it%2Fannuncio%2F2594&access_token=1795915138017917%7CiS9PlNKfIsyek5GxVcCsvs0vHd8"
|
||||||
|
|
||||||
|
func invalidateFacebookCache(url string) error {
|
||||||
|
accessToken := "1468971870274193|1WvBKvZkWAwtEJcKnjUnYc639Kg"
|
||||||
|
apiURL := "https://graph.facebook.com/v24.0/"
|
||||||
|
req, err := http.NewRequest("POST", apiURL, nil)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Error creating request: %v", err)
|
||||||
|
}
|
||||||
|
q := req.URL.Query()
|
||||||
|
q.Add("id", url)
|
||||||
|
q.Add("scrape", "true")
|
||||||
|
q.Add("access_token", accessToken)
|
||||||
|
req.URL.RawQuery = q.Encode()
|
||||||
|
client := &http.Client{}
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Error making request: %v", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
|
||||||
|
return fmt.Errorf("Failed to invalidate Facebook cache. Status Code: %d", resp.StatusCode)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Successfully invalidated Facebook cache for URL: %s\n", url)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func invalidateAllAnnunciFB() error {
|
||||||
|
annunci, err := queries.AnnunciGetActive()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to get active annunci: %w", err)
|
||||||
|
}
|
||||||
|
delayBetweenRequestsMs := 20
|
||||||
|
for _, annuncio := range annunci {
|
||||||
|
url := fmt.Sprintf("https://www.infoalloggi.it/annuncio/%s", annuncio)
|
||||||
|
err := invalidateFacebookCache(url)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to invalidate FB cache for %s: %v\n", url, err)
|
||||||
|
}
|
||||||
|
time.Sleep(time.Duration(delayBetweenRequestsMs) * time.Millisecond)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -161,6 +161,12 @@ func (s *Server) SetupRoutes() *echo.Echo {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.String(http.StatusInternalServerError, err.Error())
|
return c.String(http.StatusInternalServerError, err.Error())
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
err = invalidateAllAnnunciFB()
|
||||||
|
if err != nil {
|
||||||
|
return c.String(http.StatusInternalServerError, err.Error())
|
||||||
|
}
|
||||||
|
*/
|
||||||
return c.String(http.StatusOK, "Updated")
|
return c.String(http.StatusOK, "Updated")
|
||||||
})
|
})
|
||||||
e.GET("/update-cod/:cod", func(c echo.Context) error {
|
e.GET("/update-cod/:cod", func(c echo.Context) error {
|
||||||
|
|
@ -194,7 +200,13 @@ func (s *Server) SetupRoutes() *echo.Echo {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.String(http.StatusInternalServerError, err.Error())
|
return c.String(http.StatusInternalServerError, err.Error())
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
url := fmt.Sprintf("https://www.infoalloggi.it/annuncio/%s", cod)
|
||||||
|
err = invalidateFacebookCache(url)
|
||||||
|
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