-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
56 lines (50 loc) · 1.32 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"encoding/json"
"github.com/amazon/data-store"
"github.com/amazon/models"
"github.com/amazon/url-scraper/category-wise"
"github.com/amazon/url-scraper/single-product"
"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
"log"
"net/http"
"strings"
)
const defaultPort = "8000"
var store data_store.DataStore
func productInfo(c *gin.Context) {
link := c.Request.FormValue("url")
productInfo, err := single_product.ScrapeProductInfo(store, link)
if err != nil {
log.Printf("[Scraper Error]: %v\n", err)
}
viewInfo, err := json.Marshal(productInfo)
_, err = c.Writer.Write(viewInfo)
if err != nil {
log.Println(err)
}
}
func getAllProducts(c *gin.Context) {
category := &models.Category{
CategoryName: c.Request.FormValue("category"),
}
category.CategoryName = strings.ToLower(category.CategoryName)
err := category_wise.ScrapeAllProducts(store, *category)
if err != nil {
log.Printf("[Scraper Error]: %v\n", err)
}
_, err = c.Writer.WriteString("Category-wise Scraping: ok")
if err != nil {
log.Println(err)
}
}
func main() {
gin.SetMode(gin.ReleaseMode)
store = data_store.DbConnect()
router := gin.Default()
router.POST("/product", productInfo)
router.POST("/all-products", getAllProducts)
log.Println("server up...")
log.Fatal(http.ListenAndServe(":"+defaultPort, router))
}