init parser
This commit is contained in:
113
plati/categories.go
Normal file
113
plati/categories.go
Normal file
@ -0,0 +1,113 @@
|
||||
package plati
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Categories []*Category
|
||||
|
||||
func (categories Categories) FlatNames(nameOfParent string) {
|
||||
for _, c := range categories {
|
||||
name := ""
|
||||
if len(c.Name) > 0 {
|
||||
name = c.Name[0].Value
|
||||
}
|
||||
if c.Title != "" {
|
||||
name = c.Title + "/" + name
|
||||
}
|
||||
c.FlatName = fmt.Sprintf("%s/%s", nameOfParent, name)
|
||||
if len(c.Children) > 0 {
|
||||
c.Children.FlatNames(c.FlatName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (categories Categories) FixParentID(parentID int) {
|
||||
for _, c := range categories {
|
||||
if c.ParentID == 0 {
|
||||
c.ParentID = parentID
|
||||
}
|
||||
c.Children.FixParentID(c.ID)
|
||||
}
|
||||
}
|
||||
|
||||
type Category struct {
|
||||
FlatName string `json:"flat_name,omitempty"`
|
||||
ID int `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Level int `json:"level"`
|
||||
ParentID int `json:"parent_id"`
|
||||
Name []struct {
|
||||
Locale string `json:"locale"`
|
||||
Value string `json:"value"`
|
||||
} `json:"name"`
|
||||
Children Categories `json:"children"`
|
||||
CanAdd bool `json:"can_add"`
|
||||
}
|
||||
|
||||
type CategoriesResponse struct {
|
||||
Retval int `json:"retval"`
|
||||
Retdesc any `json:"retdesc"`
|
||||
Errors any `json:"errors"`
|
||||
Content Categories `json:"content"`
|
||||
}
|
||||
|
||||
func (c *Client) GetCategories(ctx context.Context) (*CategoriesResponse, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", "https://api.digiseller.ru/api/dictionary/platforms/categories/plati", http.NoBody)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("new request: %w", err)
|
||||
}
|
||||
req.Header.Add("Accept", "application/json")
|
||||
|
||||
resp, err := c.httpCli.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("http do: %w", err)
|
||||
}
|
||||
|
||||
// dump, err := httputil.DumpResponse(resp, true)
|
||||
// log.Printf("%d %s", resp.StatusCode, string(dump))
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
out := CategoriesResponse{}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
|
||||
return nil, fmt.Errorf("json decode: %w", err)
|
||||
}
|
||||
|
||||
return &out, nil
|
||||
}
|
||||
|
||||
type SubCategories struct {
|
||||
Retval int `json:"retval"`
|
||||
Retdesc any `json:"retdesc"`
|
||||
Errors any `json:"errors"`
|
||||
Content []*Category `json:"content"`
|
||||
}
|
||||
|
||||
func (c *Client) GetSubCategories(ctx context.Context, categoryID int) (*SubCategories, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://api.digiseller.ru/api/dictionary/platforms/subcategories/%d", categoryID), http.NoBody)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("new request: %w", err)
|
||||
}
|
||||
req.Header.Add("Accept", "application/json")
|
||||
|
||||
resp, err := c.httpCli.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("http do: %w", err)
|
||||
}
|
||||
|
||||
// dump, err := httputil.DumpResponse(resp, true)
|
||||
// log.Printf("%d %s", resp.StatusCode, string(dump))
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
out := SubCategories{}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
|
||||
return nil, fmt.Errorf("json decode: %w", err)
|
||||
}
|
||||
|
||||
return &out, nil
|
||||
}
|
17
plati/client.go
Normal file
17
plati/client.go
Normal file
@ -0,0 +1,17 @@
|
||||
package plati
|
||||
|
||||
import "net/http"
|
||||
|
||||
type Client struct {
|
||||
httpCli *http.Client
|
||||
token string
|
||||
sellerID int
|
||||
}
|
||||
|
||||
func New() *Client {
|
||||
return &Client{
|
||||
httpCli: &http.Client{},
|
||||
token: "7C731D89FED84B479B89F24F81BB8AF2",
|
||||
sellerID: 1209592,
|
||||
}
|
||||
}
|
103
plati/goodscategory.go
Normal file
103
plati/goodscategory.go
Normal file
@ -0,0 +1,103 @@
|
||||
package plati
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
)
|
||||
|
||||
func (c *Client) GetBlockGoodsCategory(ctx context.Context, idC int, idR int, sort string, page int, rows int, curr string, lang string) ([]*Good, error) {
|
||||
u := fmt.Sprintf("https://plati.market/asp/block_goods_category.asp?preorders=0&id_cb=0&id_c=%d&id_r=%d&sort=%s&page=%d&rows=%d&curr=%s&pp_only=false&lang=ru-RU&rnd=%f", idC, idR, sort, page, rows, curr, rand.Float32())
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", u, http.NoBody)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("new request: %w", err)
|
||||
}
|
||||
|
||||
dump, err := httputil.DumpRequestOut(req, false)
|
||||
log.Printf("%s\n", string(dump))
|
||||
|
||||
resp, err := c.httpCli.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("http do: %w", err)
|
||||
}
|
||||
|
||||
// dump, err = httputil.DumpResponse(resp, true)
|
||||
// log.Printf("%d %s", resp.StatusCode, string(dump))
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
goods, err := parseGoodsCategory(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse goods category: %w", err)
|
||||
}
|
||||
|
||||
return goods, nil
|
||||
}
|
||||
|
||||
type Good struct {
|
||||
Name string
|
||||
GoodLink string
|
||||
Seller string
|
||||
SellerLink string
|
||||
SellerRating int
|
||||
Sold int
|
||||
Price float64
|
||||
}
|
||||
|
||||
func parseGoodsCategory(r io.Reader) ([]*Good, error) {
|
||||
doc, err := goquery.NewDocumentFromReader(r)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("goquery new document: %w", err)
|
||||
}
|
||||
|
||||
goods := make([]*Good, 0)
|
||||
|
||||
doc.Find("table.goods-table-category>tbody>tr").Each(func(i int, tr *goquery.Selection) {
|
||||
good := Good{}
|
||||
goods = append(goods, &good)
|
||||
tr.Find("td").Each(func(i int, td *goquery.Selection) {
|
||||
if td.HasClass("product-title") {
|
||||
titleBlock := td.Find("a").First()
|
||||
good.Name = titleBlock.Text()
|
||||
good.GoodLink, _ = titleBlock.Attr("href")
|
||||
}
|
||||
if td.HasClass("product-merchant") {
|
||||
titleBlock := td.Find("a").First()
|
||||
good.Seller = titleBlock.Text()
|
||||
good.SellerLink, _ = titleBlock.Attr("href")
|
||||
spanBlock := td.Find("span").First()
|
||||
sellerRating, err := strconv.ParseInt(spanBlock.Text(), 10, 64)
|
||||
if err != nil {
|
||||
log.Printf("cannot parse seller rating %s", spanBlock.Text())
|
||||
}
|
||||
good.SellerRating = int(sellerRating)
|
||||
}
|
||||
if td.HasClass("product-sold") {
|
||||
sold, err := strconv.ParseInt(td.Text(), 10, 64)
|
||||
if err != nil {
|
||||
log.Printf("cannot parse product sold %s", td.Text())
|
||||
}
|
||||
good.Sold = int(sold)
|
||||
}
|
||||
if td.HasClass("product-price") {
|
||||
productPriceText := strings.Fields(td.Text())[0]
|
||||
productPrice, err := strconv.ParseFloat(productPriceText, 64)
|
||||
if err != nil {
|
||||
log.Printf("cannot parse product price %s: %v", td.Text(), err)
|
||||
}
|
||||
good.Price = productPrice
|
||||
}
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
return goods, nil
|
||||
}
|
53
plati/goodscategory_test.go
Normal file
53
plati/goodscategory_test.go
Normal file
@ -0,0 +1,53 @@
|
||||
package plati
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_parseGoodsCategory(t *testing.T) {
|
||||
type args struct {
|
||||
body io.Reader
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want []*Good
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "final_fantasy",
|
||||
args: args{
|
||||
body: bytes.NewReader([]byte(`<span class="GoodsBlock_oneline" id="GoodsBlock_1298"><a name=1298></a><div class="table_header clearfix"><h2 class="games-header"> <span><i class="platiru-loader" id="loader_1298"></i></span></h2></div><div><table class="goods-table goods-table-category"><thead><tr><th colspan="3" class="product-title">Название товара</td><th class="product-merchant"><div class="nowrap">Продавец <span class="rating_title">рейтинг</span></div></td><th class="product-sold">Продано</td><th class="product-price"><select onchange="return Goods_C('0',0, 1298,9901,'','cntSellDESC', 1, 100, this.value);"><option value=USD >USD</option><option value=RUR selected>RUB</option><option value=EUR >EUR</option><option value=UAH >UAH</option></select></div></tr></thead><td class="product-checkbox"><div><a class="product-to-notepad" onclick="return noteItem(4026129,0)"><i id="n4026129" class="unchecked" title="В закладки"></i></a></div></td><td class="product-chat"><div><i data-tooltip="m=chat" class="chat_online" onclick="return PopUp(197847, 0);"></i></a></div></td><td class="product-title"><div><a href="/itm/final-fantasy-type-0-hd-steam-gift-ru/4026129" title="FINAL FANTASY TYPE-0 HD (Steam Gift Россия)">FINAL FANTASY TYPE-0 HD (Steam Gift Россия)</a></div></td><td class="product-merchant" data-th='Продавец' class="product-merchant"><div><a href="/seller/igorderish/197847">IgorDeRish</a> <span>582</span></div></td><td class="product-sold" data-th='Продано' class="product-sold"><div>1</div></td><td data-th='Цена' class="product-price"><div class="product-price-inner"><div>498 </span>руб. </div></div></td></tr></table><div class="sort_wrapper"><div class="pages_nav" style="height:25px"></div></div></div>`)),
|
||||
},
|
||||
want: []*Good{
|
||||
{
|
||||
Name: "FINAL FANTASY TYPE-0 HD (Steam Gift Россия)",
|
||||
GoodLink: "/itm/final-fantasy-type-0-hd-steam-gift-ru/4026129",
|
||||
Seller: "IgorDeRish",
|
||||
SellerLink: "/seller/igorderish/197847",
|
||||
SellerRating: 582,
|
||||
Sold: 1,
|
||||
Price: 498.0,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := parseGoodsCategory(tt.args.body)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("parseGoodsCategory() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
gotJson, _ := json.Marshal(got)
|
||||
wantJson, _ := json.Marshal(tt.want)
|
||||
t.Errorf("parseGoodsCategory() = %s, want %s", string(gotJson), string(wantJson))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user