Skip to content

Commit 6f7e3c7

Browse files
committed
Hepsiburada tamamladım. Buna bir de open şeysi eklersek nice olur gibi :D. Şu anda table olarak görünüyor. Harika ya xD
1 parent f5e2dea commit 6f7e3c7

File tree

6 files changed

+42
-11
lines changed

6 files changed

+42
-11
lines changed

cmd/hepsiburada.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ limitations under the License.
1616
package cmd
1717

1818
import (
19-
"fmt"
2019
"log"
2120

2221
"github.com/aligoren/fiyatine/internal/models"
22+
"github.com/aligoren/fiyatine/internal/render"
2323
"github.com/aligoren/fiyatine/internal/services"
2424
"github.com/spf13/cobra"
2525
)
@@ -39,13 +39,20 @@ var hepsiburadaCmd = &cobra.Command{
3939

4040
service := services.BaseService{ProductService: hb}
4141

42-
results := service.Search()
42+
products := service.Search()
4343

44-
if len(results) == 0 {
44+
if len(products) == 0 {
4545
log.Println("Hepsiburada sitesinde aradığınız kriterlere uygun ürün bulunamadı")
4646
}
4747

48-
fmt.Printf("%v\n", results)
48+
headers := []string{"Satıcı", "ID", "Ürün Adı", "Fiyat"}
49+
rows := [][]string{}
50+
51+
for _, product := range products {
52+
rows = append(rows, []string{"Hepsiburada", product.ID, product.Title, product.Price})
53+
}
54+
55+
render.RenderOutput(headers, rows)
4956
},
5057
}
5158

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ require (
1515
github.com/hashicorp/hcl v1.0.0 // indirect
1616
github.com/inconshreveable/mousetrap v1.0.0 // indirect
1717
github.com/magiconair/properties v1.8.6 // indirect
18+
github.com/mattn/go-runewidth v0.0.9 // indirect
1819
github.com/mitchellh/mapstructure v1.5.0 // indirect
20+
github.com/olekukonko/tablewriter v0.0.5 // indirect
1921
github.com/pelletier/go-toml v1.9.5 // indirect
2022
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
2123
github.com/spf13/afero v1.8.2 // indirect

go.sum

+4
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,14 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
140140
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
141141
github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo=
142142
github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
143+
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
144+
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
143145
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
144146
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
145147
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
146148
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
149+
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
150+
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
147151
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
148152
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
149153
github.com/pelletier/go-toml/v2 v2.0.1 h1:8e3L2cCQzLFi2CR4g7vGFuFxX7Jl1kKX8gW+iV0GUKU=

internal/models/response_model.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package models
22

33
type ResponseModel struct {
4-
ID string
5-
Title string
6-
Url string
7-
Price float64
4+
Vendor string
5+
ID string
6+
Title string
7+
Url string
8+
Price string
89
}

internal/parsers/hepsiburada_parser.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package parsers
22

33
import (
4+
"fmt"
45
"io"
56
"log"
6-
"strconv"
77
"strings"
88

99
"github.com/PuerkitoBio/goquery"
@@ -31,7 +31,6 @@ func (p HepsiburadaParser) parseServiceResponse() []models.ResponseModel {
3131
priceData := s.Find("div[data-test-id='price-current-price']").Contents().FilterFunction(func(i int, s *goquery.Selection) bool {
3232
return !s.Is("span")
3333
}).Text()
34-
price, _ := strconv.ParseFloat(strings.Replace(priceData, ",", ".", 1), 64)
3534

3635
splitUrl := strings.Split(url, "-")
3736
id := splitUrl[len(splitUrl)-1]
@@ -41,7 +40,7 @@ func (p HepsiburadaParser) parseServiceResponse() []models.ResponseModel {
4140
ID: id,
4241
Title: productTitle,
4342
Url: url,
44-
Price: price,
43+
Price: fmt.Sprintf("₺%s", priceData),
4544
})
4645
}
4746
})

internal/render/render.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package render
2+
3+
import (
4+
"os"
5+
6+
"github.com/olekukonko/tablewriter"
7+
)
8+
9+
func RenderOutput(headers []string, rows [][]string) {
10+
table := tablewriter.NewWriter(os.Stdout)
11+
12+
table.SetHeader(headers)
13+
table.SetAutoWrapText(false)
14+
table.SetRowLine(true)
15+
16+
table.AppendBulk(rows)
17+
table.Render()
18+
}

0 commit comments

Comments
 (0)