diff options
author | Andinus <andinus@nand.sh> | 2020-03-22 13:43:54 +0530 |
---|---|---|
committer | Andinus <andinus@nand.sh> | 2020-03-22 13:43:54 +0530 |
commit | 9fde6e5a892b228c2d64af05b02380847573c832 (patch) | |
tree | 0d8fb21a7825b0e9f678180f5b66d866c8b38afa /fetch/wikipedia.go | |
parent | c3ff3d068fdeacacec1f693bf9513742983814ad (diff) | |
download | indus-9fde6e5a892b228c2d64af05b02380847573c832.tar.gz |
Add initial version
Diffstat (limited to 'fetch/wikipedia.go')
-rw-r--r-- | fetch/wikipedia.go | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/fetch/wikipedia.go b/fetch/wikipedia.go new file mode 100644 index 0000000..ee057d4 --- /dev/null +++ b/fetch/wikipedia.go @@ -0,0 +1,39 @@ +package fetch + +import ( + "encoding/json" + "fmt" +) + +// Wiki struct holds information retrieved from wikipedia api. +type Wiki struct { + Type string `json:"type"` + Title string `json:"title"` + DisplayTitle string `json:"displaytitle"` + PageID int `json:"pageid"` + Description string `json:"description"` + Language string `json:"lang"` + Extract string `json:"extract"` +} + +// Wikipedia gets the summary from wikipedia api & returns it with an error (if +// exists). It takes a string as input which will be the search query +// sent to wikipedia. +func Wikipedia(q string) (Wiki, error) { + w := Wiki{} + api := fmt.Sprintf("https://en.wikipedia.org/api/rest_v1/page/summary/%s", q) + + body, err := getRes(api) + if err != nil { + return w, err + } + + // Unmarshal json to w + err = json.Unmarshal([]byte(body), &w) + if err != nil { + err = fmt.Errorf("Unmarshalling Json failed\n%s", err.Error()) + return w, err + } + + return w, err +} |