2024-09-24 06:17:11 -04:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
2024-11-14 05:57:11 -05:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2024-11-15 02:02:45 -05:00
|
|
|
|
|
|
|
"github.com/nitezs/pcgamedb/config"
|
|
|
|
"github.com/nitezs/pcgamedb/crawler"
|
|
|
|
"github.com/nitezs/pcgamedb/model"
|
|
|
|
"github.com/nitezs/pcgamedb/utils"
|
2024-09-24 06:17:11 -04:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Crawl(logger *zap.Logger) {
|
|
|
|
var games []*model.GameDownload
|
|
|
|
var crawlerMap = crawler.BuildCrawlerMap(logger)
|
|
|
|
for _, item := range crawlerMap {
|
2024-11-14 12:29:19 -05:00
|
|
|
logger.Info("Crawling", zap.String("crawler", item.Name()))
|
2024-09-24 06:17:11 -04:00
|
|
|
if c, ok := item.(crawler.PagedCrawler); ok {
|
|
|
|
g, err := c.CrawlMulti([]int{1, 2, 3})
|
|
|
|
if err != nil {
|
2024-11-15 02:02:45 -05:00
|
|
|
logger.Warn("Failed to crawl games", zap.String("crawler", c.Name()), zap.Error(err))
|
2024-09-24 06:17:11 -04:00
|
|
|
}
|
|
|
|
games = append(games, g...)
|
|
|
|
} else if c, ok := item.(crawler.SimpleCrawler); ok {
|
|
|
|
g, err := c.CrawlAll()
|
|
|
|
if err != nil {
|
2024-11-15 02:02:45 -05:00
|
|
|
logger.Warn("Failed to crawl games", zap.String("crawler", c.Name()), zap.Error(err))
|
2024-09-24 06:17:11 -04:00
|
|
|
}
|
|
|
|
games = append(games, g...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logger.Info("Crawled finished", zap.Int("count", len(games)))
|
|
|
|
for _, game := range games {
|
|
|
|
logger.Info(
|
|
|
|
"Crawled game",
|
|
|
|
zap.String("name", game.RawName),
|
|
|
|
zap.String("author", game.Author),
|
|
|
|
zap.String("url", game.Url),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Clean(logger)
|
2024-11-14 05:57:11 -05:00
|
|
|
for _, u := range config.Config.Webhooks.CrawlTask {
|
|
|
|
_, err := url.Parse(u)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("Invalid webhook url", zap.String("url", u), zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
logger.Info("webhook triggered", zap.String("task", "crawl"), zap.String("url", u))
|
|
|
|
_, err = utils.Fetch(utils.FetchConfig{
|
|
|
|
Url: u,
|
|
|
|
Method: http.MethodPost,
|
|
|
|
Headers: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
Data: games,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("Failed to trigger webhook", zap.String("task", "crawl"), zap.String("url", u), zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
2024-09-24 06:17:11 -04:00
|
|
|
}
|