-
Notifications
You must be signed in to change notification settings - Fork 0
/
wikigenre.go
391 lines (345 loc) · 8.68 KB
/
wikigenre.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"net/url"
"os"
"regexp"
"runtime"
"strings"
"sync"
"time"
"github.com/Perlence/go-wikigenre/Godeps/_workspace/src/github.com/PuerkitoBio/goquery"
"github.com/Perlence/go-wikigenre/Godeps/_workspace/src/github.com/franela/goreq"
"github.com/Perlence/go-wikigenre/Godeps/_workspace/src/github.com/shiena/ansicolor"
"github.com/Perlence/go-wikigenre/Godeps/_workspace/src/github.com/ttacon/chalk"
)
// ErrNoGenres is returned if scraping yields no genres.
var ErrNoGenres = fmt.Errorf("couldn't find any genres")
var colorStderr = ansicolor.NewAnsiColorWriter(os.Stderr)
var logger = log.New(colorStderr, "", log.LstdFlags)
// Log requests to Wikipedia.
var Verbose = false
const verboseUsage = "print URIs of HTTP requests"
func init() {
flag.BoolVar(&Verbose, "v", false, verboseUsage)
goreq.SetConnectTimeout(10 * time.Second)
}
func usage() {
fmt.Fprintln(os.Stderr, `usage: go-wikigenre [-h] [-v] "[ARTIST - ]ALBUM"( "[ARTIST - ]ALBUM")*`)
fmt.Fprintln(os.Stderr, ` -v=false: `+verboseUsage)
os.Exit(2)
}
// Prevent data races in goreq the hard way.
// Passing cookiejar to goreq.Request will create a new instance of http.Client,
// so Do method won't write into CheckRedirect field of goreq.DefaultClient.
type dummyCookiejar struct{}
func (c dummyCookiejar) SetCookies(u *url.URL, cookies []*http.Cookie) {}
func (c dummyCookiejar) Cookies(u *url.URL) []*http.Cookie {
return nil
}
func main() {
flag.Usage = usage
flag.Parse()
args := flag.Args()
var artistAlbums []artistAlbum
if len(args) > 0 {
artistAlbums = artistAlbumsFromCLI(args)
} else {
var err error
artistAlbums, err = artistAlbumsFromStdin()
if err != nil {
errorln("error reading from stdin: ", err)
os.Exit(1)
}
}
code := 0
gs, errs := multipleAlbumGenres(artistAlbums)
if errs != nil {
for _, err := range errs {
errorln(err)
}
code = 1
}
for _, g := range gs {
fmt.Println(strings.Join(g, "; "))
}
os.Exit(code)
}
func errorln(arg ...interface{}) {
fmt.Fprint(colorStderr, chalk.Red)
fmt.Fprint(colorStderr, arg...)
fmt.Fprint(colorStderr, chalk.Reset, "\n")
}
type artistAlbum struct {
artist, album, both string
}
func artistAlbumsFromCLI(args []string) []artistAlbum {
var result []artistAlbum
for _, arg := range args {
parts := strings.SplitN(arg, " - ", 2)
var artist, album string
switch len(parts) {
case 1:
artist, album = "", arg
case 2:
artist, album = parts[0], parts[1]
}
result = append(result, artistAlbum{artist, album, arg})
}
return result
}
// Read foobar2k items from stdin.
func artistAlbumsFromStdin() ([]artistAlbum, error) {
s := bufio.NewScanner(os.Stdin)
var lines []string
for s.Scan() {
line := s.Text()
// Look for a zero-length read.
if len(line) == 0 {
break
}
lines = append(lines, line)
}
if err := s.Err(); err != nil {
return nil, err
}
artistAlbums := make([]artistAlbum, len(lines))
for i, line := range lines {
artistAlbums[i] = parseFoobar2kItem(line)
}
return artistAlbums, nil
}
var reFoobar2kItem = regexp.MustCompile(`(?:(.+) - )?\[(.+?)?(?: CD\d+)?(?: #\d+)?\]`)
func parseFoobar2kItem(item string) artistAlbum {
matches := reFoobar2kItem.FindStringSubmatch(item)
if len(matches) == 0 {
return artistAlbum{}
}
both, artist, album := "", matches[1], matches[2]
if artist == "" {
both = album
} else if album == "" {
both = artist
} else {
both = fmt.Sprintf("%s - %s", artist, album)
}
return artistAlbum{artist, album, both}
}
func multipleAlbumGenres(as []artistAlbum) ([][]string, []error) {
var wg sync.WaitGroup
m := new(sync.Mutex)
wg.Add(len(as))
uniqueArtistAlbumMap := make(map[artistAlbum][]string)
var errs []error
for _, aa := range as {
q := aa
go func() {
defer func() {
m.Unlock()
wg.Done()
runtime.Gosched()
}()
if q == (artistAlbum{}) {
return
}
m.Lock()
_, ok := uniqueArtistAlbumMap[q]
if ok {
// Don't query if query is already in process.
return
}
uniqueArtistAlbumMap[q] = nil
m.Unlock()
gs, err := AlbumGenres(q.artist, q.album)
m.Lock()
if err != nil {
errs = append(errs, fmt.Errorf("error finding genres for %s: %s", q.both, err))
}
uniqueArtistAlbumMap[q] = gs
}()
}
wg.Wait()
var result [][]string
for _, aa := range as {
result = append(result, uniqueArtistAlbumMap[aa])
}
return result, errs
}
// AlbumGenres searches Wikipedia for album page and scrapes genres from it. At
// least one of artist or album must be given.
func AlbumGenres(artist, album string) ([]string, error) {
for _, variant := range searchVariants(artist, album) {
gs, err := albumGenres(variant)
if err != nil {
return nil, err
}
if len(gs) > 0 {
return gs, nil
}
}
return nil, ErrNoGenres
}
func searchVariants(artist, album string) []string {
var variants []string
if artist != "" && album != "" {
variants = append(variants, fmt.Sprintf("%s (%s album)", album, artist))
}
if album != "" {
variants = append(variants, fmt.Sprintf("%s (album)", album))
variants = append(variants, album)
}
if artist != "" {
variants = append(variants, artist)
}
return variants
}
func albumGenres(query string) ([]string, error) {
searchResp, err := searchWikipedia(query)
if err != nil {
return nil, err
}
// Bail if nothing's found.
if len(searchResp.uris) == 0 {
return nil, nil
}
uri := searchResp.uris[0] // TODO: check other URIs as well
resp, err := wikipediaPage(uri)
if resp.Body != nil {
defer resp.Body.Close()
}
doc, err := goquery.NewDocumentFromResponse(resp.Response)
if err != nil {
return nil, err
}
return scrapeGenres(doc), nil
}
func searchWikipedia(query string) (searchResponse, error) {
var sr searchResponse
resp, err := goreq.Request{
Uri: "https://en.wikipedia.org/w/api.php",
QueryString: url.Values{
"action": {"opensearch"},
"search": {query},
},
UserAgent: "Wikigenre",
CookieJar: dummyCookiejar{},
}.Do()
if err != nil {
return sr, err
}
if !isResponseOK(resp) {
return sr, fmt.Errorf("search on Wikipedia failed, HTTP status %s", resp.Status)
}
if resp.Body != nil {
defer resp.Body.Close()
}
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(&sr); err != nil {
return sr, err
}
return sr, nil
}
// isResponseOK returns false if response code is between 400 and 599.
func isResponseOK(r *goreq.Response) bool {
return !(400 <= r.StatusCode && r.StatusCode < 600)
}
type searchResponse struct {
query string
suggestions []string
snippets []string
uris []string
}
func (sr *searchResponse) UnmarshalJSON(data []byte) error {
assertError := func(o interface{}) error {
return fmt.Errorf("unable to assert %#v", o)
}
var jsonResp []interface{}
err := json.Unmarshal(data, &jsonResp)
if err != nil {
return err
}
query, ok := jsonResp[0].(string)
if !ok {
return assertError(jsonResp[0])
}
suggestions, ok := interfaceToStringSlice(jsonResp[1])
if !ok {
return assertError(jsonResp[1])
}
snippets, ok := interfaceToStringSlice(jsonResp[2])
if !ok {
return assertError(jsonResp[2])
}
uris, ok := interfaceToStringSlice(jsonResp[3])
if !ok {
return assertError(jsonResp[3])
}
sr.query = query
sr.suggestions = suggestions
sr.snippets = snippets
sr.uris = uris
return nil
}
func interfaceToStringSlice(obj interface{}) ([]string, bool) {
slice, ok := obj.([]interface{})
if !ok {
return nil, ok
}
result := make([]string, len(slice))
for i, v := range slice {
result[i], ok = v.(string)
if !ok {
return nil, ok
}
}
return result, true
}
func wikipediaPage(uri string) (*goreq.Response, error) {
if Verbose {
logger.Println(uri)
}
resp, err := goreq.Request{
Uri: uri,
CookieJar: dummyCookiejar{},
}.Do()
if err != nil {
return nil, err
}
if !isResponseOK(resp) {
return nil, fmt.Errorf("failed to open Wikipedia page %s, HTTP status %s", uri, resp.Status)
}
return resp, nil
}
func scrapeGenres(doc *goquery.Document) []string {
var result []string
doc.Find("table.haudio td.category a").
Each(textFromSelection(&result))
if len(result) > 0 {
return result
}
doc.Find("table.infobox th>a").
FilterFunction(func(i int, link *goquery.Selection) bool { return link.Text() == "Genre" }).
Parent().
Parent().
Find("td a").
Each(textFromSelection(&result))
return result
}
func textFromSelection(result *[]string) func(int, *goquery.Selection) {
return func(i int, link *goquery.Selection) {
*result = append(*result, title(link.Text()))
}
}
// Title upper-cases only the first letter of each word.
func title(s string) string {
var parts []string
for _, part := range strings.Split(s, " ") {
parts = append(parts, strings.ToUpper(part[0:1])+part[1:])
}
return strings.Join(parts, " ")
}