From 75364805a5f8615c9053dae2b6d45f5fc81ba392 Mon Sep 17 00:00:00 2001 From: Niko Storni Date: Tue, 7 May 2019 21:15:43 +0200 Subject: [PATCH] more refactoring --- manager/ytsync.go | 1 + sources/youtubeVideo.go | 71 ++++++++++++++++++++--------------------- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/manager/ytsync.go b/manager/ytsync.go index cc9b028..a3b69fa 100644 --- a/manager/ytsync.go +++ b/manager/ytsync.go @@ -323,6 +323,7 @@ func (s *Sync) setChannelTerminationStatus(e *error) { noFailConditions := []string{ "this youtube channel is being managed by another server", "interrupted during daemon startup", + "playlist items not found", } if util.SubstringInSlice((*e).Error(), noFailConditions) { return diff --git a/sources/youtubeVideo.go b/sources/youtubeVideo.go index 6e197e2..fc24c7c 100644 --- a/sources/youtubeVideo.go +++ b/sources/youtubeVideo.go @@ -39,6 +39,7 @@ type YoutubeVideo struct { tags []string awsConfig aws.Config thumbnailURL string + lbryChannelID string } const reflectorURL = "http://blobs.lbry.io/" @@ -142,6 +143,11 @@ func (v *YoutubeVideo) getAbbrevDescription() string { if strings.Count(description, "\n") < maxLines { return description } + additionalDescription := "\nhttps://www.youtube.com/watch?v=" + v.id + khanAcademyClaimID := "5fc52291980268b82413ca4c0ace1b8d749f3ffb" + if v.lbryChannelID == khanAcademyClaimID { + additionalDescription = additionalDescription + "\nNote: All Khan Academy content is available for free at (www.khanacademy.org)" + } return strings.Join(strings.Split(description, "\n")[:maxLines], "\n") + "\n..." } @@ -249,36 +255,23 @@ func (v *YoutubeVideo) triggerThumbnailSave() (err error) { return err } -func (v *YoutubeVideo) publish(daemon *jsonrpc.Client, claimAddress string, amount float64, channelID string, namer *namer.Namer) (*SyncSummary, error) { - additionalDescription := "\nhttps://www.youtube.com/watch?v=" + v.id - khanAcademyClaimID := "5fc52291980268b82413ca4c0ace1b8d749f3ffb" - if channelID == khanAcademyClaimID { - additionalDescription = additionalDescription + "\nNote: All Khan Academy content is available for free at (www.khanacademy.org)" - } - var languages []string = nil - if v.youtubeInfo.Snippet.DefaultLanguage != "" { - languages = []string{v.youtubeInfo.Snippet.DefaultLanguage} - } +func (v *YoutubeVideo) publish(daemon *jsonrpc.Client, claimAddress string, amount float64, namer *namer.Namer) (*SyncSummary, error) { + languages, locations, tags := v.getMetadata() - videoDuration, err := duration.FromString(v.youtubeInfo.ContentDetails.Duration) - - if err != nil { - return nil, errors.Err(err) - } options := jsonrpc.StreamCreateOptions{ ClaimCreateOptions: jsonrpc.ClaimCreateOptions{ Title: v.title, - Description: v.getAbbrevDescription() + additionalDescription, + Description: v.getAbbrevDescription(), ClaimAddress: &claimAddress, Languages: languages, ThumbnailURL: &v.thumbnailURL, - Tags: v.youtubeInfo.Snippet.Tags, + Tags: tags, + Locations: locations, }, Author: util.PtrToString(v.channelTitle), License: util.PtrToString("Copyrighted (contact author)"), ReleaseTime: util.PtrToInt64(v.publishedAt.Unix()), - Duration: util.PtrToUint64(uint64(math.Ceil(videoDuration.ToDuration().Seconds()))), - ChannelID: &channelID, + ChannelID: &v.lbryChannelID, } return publishAndRetryExistingNames(daemon, v.title, v.getFullPath(), amount, options, namer) @@ -300,7 +293,7 @@ type SyncParams struct { func (v *YoutubeVideo) Sync(daemon *jsonrpc.Client, params SyncParams, existingVideoData *sdk.SyncedVideo, reprocess bool) (*SyncSummary, error) { v.maxVideoSize = int64(params.MaxVideoSize) * 1024 * 1024 v.maxVideoLength = params.MaxVideoLength - + v.lbryChannelID = params.ChannelID if reprocess { summary, err := v.reprocess(daemon, params, existingVideoData) return summary, err @@ -322,13 +315,30 @@ func (v *YoutubeVideo) downloadAndPublish(daemon *jsonrpc.Client, params SyncPar } log.Debugln("Created thumbnail for " + v.id) - summary, err := v.publish(daemon, params.ClaimAddress, params.Amount, params.ChannelID, params.Namer) + summary, err := v.publish(daemon, params.ClaimAddress, params.Amount, params.Namer) //delete the video in all cases (and ignore the error) _ = v.delete() return summary, errors.Prefix("publish error", err) } +func (v *YoutubeVideo) getMetadata() (languages []string, locations []jsonrpc.Location, tags []string) { + languages = nil + if v.youtubeInfo.Snippet.DefaultLanguage != "" { + languages = []string{v.youtubeInfo.Snippet.DefaultLanguage} + } + + locations = nil + if v.youtubeInfo.RecordingDetails.Location != nil { + locations = []jsonrpc.Location{{ + Latitude: util.PtrToString(fmt.Sprintf("%.7f", v.youtubeInfo.RecordingDetails.Location.Latitude)), + Longitude: util.PtrToString(fmt.Sprintf("%.7f", v.youtubeInfo.RecordingDetails.Location.Longitude)), + }} + } + tags = append([]string{youtubeCategories[v.youtubeInfo.Snippet.CategoryId]}, v.youtubeInfo.Snippet.Tags...) + return languages, locations, tags +} + func (v *YoutubeVideo) reprocess(daemon *jsonrpc.Client, params SyncParams, existingVideoData *sdk.SyncedVideo) (*SyncSummary, error) { c, err := daemon.ClaimSearch(nil, &existingVideoData.ClaimID, nil, nil) if err != nil { @@ -341,18 +351,7 @@ func (v *YoutubeVideo) reprocess(daemon *jsonrpc.Client, params SyncParams, exis } currentClaim := c.Claims[0] - var languages []string = nil - if v.youtubeInfo.Snippet.DefaultLanguage != "" { - languages = []string{v.youtubeInfo.Snippet.DefaultLanguage} - } - - var locations []jsonrpc.Location = nil - if v.youtubeInfo.RecordingDetails.Location != nil { - locations = []jsonrpc.Location{{ - Latitude: util.PtrToString(fmt.Sprintf("%.7f", v.youtubeInfo.RecordingDetails.Location.Latitude)), - Longitude: util.PtrToString(fmt.Sprintf("%.7f", v.youtubeInfo.RecordingDetails.Location.Longitude)), - }} - } + languages, locations, tags := v.getMetadata() thumbnailURL := "" if currentClaim.Value.GetThumbnail() == nil { @@ -362,8 +361,6 @@ func (v *YoutubeVideo) reprocess(daemon *jsonrpc.Client, params SyncParams, exis thumbnailURL = thumbs.ThumbnailEndpoint + v.ID() } - tags := append(v.youtubeInfo.Snippet.Tags, youtubeCategories[v.youtubeInfo.Snippet.CategoryId]) - videoSize, err := currentClaim.GetStreamSizeByMagic() if err != nil { if existingVideoData.Size > 0 { @@ -374,10 +371,12 @@ func (v *YoutubeVideo) reprocess(daemon *jsonrpc.Client, params SyncParams, exis return nil, errors.Err("the video must be republished as we can't get the right size") } } + videoDuration, err := duration.FromString(v.youtubeInfo.ContentDetails.Duration) if err != nil { return nil, errors.Err(err) } + pr, err := daemon.StreamUpdate(existingVideoData.ClaimID, jsonrpc.StreamUpdateOptions{ StreamCreateOptions: &jsonrpc.StreamCreateOptions{ ClaimCreateOptions: jsonrpc.ClaimCreateOptions{ @@ -392,7 +391,7 @@ func (v *YoutubeVideo) reprocess(daemon *jsonrpc.Client, params SyncParams, exis License: util.PtrToString("Copyrighted (contact author)"), ReleaseTime: util.PtrToInt64(v.publishedAt.Unix()), Duration: util.PtrToUint64(uint64(math.Ceil(videoDuration.ToDuration().Seconds()))), - ChannelID: ¶ms.ChannelID, + ChannelID: &v.lbryChannelID, }, FileSize: util.PtrToString(fmt.Sprintf("%d", videoSize)), })