mirror of https://github.com/fmartingr/bazaar.git
feat: akiracomics improvements
* added tests * extracting description * using ShopOptions to perform request
This commit is contained in:
parent
0cf15e589a
commit
3854a1fe11
File diff suppressed because it is too large
Load Diff
|
@ -2,7 +2,6 @@ package akiracomics
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
@ -19,22 +18,26 @@ type AkiraShop struct {
|
|||
}
|
||||
|
||||
func (s *AkiraShop) Get(url string) (*models.Product, error) {
|
||||
res, err := http.Get(url)
|
||||
body, err := s.ShopOptions.Client.Get(url)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error retrieving url: %s", err)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode != 200 {
|
||||
return nil, fmt.Errorf("error retrieving url: %d %s", res.StatusCode, res.Status)
|
||||
return nil, fmt.Errorf("error during request: %s", err)
|
||||
}
|
||||
|
||||
doc, err := goquery.NewDocumentFromReader(res.Body)
|
||||
doc, err := goquery.NewDocumentFromReader(body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing body: %s", err)
|
||||
}
|
||||
|
||||
description, err := doc.Find(`[itemprop="description"]`).Html()
|
||||
if err != nil {
|
||||
// TODO error logging
|
||||
} else {
|
||||
description = strings.Replace(description, "<br/>", "\n", -1)
|
||||
}
|
||||
|
||||
product := models.Product{
|
||||
URL: url,
|
||||
URL: url,
|
||||
Description: description,
|
||||
}
|
||||
|
||||
doc.Find(`div.panel-ficha-producto div.panel-grupo`).Each(func(i int, s *goquery.Selection) {
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
package akiracomics_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/fmartingr/bazaar/pkg/clients"
|
||||
"github.com/fmartingr/bazaar/pkg/models"
|
||||
"github.com/fmartingr/bazaar/pkg/shop/akiracomics"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestAkiraComics_Ok(t *testing.T) {
|
||||
shop := akiracomics.NewAkiraShopFactory()(models.NewShopOptions(clients.NewMockClient()))
|
||||
|
||||
testUrl := "https://www.akiracomics.com/test/"
|
||||
|
||||
product, err := shop.Get(testUrl)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
t.Log(product)
|
||||
|
||||
assert.Equal(t, "TODO PARECÍA DEMASIADO FÁCIL.\n\nLos amigos y aliados de Rimuru siguen desconcertados por su ascenso, pero algo los preocupa aún más: el regreso de Veldora, el temido Dragón Tormenta. Al mismo tiempo, la lentitud y la cautela de los súbditos de Rimuru hacen presagiar futuras luchas mayores que están por venir y alteran el estado de poder que se ha creado.", product.Description)
|
||||
assert.Equal(t, "AQUELLA VEZ QUE ME CONVERTI EN SLIME VOL.16 [RUSTICA]", product.Name)
|
||||
assert.Equal(t, "https://www.akiracomics.com/imagenes/poridentidad?identidad=24552a54-365d-4d31-a73e-9fd5f927c3a0&ancho=900&alto=", product.ImageURL)
|
||||
assert.Equal(t, 8.55, product.Price)
|
||||
assert.Equal(t, "8,55 €", product.PriceText)
|
||||
assert.Equal(t, testUrl, product.URL)
|
||||
}
|
Loading…
Reference in New Issue