-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_play.go
99 lines (94 loc) · 2.31 KB
/
google_play.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
package scraper
import (
"errors"
"net/url"
"strconv"
"strings"
"github.com/betacraft/goquery"
)
func parsePlayStorePage(doc *goquery.Document) (*App, error) {
var err error
app := new(App)
itemprops := make([]goquery.Selection, 0)
// adding all itemprops
doc.Find("*").Each(func(i int, s *goquery.Selection) {
getItemprop(s, &itemprops)
})
splitData := strings.Split(doc.Url.String(), "?id=")
if len(splitData) < 2 {
return app, errors.New("invalid url")
}
app.PackageName = splitData[1]
// sanitizing data
for _, prop := range itemprops {
itemprop, _ := prop.Attr("itemprop")
switch itemprop {
case "image":
app.IconUrl, _ = prop.Attr("src")
app.IconUrl = strings.Split(app.IconUrl, "=w")[0]
app.IconUrl += "=w512"
case "name":
// checking parent to differ between author name and app name
parent, _ := prop.Html()
if strings.Contains(parent, "id-app-title") {
app.Name = prop.Text()
continue
}
if prop.Text() == "" {
continue
}
app.Author = prop.Text()
case "genre":
app.Genre = prop.Text()
case "price":
price, _ := prop.Attr("content")
app.Price = price
case "screenshot":
url, _ := prop.Attr("src")
app.ScreenshotUrls = append(app.ScreenshotUrls, url)
case "description":
app.Description = prop.Text()
case "aggregateRating":
app.AggregateRating = prop.Text()
case "ratingValue":
value, _ := prop.Attr("content")
app.RatingValue, err = strconv.ParseFloat(value, 64)
if err != nil {
return nil, err
}
case "ratingCount":
count, _ := prop.Attr("content")
app.RatingCount, err = strconv.Atoi(count)
if err != nil {
return nil, err
}
case "datePublished":
app.LastUpdated = prop.Text()
case "fileSize":
app.FileSize = prop.Text()
case "numDownloads":
app.Downloads = prop.Text()
case "softwareVersion":
app.VersionName = prop.Text()
case "operatingSystems":
app.OperatingSystem = prop.Text()
case "contentRating":
app.ContentRating = prop.Text()
}
}
return app, nil
}
func parsePlayStore(url *url.URL) (*App, error) {
doc, err := goquery.NewDocument(url.String())
if err != nil {
return nil, err
}
return parsePlayStorePage(doc)
}
func getItemprop(s *goquery.Selection, array *[]goquery.Selection) {
_, ok := s.Attr("itemprop")
if !ok {
return
}
*array = append(*array, *s)
}