telegraph/get_views.go

54 lines
1.4 KiB
Go
Raw Normal View History

2017-09-04 21:09:59 +00:00
package telegraph
import (
"strconv"
json "github.com/pquerna/ffjson/ffjson"
http "github.com/valyala/fasthttp"
)
// PageViews represents the number of page views for a Telegraph article.
type PageViews struct {
// Number of page views for the target page.
Views int `json:"views"`
}
// GetViews get the number of views for a Telegraph article. By default, the total number of page
// views will be returned. Returns a PageViews object on success.
func GetViews(path string, year, month, day, hour int) (*PageViews, error) {
args := http.AcquireArgs()
2017-09-04 21:09:59 +00:00
if hour > -1 {
// If passed, the number of page views for the requested hour will be returned.
2017-09-04 21:09:59 +00:00
args.Add("hour", strconv.Itoa(hour))
}
if day > 0 {
// Required if hour is passed. If passed, the number of page views for the requested day will
// be returned.
2017-09-04 21:09:59 +00:00
args.Add("day", strconv.Itoa(day))
}
if month > 0 {
// Required if day is passed. If passed, the number of page views for the requested month will
// be returned.
2017-09-04 21:09:59 +00:00
args.Add("month", strconv.Itoa(month))
}
if year > 0 {
// Required if month is passed. If passed, the number of page views for the requested year
// will be returned.
2017-09-04 21:09:59 +00:00
args.Add("year", strconv.Itoa(year))
}
body, err := request("getViews", path, args)
2017-09-04 21:09:59 +00:00
if err != nil {
return nil, err
}
var resp PageViews
2017-09-04 21:12:34 +00:00
err = json.Unmarshal(*body.Result, &resp)
2017-09-04 21:09:59 +00:00
2017-09-04 21:12:34 +00:00
return &resp, err
2017-09-04 21:09:59 +00:00
}