replace yt channel video count with a scrape of socialblade.com
This commit is contained in:
parent
f39fc11697
commit
1369ed0b48
2 changed files with 33 additions and 17 deletions
|
@ -72,11 +72,10 @@ func (s *Sync) walletSetup() error {
|
||||||
}
|
}
|
||||||
log.Debugf("Starting balance is %.4f", balance)
|
log.Debugf("Starting balance is %.4f", balance)
|
||||||
|
|
||||||
n, err := ytapi.CountVideosInChannel(s.APIConfig.YoutubeAPIKey, s.YoutubeChannelID)
|
videosOnYoutube, err := ytapi.CountVideosInChannel(s.YoutubeChannelID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
videosOnYoutube := int(n)
|
|
||||||
|
|
||||||
log.Debugf("Source channel has %d videos", videosOnYoutube)
|
log.Debugf("Source channel has %d videos", videosOnYoutube)
|
||||||
if videosOnYoutube == 0 {
|
if videosOnYoutube == 0 {
|
||||||
|
|
|
@ -1,14 +1,16 @@
|
||||||
package ytapi
|
package ytapi
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/lbryio/ytsync/v5/downloader"
|
"github.com/lbryio/ytsync/v5/downloader"
|
||||||
|
|
||||||
"github.com/lbryio/ytsync/v5/ip_manager"
|
"github.com/lbryio/ytsync/v5/ip_manager"
|
||||||
"github.com/lbryio/ytsync/v5/sdk"
|
"github.com/lbryio/ytsync/v5/sdk"
|
||||||
"github.com/lbryio/ytsync/v5/sources"
|
"github.com/lbryio/ytsync/v5/sources"
|
||||||
|
@ -93,26 +95,41 @@ func GetVideosToSync(apiKey, channelID string, syncedVideos map[string]sdk.Synce
|
||||||
return videos, nil
|
return videos, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func CountVideosInChannel(apiKey, channelID string) (uint64, error) {
|
func CountVideosInChannel(channelID string) (int, error) {
|
||||||
client := &http.Client{
|
res, err := http.Get("https://socialblade.com/youtube/channel/" + channelID)
|
||||||
Transport: &transport.APIKey{Key: apiKey},
|
|
||||||
}
|
|
||||||
|
|
||||||
service, err := ytlib.New(client)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, errors.Prefix("error creating YouTube service", err)
|
return 0, err
|
||||||
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
|
||||||
|
var line string
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(res.Body)
|
||||||
|
for scanner.Scan() {
|
||||||
|
if strings.Contains(scanner.Text(), "youtube-stats-header-uploads") {
|
||||||
|
line = scanner.Text()
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
response, err := service.Channels.List("statistics").Id(channelID).Do()
|
if err := scanner.Err(); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if line == "" {
|
||||||
|
return 0, errors.Err("upload count line not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
matches := regexp.MustCompile(">([0-9]+)<").FindStringSubmatch(line)
|
||||||
|
if len(matches) != 2 {
|
||||||
|
return 0, errors.Err("upload count not found with regex")
|
||||||
|
}
|
||||||
|
|
||||||
|
num, err := strconv.Atoi(matches[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, errors.Prefix("error getting channels", err)
|
return 0, errors.Err(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(response.Items) < 1 {
|
return num, nil
|
||||||
return 0, errors.Err("youtube channel not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
return response.Items[0].Statistics.VideoCount, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ChannelInfo(apiKey, channelID string) (*ytlib.ChannelSnippet, *ytlib.ChannelBrandingSettings, error) {
|
func ChannelInfo(apiKey, channelID string) (*ytlib.ChannelSnippet, *ytlib.ChannelBrandingSettings, error) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue