/
yahooboss.go
117 lines (96 loc) · 2.64 KB
/
yahooboss.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
package yahooboss
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"github.com/garyburd/go-oauth/oauth"
)
// BossSearch stores a Yahoo Boss Search, and implements the search functions
type BossSearch struct {
Token string
Secret string
SearchType string
}
// BossResultRow contains a single row result
type BossResultRow struct {
url string
title string
}
// BossResult contains basic data about the search
// and a list of all results rows
type BossResult struct {
start int
end int
count int
totalresults int
results []BossResultRow
}
// BossResponse contains the most raw data about the search
type BossResponse struct {
responsecode int
bossresult BossResult
}
func (bs *BossSearch) signQuery(url string, values *url.Values) {
cred := oauth.Credentials{}
cred.Token = bs.Token
cred.Secret = bs.Secret
client := oauth.Client{}
client.SignatureMethod = oauth.HMACSHA1
client.Credentials = cred
client.SignForm(nil, "GET", url, *values)
}
func (bs *BossSearch) buildQuery(text string) string {
searchType := bs.SearchType
bossURL := fmt.Sprintf("%s%s", "https://yboss.yahooapis.com/ysearch/", searchType)
getParams := url.Values{}
getParams.Set("q", fmt.Sprintf("\"%s\"", text))
getParams.Add("format", "json")
getParams.Add("title", "lyrics")
bs.signQuery(bossURL, &getParams)
return fmt.Sprintf("%s?%s", bossURL, getParams.Encode())
}
// Search does a search using Yahoo Boss for "text"
func (bs *BossSearch) Search(text string) (BossResponse, error) {
url := bs.buildQuery(text)
res, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err)
}
return parseSearchResult(body, bs.SearchType)
}
/* Todo: Add err */
func parseSearchResult(body []byte, searchType string) (BossResponse, error) {
var dat map[string]interface{}
if err := json.Unmarshal(body, &dat); err != nil {
return BossResponse{}, errors.New("No JSON provided")
}
bossResponse := dat["bossresponse"].(map[string]interface{})
searchTypeResults := bossResponse[searchType].(map[string]interface{})
results := searchTypeResults["results"].([]interface{})
var br BossResponse
var bresult BossResult
var brows []BossResultRow
for _, value := range results {
mapValue := value.(map[string]interface{})
url := mapValue["url"].(string)
title := mapValue["title"].(string)
result := BossResultRow{url, title}
brows = append(brows, result)
}
bresult.start = 0
bresult.end = 50
bresult.totalresults = 147
bresult.results = brows
br.bossresult = bresult
br.responsecode = 200
return br, nil
}