2019-01-11 03:02:26 +01:00
|
|
|
package manager
|
2017-12-28 18:14:33 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2019-01-11 02:34:34 +01:00
|
|
|
"github.com/lbryio/lbry.go/extras/errors"
|
2018-03-09 17:47:38 +01:00
|
|
|
|
2017-12-28 18:14:33 +01:00
|
|
|
"google.golang.org/api/googleapi/transport"
|
|
|
|
"google.golang.org/api/youtube/v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Sync) CountVideos() (uint64, error) {
|
|
|
|
client := &http.Client{
|
2018-09-26 06:08:18 +02:00
|
|
|
Transport: &transport.APIKey{Key: s.APIConfig.YoutubeAPIKey},
|
2017-12-28 18:14:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
service, err := youtube.New(client)
|
|
|
|
if err != nil {
|
2018-03-09 17:47:38 +01:00
|
|
|
return 0, errors.Prefix("error creating YouTube service", err)
|
2017-12-28 18:14:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
response, err := service.Channels.List("statistics").Id(s.YoutubeChannelID).Do()
|
|
|
|
if err != nil {
|
2018-03-09 17:47:38 +01:00
|
|
|
return 0, errors.Prefix("error getting channels", err)
|
2017-12-28 18:14:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(response.Items) < 1 {
|
2018-03-09 17:47:38 +01:00
|
|
|
return 0, errors.Err("youtube channel not found")
|
2017-12-28 18:14:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return response.Items[0].Statistics.VideoCount, nil
|
|
|
|
}
|