-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappstore.go
More file actions
84 lines (70 loc) · 1.78 KB
/
appstore.go
File metadata and controls
84 lines (70 loc) · 1.78 KB
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
81
82
83
84
package appcrawl
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"strconv"
)
type appStoreReponse struct {
Count float64 `json:"resultCount"`
Results []appStoreApp `json:"results"`
}
type appStoreApp struct {
Name string `json:"trackName"`
Package string `json:"bundleId"`
PublisherName string `json:"artistName"`
StoreUrl string `json:"trackViewUrl"`
IconUrl string `json:"artworkUrl60"`
AppVersion string `json:"version"`
Rating float32 `json:"averageUserRating"`
RatingCount int `json:"userRatingCount"`
}
type AppStore struct{}
func (a AppStore) searchUrl(sr SearchRequest) string {
u, _ := url.Parse("https://itunes.apple.com/search")
q := u.Query()
if sr.Country != "" {
q.Set("country", sr.Country)
}
if sr.Limit > 0 {
q.Set("limit", strconv.Itoa(sr.Limit))
}
if sr.Query != "" {
q.Set("term", sr.Query)
}
q.Set("media", "software")
u.RawQuery = q.Encode()
return u.String()
}
func (a AppStore) Search(sr SearchRequest) ([]App, error) {
url := a.searchUrl(sr)
result, err := http.Get(url)
if err != nil {
return nil, err
}
defer result.Body.Close()
// TODO: make this steaming, so it can stop after
// finding a specific bundle
response, err := ioutil.ReadAll(result.Body)
if err != nil {
return nil, err
}
var appresults appStoreReponse
err = json.Unmarshal(response, &appresults)
if err != nil {
return nil, err
}
apps := make([]App, len(appresults.Results))
for i, v := range appresults.Results {
apps[i].Name = v.Name
apps[i].Package = v.Package
apps[i].PublisherName = v.PublisherName
apps[i].StoreUrl = v.StoreUrl
apps[i].IconUrl = v.IconUrl
apps[i].AppVersion = v.AppVersion
apps[i].Rating = v.Rating
apps[i].RatingCount = v.RatingCount
}
return apps, nil
}