2017-09-04 21:09:59 +00:00
|
|
|
package telegraph
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
json "github.com/pquerna/ffjson/ffjson"
|
|
|
|
http "github.com/valyala/fasthttp"
|
|
|
|
)
|
|
|
|
|
2017-12-13 09:55:41 +00:00
|
|
|
// 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.
|
2017-12-13 10:41:53 +00:00
|
|
|
func GetViews(path string, year, month, day, hour int) (*PageViews, error) {
|
2017-12-13 10:08:43 +00:00
|
|
|
args := http.AcquireArgs()
|
2017-09-04 21:09:59 +00:00
|
|
|
|
|
|
|
if hour > -1 {
|
2017-12-13 10:15:18 +00:00
|
|
|
// 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 {
|
2017-12-13 10:15:18 +00:00
|
|
|
// 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 {
|
2017-12-13 10:15:18 +00:00
|
|
|
// 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 {
|
2017-12-13 10:15:18 +00:00
|
|
|
// 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))
|
|
|
|
}
|
|
|
|
|
2017-12-13 10:04:05 +00:00
|
|
|
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
|
|
|
}
|