-
Notifications
You must be signed in to change notification settings - Fork 1
/
lookup.go
80 lines (73 loc) · 1.86 KB
/
lookup.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package playstore
import (
"errors"
"net/http"
"net/url"
"regexp"
)
var (
appIdRegExp = regexp.MustCompile("[a-zA_Z_][\\.\\w]*")
)
var (
ErrInvalidPackageId = errors.New("invalid package id.")
ErrAppDoesNotExists = errors.New("the requested app wasn't found.")
)
// LookUp method always looks for the english version so it can match
// some attribute names.
func LookUp(httpGet httpGetFunc, appId string) (*App, error) {
if !appIdRegExp.MatchString(appId) {
return nil, ErrInvalidPackageId
}
res, err := doLookUpRequest(httpGet, appId, "en")
if err != nil {
return nil, err
}
document, err := NewPlayStoreDocument(res)
if err != nil {
return nil, err
}
if !isValidApp(document) {
return nil, ErrAppDoesNotExists
}
return parseApp(document, "en")
}
// MultiLookUp lets you retrieve the app information translated to other
// languages. English content is always fetched.
// When the requested language is not available a new entry is added to the
// map with the key equals to the lang code and an empty value.
func MultiLookUp(httpGet httpGetFunc, appId string, languages []string) (*App, error) {
app, err := LookUp(httpGet, appId)
if err != nil {
return nil, err
}
for _, lang := range languages {
if lang == "en" {
continue
}
res, err := doLookUpRequest(httpGet, appId, lang)
if err != nil {
continue
}
document, err := NewPlayStoreDocument(res)
if err != nil {
continue
}
app.parseDescription(document, lang)
}
return app, nil
}
func doLookUpRequest(httpGet httpGetFunc, appId string, lang string) (*http.Response, error) {
url := getLookUpUrl(appId, lang)
return httpGet(url)
}
func getLookUpUrl(appId string, lang string) *url.URL {
query := url.Values{}
query.Set("id", appId)
query.Set("hl", lang)
return &url.URL{
Scheme: "https",
Host: ENDPOINT,
Path: "/apps/details",
RawQuery: query.Encode(),
}
}