-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.go
66 lines (54 loc) · 1.46 KB
/
search.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"regexp"
"time"
"github.com/gorilla/mux"
)
type SearchResult struct {
Results []string
}
func (s *SearchResult) UnmarshalJSON(in []byte) error {
cells := [2]json.RawMessage{}
if err := json.Unmarshal(in, &cells); err != nil {
return err
}
return json.Unmarshal(cells[1], &s.Results)
}
// Performs a simple wikipedia search and returns a list of matching article titles
func SearchForArticle(w http.ResponseWriter, r *http.Request) {
query := mux.Vars(r)["search"]
queryUrl := fmt.Sprintf("https://en.wikipedia.org/w/api.php?action=opensearch&format=json&formatversion=2&search=%s&namespace=0&limit=30", query)
APIClient := http.Client{Timeout: time.Second * 20}
req, err := http.NewRequest(http.MethodGet, queryUrl, nil)
if err != nil {
log.Fatal(err)
}
res, getErr := APIClient.Do(req)
if getErr != nil {
log.Fatal(getErr)
}
if res.Body != nil {
defer res.Body.Close()
}
body, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
log.Fatal(readErr)
}
APIResult := new(SearchResult)
jsonErr := json.Unmarshal(body, APIResult)
if jsonErr != nil {
log.Fatal(jsonErr)
}
re := regexp.MustCompile(" ")
// Replace all whitespace with underscores for consistency with the actual URLs of the articles
for i, entry := range APIResult.Results {
replaced := re.ReplaceAllString(entry, "_")
APIResult.Results[i] = replaced
}
json.NewEncoder(w).Encode(APIResult.Results)
}