/
details.go
211 lines (194 loc) · 8.68 KB
/
details.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package appstore
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"gopkg.in/guregu/null.v4"
)
type Details struct {
AppId AppId `json:"app_id"`
BundleId string `json:"bundle_id"`
Title string `json:"title"`
Url string `json:"url"`
Description string `json:"description"`
Icon string `json:"icon"`
Genres []string `json:"genres"`
GenreIds []int64 `json:"genre_ids"`
PrimaryGenre string `json:"primary_genre"`
PrimaryGenreId int64 `json:"primary_genre_id"`
ContentRating string `json:"content_rating"`
ContentAdvisories []string `json:"content_advisories"`
Languages []string `json:"languages"`
Size int64 `json:"size"`
RequiredOsVersion string `json:"required_os_version"`
Released null.Time `json:"released"`
Updated null.Time `json:"updated"`
ReleaseNotes string `json:"release_notes"`
Version string `json:"version"`
Price float64 `json:"price"`
Currency string `json:"currency"`
DeveloperId int64 `json:"developer_id"`
Developer string `json:"developer"`
DeveloperUrl string `json:"developer_url"`
DeveloperWebsite string `json:"developer_website"`
Score float64 `json:"score"`
Reviews int64 `json:"reviews"`
CurrentVersionScore null.Float `json:"current_version_score"`
CurrentVersionReviews int64 `json:"current_version_reviews"`
Screenshots []string `json:"screenshots"`
IpadScreenshots []string `json:"ipad_screenshots"`
AppletvScreenshots []string `json:"appletv_screenshots"`
SupportedDevices []string `json:"supported_devices"`
}
func ScrapeDetails(ctx context.Context, client *http.Client, appIds []AppId) (map[AppId]Details, error) {
req, err := http.NewRequestWithContext(ctx, "GET", "https://itunes.apple.com/lookup", nil)
if err != nil {
return nil, err
}
q := req.URL.Query()
q.Add("entity", "software")
q.Add("id", commaSeparatedAppIDs(appIds))
req.URL.RawQuery = q.Encode()
resp, err := client.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
if resp.StatusCode == http.StatusTooManyRequests {
return nil, ErrRateLimited
} else {
return nil, fmt.Errorf("ScrapeDetails: %s", resp.Status)
}
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var lookupResponse lookupResponse
err = json.Unmarshal(body, &lookupResponse)
if err != nil {
return nil, err
}
// If an ID does not exist, Apple's API just ignores it
result := make(map[AppId]Details)
detailsList, err := lookupResponse.ToDetails()
if err != nil {
return nil, err
}
for _, details := range detailsList {
result[details.AppId] = details
}
return result, err
}
func (lr *lookupResponse) ToDetails() ([]Details, error) {
detailsList := make([]Details, 0, lr.ResultCount)
for _, result := range lr.Results {
var icon string
if result.ArtworkURL512 != "" {
icon = result.ArtworkURL512
} else if result.ArtworkURL100 != "" {
icon = result.ArtworkURL100
} else {
icon = result.ArtworkURL60
}
genreIds := make([]int64, 0, len(result.GenreIds))
for _, genreId := range result.GenreIds {
genreId, err := strconv.ParseInt(genreId, 10, 64)
if err != nil {
return nil, err
}
genreIds = append(genreIds, genreId)
}
details := Details{
AppId: AppId(result.TrackID),
BundleId: result.BundleID,
Title: result.TrackName,
Url: result.TrackViewURL,
Description: result.Description,
Icon: icon,
Genres: result.Genres,
GenreIds: genreIds,
PrimaryGenre: result.PrimaryGenreName,
PrimaryGenreId: result.PrimaryGenreID,
ContentRating: result.ContentAdvisoryRating,
ContentAdvisories: result.Advisories,
Languages: result.LanguageCodesISO2A,
Size: result.FileSizeBytes,
RequiredOsVersion: result.MinimumOsVersion,
Released: result.ReleaseDate,
Updated: result.CurrentVersionReleaseDate,
ReleaseNotes: result.ReleaseNotes,
Version: result.Version,
Price: result.Price,
Currency: result.Currency,
DeveloperId: result.ArtistID,
Developer: result.ArtistName,
DeveloperUrl: result.ArtistViewURL,
DeveloperWebsite: result.SellerURL,
Score: result.AverageUserRating,
Reviews: result.UserRatingCount,
CurrentVersionScore: result.AverageUserRatingForCurrentVersion,
CurrentVersionReviews: result.UserRatingCountForCurrentVersion,
Screenshots: result.ScreenshotUrls,
IpadScreenshots: result.IpadScreenshotUrls,
AppletvScreenshots: result.AppletvScreenshotUrls,
SupportedDevices: result.SupportedDevices,
}
detailsList = append(detailsList, details)
}
return detailsList, nil
}
// (Mostly) automatically generated using https://mholt.github.io/json-to-go/
type lookupResponse struct {
ResultCount int `json:"resultCount"`
Results []struct {
ScreenshotUrls []string `json:"screenshotUrls"`
IpadScreenshotUrls []string `json:"ipadScreenshotUrls"`
AppletvScreenshotUrls []string `json:"appletvScreenshotUrls"`
ArtworkURL60 string `json:"artworkUrl60"`
ArtworkURL512 string `json:"artworkUrl512"`
ArtworkURL100 string `json:"artworkUrl100"`
ArtistViewURL string `json:"artistViewUrl"`
Features []interface{} `json:"features"`
SupportedDevices []string `json:"supportedDevices"`
Advisories []string `json:"advisories"`
IsGameCenterEnabled bool `json:"isGameCenterEnabled"`
Kind string `json:"kind"`
MinimumOsVersion string `json:"minimumOsVersion"`
TrackCensoredName string `json:"trackCensoredName"`
LanguageCodesISO2A []string `json:"languageCodesISO2A"`
FileSizeBytes int64 `json:"fileSizeBytes,string"`
SellerURL string `json:"sellerUrl"`
FormattedPrice string `json:"formattedPrice"`
ContentAdvisoryRating string `json:"contentAdvisoryRating"`
AverageUserRatingForCurrentVersion null.Float `json:"averageUserRatingForCurrentVersion"`
UserRatingCountForCurrentVersion int64 `json:"userRatingCountForCurrentVersion"`
AverageUserRating float64 `json:"averageUserRating"`
TrackViewURL string `json:"trackViewUrl"`
TrackContentRating string `json:"trackContentRating"`
BundleID string `json:"bundleId"`
Currency string `json:"currency"`
TrackID int64 `json:"trackId"`
TrackName string `json:"trackName"`
ReleaseDate null.Time `json:"releaseDate"`
SellerName string `json:"sellerName"`
PrimaryGenreName string `json:"primaryGenreName"`
GenreIds []string `json:"genreIds"`
IsVppDeviceBasedLicensingEnabled bool `json:"isVppDeviceBasedLicensingEnabled"`
CurrentVersionReleaseDate null.Time `json:"currentVersionReleaseDate"`
ReleaseNotes string `json:"releaseNotes"`
PrimaryGenreID int64 `json:"primaryGenreId"`
Description string `json:"description"`
ArtistID int64 `json:"artistId"`
ArtistName string `json:"artistName"`
Genres []string `json:"genres"`
Price float64 `json:"price"`
Version string `json:"version"`
WrapperType string `json:"wrapperType"`
UserRatingCount int64 `json:"userRatingCount"`
} `json:"results"`
}