-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplay-info.go
203 lines (169 loc) · 5.53 KB
/
play-info.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
package main
import (
"fmt"
"github.com/PuerkitoBio/goquery"
"github.com/codegangsta/cli"
"github.com/mgutz/ansi"
"os"
"sort"
str "strings"
)
var baseString = "https://play.google.com/store/apps/details?id="
var baseYouTubeURL = "https://www.youtube.com/watch?v="
// for my own
const debug = false
var divider = fmt.Sprintf(ansi.Color(str.Repeat("-", 56)+"\n", "yellow"))
func main() {
app := cli.NewApp()
app.Name = "Play Go"
app.Usage = "Get app info via commandline"
app.Version = "0.1.0"
app.Action = func(c *cli.Context) {
arg := c.Args()
if len(arg) == 0 {
fmt.Println(ansi.Color("Error : Please enter package name", "red"))
} else {
fmt.Println("")
pkg := c.Args()[0]
fmt.Printf(ansi.Color("Processing Results for \"%s\"\n", "green"), pkg)
fmt.Println(divider)
getData(c.Args()[0])
fmt.Println("")
fmt.Println(divider)
}
}
app.Run(os.Args)
}
func getData(pkgName string) {
// Using with file
var doc *goquery.Document
var err error
if debug {
f, err := os.Open("yoteshin.html")
panicIf(err)
defer f.Close()
doc, err = goquery.NewDocumentFromReader(f)
} else {
doc, err = goquery.NewDocument(fmt.Sprintf("%s%s", baseString, pkgName))
panicIf(err)
}
tmp := make(map[titleMap]string)
tmp[titleMap{1, "Title"}] = doc.Find(`div[itemprop='name']`).First().Text()
tmp[titleMap{2, "Category"}] = str.TrimSpace(doc.Find(".category").First().Text())
price := str.TrimSpace(doc.Find(".price").First().Text())
if price == "Install" {
tmp[titleMap{3, "Price"}] = "Free"
} else {
tmp[titleMap{3, "Price"}] = str.TrimSpace(doc.Find(".price").First().Text())
}
doc.Find(".meta-info").Each(func(i int, s *goquery.Selection) {
fieldName := str.TrimSpace(s.Find(".title").Text())
switch fieldName {
case "Updated":
tmp[titleMap{4, "Updated"}] = s.Find(".content").Text()
case "Installs":
tmp[titleMap{5, "Total Installs"}] = s.Find(".content").Text()
case "Size":
tmp[titleMap{6, "Size"}] = s.Find(".content").Text()
case "Current Version":
tmp[titleMap{7, "Current Version"}] = s.Find(".content").Text()
case "Requires Android":
tmp[titleMap{8, "Requires Android"}] = s.Find(".content").Text()
case "Content Rating":
tmp[titleMap{9, "Content Rating"}] = s.Find(".content").Text()
case "Developer":
// Ugly hack
s.Find(".dev-link").Each(func(i int, t *goquery.Selection) {
nodeHref, _ := t.Attr("href")
if str.Contains(nodeHref, "mailto:") {
tmp[titleMap{10, "Email"}] = str.Split(nodeHref, "mailto:")[1]
} else {
raw := str.Split(nodeHref, "&")[0]
tmp[titleMap{11, "Website"}] = str.Split(raw, "q=")[1]
}
})
}
})
score := doc.Find(".score-container").First()
if score != nil {
tmp[titleMap{12, "Score"}] = score.Find(".score").First().Text()
node := doc.Find(`meta[itemprop='ratingCount']`)
v, _ := node.Attr("content")
tmp[titleMap{13, "Votes"}] = v
}
tmp[titleMap{14, "Developer"}] = str.TrimSpace((doc.Find(`div[itemprop='author']`).Find(".primary").Text()))
tmp[titleMap{15, "What's New"}] = doc.Find(".whatsnew .recent-change").Text()
tmp[titleMap{16, "Description"}] = doc.Find(`div[itemprop='description']`).First().Text()
tmp[titleMap{17, "App Id"}] = pkgName
tmp[titleMap{18, "Icon Url"}], _ = doc.Find(".cover-image").Attr("src")
// Should we make slice or just an string ?
var imgSlice []string
doc.Find(".full-screenshot").Each(func(i int, s *goquery.Selection) {
fsLinks, _ := s.Attr("src")
imgSlice = append(imgSlice, fsLinks)
})
for _, imgSliceLinks := range imgSlice {
tmp[titleMap{19, "Full Screenshot"}] += fmt.Sprintf("%s\n", imgSliceLinks)
}
tmp[titleMap{20, "Market Url"}] = marketURL(pkgName)
doc.Find(".recommendation").Find(".rec-cluster").Each(func(i int, recommended *goquery.Selection) {
header := str.TrimSpace(recommended.Find(".heading").First().Text())
if header == "Similar" {
recommended.Find(".card").Each(func(j int, card *goquery.Selection) {
similarAppIds, _ := card.Attr("data-docid")
tmp[titleMap{21, "Related App"}] += fmt.Sprintf("%s\n", similarAppIds)
})
} else {
recommended.Find(".card").Each(func(j int, card *goquery.Selection) {
similarAppIds, _ := card.Attr("data-docid")
tmp[titleMap{22, "More from Developer"}] += fmt.Sprintf("%s\n", similarAppIds)
})
}
})
doc.Find(".play-action-container").Each(func(i int, node *goquery.Selection) {
url, _ := node.Attr("data-video-url")
videoID := str.Split(str.Split(url, "embed/")[1], "?")[0]
if len(url) > 0 {
tmp[titleMap{23, "YouTube Url"}] = fmt.Sprintf("%s%s", baseYouTubeURL, videoID)
}
})
// Go iteration order is randomzies
// https://blog.golang.org/go-maps-in-action#TOC_7.
var keys byIndex
for k := range tmp {
keys = append(keys, k)
}
// sort the keys
sort.Sort(keys)
for _, k := range keys {
var rows string
rows = fmt.Sprintf("%s %s | %s\n", k.Title, buffer(k.Title), str.TrimSpace(tmp[k]))
fmt.Printf(rows)
}
}
func marketURL(pkgName string) string {
return fmt.Sprintf("%s%s&hl=en", baseString, pkgName)
}
// I copied it from https://github.com/addyosmani/psi/blob/master/lib%2Futils.js#L36-L50
func buffer(msg string) string {
var ret = ""
length := 24
length = length - len(msg) - 1
if length > 0 {
ret = str.Repeat(" ", length)
}
return ret
}
type byIndex []titleMap
func (a byIndex) Len() int { return len(a) }
func (a byIndex) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byIndex) Less(i, j int) bool { return a[i].Index < a[j].Index }
func panicIf(err error) {
if err != nil {
panic(err)
}
}
type titleMap struct {
Index int
Title string
}