From ff50c6238c92d9a12334b03721ecbefe335f87d5 Mon Sep 17 00:00:00 2001 From: Junv Zhao Date: Thu, 17 May 2018 21:04:35 +1000 Subject: [PATCH] Init project --- .gitignore | 2 ++ main.go | 21 ++++++++++++++++ models/product.go | 26 ++++++++++++++++++++ runner/kogan.go | 61 +++++++++++++++++++++++++++++++++++++++++++++++ runner/runner.go | 6 +++++ 5 files changed, 116 insertions(+) create mode 100644 .gitignore create mode 100644 main.go create mode 100644 models/product.go create mode 100644 runner/kogan.go create mode 100644 runner/runner.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ec73e4a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +shopping +/vendor diff --git a/main.go b/main.go new file mode 100644 index 0000000..dd9a0b8 --- /dev/null +++ b/main.go @@ -0,0 +1,21 @@ +package main + +import "github.com/gin-gonic/gin" +import _ "github.com/jinzhu/gorm/dialects/postgres" + +func init() { + +} + +func main() { + r := gin.Default() + r.GET("/health", func(c *gin.Context) { + c.JSON(200, gin.H{ + "message": "Everything is OK", + }) + }) + r.GET("/api/products", func(c *gin.Context) { + c.JSON(200, make([]string, 0)) + }) + r.Run() +} diff --git a/models/product.go b/models/product.go new file mode 100644 index 0000000..3a5f724 --- /dev/null +++ b/models/product.go @@ -0,0 +1,26 @@ +package models + +import ( + "time" + + "github.com/jinzhu/gorm" +) + +// Product the model for product item +type Product struct { + gorm.Model + ULID string + Name string + CurrentPrice float64 + URL string + Brand string + Nation string +} + +// Price the price for a single day +type Price struct { + gorm.Model + ProductID string + Price string + DateTime time.Time +} diff --git a/runner/kogan.go b/runner/kogan.go new file mode 100644 index 0000000..ba654ee --- /dev/null +++ b/runner/kogan.go @@ -0,0 +1,61 @@ +package runner + +import ( + "fmt" + "net/http" + "time" + + "github.com/asciimoo/colly" +) + +var ( + baseURL = "https://www.kogan.com" + initURL = "https://www.kogan.com/au/buy/microsoft-surface-book-128gb-i5-8gb-ram-certified-refurbished/" +) + +// KoganRunner kogan.com runner +type KoganRunner struct { +} + +func (runner *KoganRunner) run() { + // Instantiate default collector + c := colly.NewCollector() + c.MaxDepth = 1 + // Visit only domains: hackerspaces.org, wiki.hackerspaces.org + // c.AllowedDomains = []string{"hackerspaces.org", "tuchong.com"} + + // c.OnHTML("title", func(e *colly.HTMLElement) { + // fmt.Printf("title is: %s \n", e.Text) + // }) + // + c.OnHTML(".product-info__wrapper", func(e *colly.HTMLElement) { + price := e.ChildAttr("h3[itemprop='price']", "content") + product := e.ChildText("h1[itemprop='name']") + fmt.Printf("The %s price is: %s and url is: %s \n", product, price, e.Request.URL) + }) + + // On every a element which has href attribute call callback + c.OnHTML("a", func(e *colly.HTMLElement) { + if title := e.Attr("title"); title == "View Product" { + href := e.Attr("href") + fmt.Printf("Product found: %s -> %s\n", title, BaseURL+href) + time.Sleep(time.Second * 2) + c.Visit(e.Request.AbsoluteURL(href)) + } + }) + + // c.OnHTML(".post-photos img[src]", func(e *colly.HTMLElement) { + // fmt.Println("---image found----:" + e.Attr("src")) + // }) + + // Before making a request print "Visiting ..." + c.OnRequest(func(r *colly.Request) { + + // myHeaders := map[string][]string{"User-Agent": []string{"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"}} + // headers.Add("User-Agent", ) + r.Headers = &http.Header{"User-Agent": []string{"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"}} + fmt.Println("Visiting", r.URL.String()) + }) + + c.Visit(initURL) +} diff --git a/runner/runner.go b/runner/runner.go new file mode 100644 index 0000000..a60c628 --- /dev/null +++ b/runner/runner.go @@ -0,0 +1,6 @@ +package runner + +// Runner the runner interface +type Runner interface { + run() +}