mirror of
https://github.com/wahyd4/scraper.git
synced 2026-08-09 05:06:22 +10:00
118 lines
2.9 KiB
Go
118 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/gocolly/colly"
|
|
"github.com/jinzhu/gorm"
|
|
_ "github.com/jinzhu/gorm/dialects/postgres"
|
|
"github.com/wahyd4/scraper/config"
|
|
"github.com/wahyd4/scraper/handlers"
|
|
"github.com/wahyd4/scraper/model"
|
|
"github.com/wahyd4/scraper/status"
|
|
)
|
|
|
|
// const URL = "https://www.domain.com.au/auction-results/melbourne/"
|
|
const (
|
|
urlTemplate = "https://www.domain.com.au/auction-results"
|
|
timeLayout = "2006-01-02"
|
|
initDate = "2020-02-29"
|
|
clearanceInfoSelector = "table.css-54wo6m"
|
|
housesSelector = "article.css-3xqrp1"
|
|
scrapTimes = 150
|
|
)
|
|
|
|
var (
|
|
city string
|
|
auctionDate time.Time
|
|
isUpToDate = false
|
|
cities = []string{"melbourne", "sydney"}
|
|
)
|
|
|
|
func main() {
|
|
dbConfig := config.Config{
|
|
Host: getEnvWithDefault("HOST", "home.toozhao.com"),
|
|
Port: getEnvWithDefault("PORT", "54322"),
|
|
DBName: getEnvWithDefault("DB_NAME", "data"),
|
|
Username: getEnvWithDefault("DB_USERNAME", "postgres"),
|
|
Password: getEnvWithDefault("DB_PASSWORD", "hahah"),
|
|
}
|
|
connStr := dbConfig.GetConnectionString()
|
|
|
|
db, err := gorm.Open("postgres", connStr)
|
|
if err != nil {
|
|
panic("failed to connect database")
|
|
}
|
|
defer db.Close()
|
|
|
|
// Migrate the schema
|
|
db.AutoMigrate(&status.ResultStatus{}, &model.House{}, &model.AuctionResult{})
|
|
|
|
handler := status.Handler{DB: db}
|
|
|
|
c := colly.NewCollector()
|
|
|
|
c.OnHTML("h1.css-1funogi .css-113axn7", func(e *colly.HTMLElement) {
|
|
latestStatus := handler.GetStatus()
|
|
if e.Text == latestStatus.Latest {
|
|
isUpToDate = true
|
|
log.Warn().Msgf("the result is been updated to latest %s, will exit now", e.Text)
|
|
return
|
|
}
|
|
|
|
latestStatus.Latest = e.Text
|
|
handler.SaveStatus(latestStatus)
|
|
})
|
|
|
|
c.OnHTML(clearanceInfoSelector, func(e *colly.HTMLElement) {
|
|
handlers.ScrapClearanceRate(city, auctionDate, db, e)
|
|
})
|
|
|
|
c.OnHTML(housesSelector, func(e *colly.HTMLElement) {
|
|
// if isUpToDate {
|
|
// return
|
|
// }
|
|
handlers.ScrapHouses(city, auctionDate, db, e)
|
|
|
|
})
|
|
|
|
c.OnRequest(func(r *colly.Request) {
|
|
log.Info().Msgf("visiting %s", r.URL)
|
|
})
|
|
|
|
// 2006-01-02
|
|
auctionDate, err = time.Parse(timeLayout, initDate)
|
|
if err != nil {
|
|
log.Panic().Msgf("failed to get date %v", err)
|
|
}
|
|
for _, cityVariable := range cities {
|
|
city = cityVariable
|
|
for i := 0; i < scrapTimes; i++ {
|
|
url := fmt.Sprintf("%s/%s/%s", urlTemplate, city, auctionDate.Format(timeLayout))
|
|
if err = c.Visit(url); err != nil {
|
|
if err.Error() == "Not Found" {
|
|
log.Warn().Msgf("Not fund data at %s with error %v will try next", url, err)
|
|
} else {
|
|
log.Panic().Msgf("failed to scrap content %v", err)
|
|
}
|
|
}
|
|
|
|
auctionDate = auctionDate.Add(-time.Hour * 24 * 7)
|
|
log.Info().Msgf("wil scrap the data for %s and url %s", auctionDate, url)
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
func getEnvWithDefault(key, defaultValue string) string {
|
|
val := os.Getenv(key)
|
|
if val != "" {
|
|
return val
|
|
}
|
|
return defaultValue
|
|
}
|