/
category.go
53 lines (42 loc) · 1.24 KB
/
category.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
package category
import (
"net/http"
"github.com/n0madic/google-play-scraper/internal/util"
"github.com/n0madic/google-play-scraper/pkg/scraper"
"github.com/n0madic/google-play-scraper/pkg/store"
)
// Options type alias
type Options = scraper.Options
// List of clusters
type List map[string]*scraper.Scraper
// New return category list instance
func New(category store.Category, age store.Age, options Options) (List, error) {
path := ""
if category != "" {
path += "/category/" + string(category)
}
req, err := http.NewRequest("GET", scraper.BaseURL+path, nil)
if err != nil {
return nil, err
}
if age != "" {
q := req.URL.Query()
q.Add("age", string(age))
req.URL.RawQuery = q.Encode()
}
data, err := util.GetInitData(req)
if err != nil {
return nil, err
}
list := make(List)
clusterList := util.GetJSONArray(data["ds:3"], "0.1")
for _, cluster := range clusterList {
key := util.GetJSONValue(cluster.String(), "20.0", "21.1.0", "22.1.0")
url, err := util.AbsoluteURL(scraper.BaseURL, util.GetJSONValue(cluster.String(), "20.2.4.2", "21.1.2.4.2", "22.1.2.4.2"))
if key != "" && err == nil {
list[key] = scraper.New(url, &options)
list[key].ParseResult(cluster.String(), "21.0", "22.0")
}
}
return list, nil
}