infoalloggi-monorepo/apps/backend/facebook.go

58 lines
1.5 KiB
Go

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
}