telegraph/get_views.go

38 lines
861 B
Go
Raw Normal View History

2017-09-04 21:09:59 +00:00
package telegraph
import (
2018-07-27 11:51:14 +00:00
gopath "path"
2017-09-04 21:09:59 +00:00
"strconv"
http "github.com/valyala/fasthttp"
)
// 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.
2019-07-24 08:03:29 +00:00
func GetViews(path string, date ...int) (*PageViews, error) {
args := http.AcquireArgs()
2018-07-27 11:51:14 +00:00
defer http.ReleaseArgs(args)
args.Add("path", path) // required
2019-07-24 08:03:29 +00:00
if len(date) > 0 {
args.Add("year", strconv.Itoa(date[0]))
}
if len(date) > 1 {
args.Add("month", strconv.Itoa(date[1]))
}
if len(date) > 2 {
args.Add("day", strconv.Itoa(date[2]))
}
if len(date) > 3 {
args.Add("hour", strconv.Itoa(date[3]))
}
2018-07-27 11:51:14 +00:00
2019-07-24 08:03:29 +00:00
data, err := makeRequest(gopath.Join("getViews", path), args)
2017-09-04 21:09:59 +00:00
if err != nil {
return nil, err
}
2019-07-24 08:03:29 +00:00
var result PageViews
err = parser.Unmarshal(data, &result)
return &result, err
2017-09-04 21:09:59 +00:00
}