From d99e2001782c19009dd1de6fe143cb9b276ed325 Mon Sep 17 00:00:00 2001 From: Mark Beamer Jr Date: Wed, 29 Jul 2020 00:12:23 -0400 Subject: [PATCH] Add check for already published videos using new video-state api. --- manager/ytsync.go | 2 +- sdk/api.go | 40 ++++++++++++++++++++++++++++++++++++++++ ytapi/ytapi.go | 13 ++++++++++--- 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/manager/ytsync.go b/manager/ytsync.go index 2d69b93..ad387e2 100644 --- a/manager/ytsync.go +++ b/manager/ytsync.go @@ -995,7 +995,7 @@ func (s *Sync) enqueueYoutubeVideos() error { return err } - videos, err := ytapi.GetVideosToSync(s.YoutubeChannelID, s.syncedVideos, s.Manager.SyncFlags.QuickSync, s.Manager.videosLimit, ytapi.VideoParams{ + videos, err := ytapi.GetVideosToSync(s.APIConfig, s.YoutubeChannelID, s.syncedVideos, s.Manager.SyncFlags.QuickSync, s.Manager.videosLimit, ytapi.VideoParams{ VideoDir: s.videoDirectory, S3Config: s.Manager.GetS3AWSConfig(), Stopper: s.grp, diff --git a/sdk/api.go b/sdk/api.go index 903229b..bf66b14 100644 --- a/sdk/api.go +++ b/sdk/api.go @@ -368,3 +368,43 @@ func (a *APIConfig) MarkVideoStatus(status VideoStatus) error { } return errors.Err("invalid API response. Status code: %d", res.StatusCode) } + +func (a *APIConfig) VideoState(videoID string) (string, error) { + endpoint := a.ApiURL + "/yt/video_state" + vals := url.Values{ + "video_id": {videoID}, + "auth_token": {a.ApiToken}, + } + + res, err := http.PostForm(endpoint, vals) + if err != nil { + return "", errors.Err(err) + } + defer res.Body.Close() + body, _ := ioutil.ReadAll(res.Body) + if res.StatusCode == http.StatusNotFound { + return "not_found", nil + } + if res.StatusCode != http.StatusOK { + util.SendErrorToSlack("Error %d while trying to call %s. Waiting to retry", res.StatusCode, endpoint) + log.Debugln(string(body)) + time.Sleep(30 * time.Second) + return a.VideoState(videoID) + } + var response struct { + Success bool `json:"success"` + Error null.String `json:"error"` + Data null.String `json:"data"` + } + err = json.Unmarshal(body, &response) + if err != nil { + return "", errors.Err(err) + } + if !response.Error.IsNull() { + return "", errors.Err(response.Error.String) + } + if !response.Data.IsNull() { + return response.Data.String, nil + } + return "", errors.Err("invalid API response. Status code: %d", res.StatusCode) +} diff --git a/ytapi/ytapi.go b/ytapi/ytapi.go index c0e1b99..1d51532 100644 --- a/ytapi/ytapi.go +++ b/ytapi/ytapi.go @@ -51,7 +51,7 @@ type VideoParams struct { var mostRecentlyFailedChannel string // TODO: fix this hack! -func GetVideosToSync(channelID string, syncedVideos map[string]sdk.SyncedVideo, quickSync bool, maxVideos int, videoParams VideoParams) ([]Video, error) { +func GetVideosToSync(config *sdk.APIConfig, channelID string, syncedVideos map[string]sdk.SyncedVideo, quickSync bool, maxVideos int, videoParams VideoParams) ([]Video, error) { var videos []Video if quickSync { @@ -76,7 +76,7 @@ func GetVideosToSync(channelID string, syncedVideos map[string]sdk.SyncedVideo, mostRecentlyFailedChannel = channelID } - vids, err := getVideos(videoIDs, videoParams.Stopper.Ch(), videoParams.IPPool) + vids, err := getVideos(config, videoIDs, videoParams.Stopper.Ch(), videoParams.IPPool) if err != nil { return nil, err } @@ -161,7 +161,7 @@ func ChannelInfo(apiKey, channelID string) (*ytlib.ChannelSnippet, *ytlib.Channe return response.Items[0].Snippet, response.Items[0].BrandingSettings, nil } -func getVideos(videoIDs []string, stopChan stop.Chan, ipPool *ip_manager.IPPool) ([]*ytdl.YtdlVideo, error) { +func getVideos(config *sdk.APIConfig, videoIDs []string, stopChan stop.Chan, ipPool *ip_manager.IPPool) ([]*ytdl.YtdlVideo, error) { var videos []*ytdl.YtdlVideo for _, videoID := range videoIDs { select { @@ -175,6 +175,13 @@ func getVideos(videoIDs []string, stopChan stop.Chan, ipPool *ip_manager.IPPool) // return nil, err //} //video, err := downloader.GetVideoInformation(videoID, &net.TCPAddr{IP: net.ParseIP(ip)}) + state, err := config.VideoState(videoID) + if err != nil { + return nil, errors.Err(err) + } + if state == "published" { + continue + } video, err := downloader.GetVideoInformation(videoID, stopChan, nil) if err != nil { //ipPool.ReleaseIP(ip)