-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoodreads.go
39 lines (32 loc) · 926 Bytes
/
goodreads.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
package ratings
import (
"encoding/json"
"strconv"
"github.com/meghashyamc/book-ratings/models"
log "github.com/sirupsen/logrus"
)
func GetGoodreadsRating(book string) (float32, error) {
res, err := getBookDetailsFromSearch(book, goodreads)
if err != nil {
return 0.0, err
}
goodreadsPageMap := models.GoodreadsPageMap{}
if err := json.Unmarshal(res.Pagemap, &goodreadsPageMap); err != nil {
log.WithFields(log.Fields{
"err": err.Error(),
"book": book,
"site": goodreads,
}).Error("Got an error when unmarshaling search result to read Goodreads rating")
return 0.0, err
}
goodreadsRating, err := strconv.ParseFloat(goodreadsPageMap.Review[0].RatingStars, 32)
if err != nil {
log.WithFields(log.Fields{
"err": err.Error(),
"book": book,
"site": goodreads,
}).Error("Could not convert Goodreads rating to a number")
return 0.0, err
}
return float32(goodreadsRating), nil
}