Tracking synced videos #115
2 changed files with 42 additions and 13 deletions
|
@ -24,6 +24,7 @@ type SyncContext struct {
|
||||||
SyncDbPath string
|
SyncDbPath string
|
||||||
LbrynetAddr string
|
LbrynetAddr string
|
||||||
ChannelID string
|
ChannelID string
|
||||||
|
VideoID string
|
||||||
PublishBid float64
|
PublishBid float64
|
||||||
YouTubeSourceConfig *YouTubeSourceConfig
|
YouTubeSourceConfig *YouTubeSourceConfig
|
||||||
}
|
}
|
||||||
|
@ -44,11 +45,22 @@ func (c *SyncContext) Validate() error {
|
||||||
if c.PublishBid <= 0.0 {
|
if c.PublishBid <= 0.0 {
|
||||||
return errors.New("Publish bid is not greater than zero")
|
return errors.New("Publish bid is not greater than zero")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if c.YouTubeSourceConfig.ChannelID != "" {
|
||||||
|
// Validate for YouTube source
|
||||||
|
// For now, an API key is required
|
||||||
|
if c.YouTubeSourceConfig.APIKey == "" {
|
||||||
|
return errors.New("YouTube source was selected, but no YouTube API key was provided.")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return errors.New("No video source provided")
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type YouTubeSourceConfig struct {
|
type YouTubeSourceConfig struct {
|
||||||
YouTubeAPIKey string
|
ChannelID string
|
||||||
|
APIKey string
|
||||||
}
|
}
|
||||||
|
|
||||||
var syncContext SyncContext
|
var syncContext SyncContext
|
||||||
|
@ -58,7 +70,7 @@ func AddCommand(rootCmd *cobra.Command) {
|
||||||
Use: "local",
|
Use: "local",
|
||||||
Short: "run a personal ytsync",
|
Short: "run a personal ytsync",
|
||||||
Run: localCmd,
|
Run: localCmd,
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(0),
|
||||||
}
|
}
|
||||||
cmd.Flags().BoolVar(&syncContext.DryRun, "dry-run", false, "Display information about the stream publishing, but do not publish the stream")
|
cmd.Flags().BoolVar(&syncContext.DryRun, "dry-run", false, "Display information about the stream publishing, but do not publish the stream")
|
||||||
cmd.Flags().BoolVar(&syncContext.KeepCache, "keep-cache", false, "Don't delete local files after publishing.")
|
cmd.Flags().BoolVar(&syncContext.KeepCache, "keep-cache", false, "Don't delete local files after publishing.")
|
||||||
|
@ -68,10 +80,11 @@ func AddCommand(rootCmd *cobra.Command) {
|
||||||
cmd.Flags().Float64Var(&syncContext.PublishBid, "publish-bid", 0.01, "Bid amount for the stream claim")
|
cmd.Flags().Float64Var(&syncContext.PublishBid, "publish-bid", 0.01, "Bid amount for the stream claim")
|
||||||
cmd.Flags().StringVar(&syncContext.LbrynetAddr, "lbrynet-address", getEnvDefault("LBRYNET_ADDRESS", ""), "JSONRPC address of the local LBRYNet daemon")
|
cmd.Flags().StringVar(&syncContext.LbrynetAddr, "lbrynet-address", getEnvDefault("LBRYNET_ADDRESS", ""), "JSONRPC address of the local LBRYNet daemon")
|
||||||
cmd.Flags().StringVar(&syncContext.ChannelID, "channel-id", "", "LBRY channel ID to publish to")
|
cmd.Flags().StringVar(&syncContext.ChannelID, "channel-id", "", "LBRY channel ID to publish to")
|
||||||
|
cmd.Flags().StringVar(&syncContext.VideoID, "video-id", "", "ID of video to sync. This will attempt to sync only this one video.")
|
||||||
|
|
||||||
// For now, assume source is always YouTube
|
|
||||||
syncContext.YouTubeSourceConfig = &YouTubeSourceConfig{}
|
syncContext.YouTubeSourceConfig = &YouTubeSourceConfig{}
|
||||||
cmd.Flags().StringVar(&syncContext.YouTubeSourceConfig.YouTubeAPIKey, "youtube-api-key", getEnvDefault("YOUTUBE_API_KEY", ""), "YouTube API Key")
|
cmd.Flags().StringVar(&syncContext.YouTubeSourceConfig.APIKey, "youtube-api-key", getEnvDefault("YOUTUBE_API_KEY", ""), "YouTube API Key")
|
||||||
|
cmd.Flags().StringVar(&syncContext.YouTubeSourceConfig.ChannelID, "youtube-channel", "", "YouTube Channel ID")
|
||||||
rootCmd.AddCommand(cmd)
|
rootCmd.AddCommand(cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,7 +101,11 @@ func localCmd(cmd *cobra.Command, args []string) {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
videoID := args[0]
|
videoID := syncContext.VideoID
|
||||||
|
if videoID == "" {
|
||||||
|
log.Errorf("Only single video mode is supported currently. Please provided a video ID.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
log.Debugf("Running sync for video ID %s", videoID)
|
log.Debugf("Running sync for video ID %s", videoID)
|
||||||
|
|
||||||
|
@ -126,22 +143,31 @@ func localCmd(cmd *cobra.Command, args []string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = syncVideo(syncContext, syncDB, videoSource, publisher, videoID)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Error syncing %s: %v", videoID, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Info("Done")
|
||||||
|
}
|
||||||
|
|
||||||
|
func syncVideo(syncContext SyncContext, syncDB *SyncDb, videoSource VideoSource, publisher VideoPublisher, videoID string) error {
|
||||||
sourceVideo, err := videoSource.GetVideo(videoID)
|
sourceVideo, err := videoSource.GetVideo(videoID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error getting source video: %v", err)
|
log.Errorf("Error getting source video: %v", err)
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = syncDB.SaveVideoData(*sourceVideo)
|
err = syncDB.SaveVideoData(*sourceVideo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error saving video data: %v", err)
|
log.Errorf("Error saving video data: %v", err)
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
processedVideo, err := processVideoForPublishing(*sourceVideo, syncContext.ChannelID)
|
processedVideo, err := processVideoForPublishing(*sourceVideo, syncContext.ChannelID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error processing source video for publishing: %v", err)
|
log.Errorf("Error processing source video for publishing: %v", err)
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if syncContext.DryRun {
|
if syncContext.DryRun {
|
||||||
|
@ -153,20 +179,21 @@ func localCmd(cmd *cobra.Command, args []string) {
|
||||||
claimID, doneReflectingCh, err := publisher.Publish(*processedVideo, syncContext.ReflectStreams)
|
claimID, doneReflectingCh, err := publisher.Publish(*processedVideo, syncContext.ReflectStreams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error publishing video: %v", err)
|
log.Errorf("Error publishing video: %v", err)
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
err = syncDB.SaveVideoPublication(*processedVideo, claimID)
|
err = syncDB.SaveVideoPublication(*processedVideo, claimID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Sync DB is corrupted after getting here
|
// Sync DB is corrupted after getting here
|
||||||
// and will allow double publication.
|
// and will allow double publication.
|
||||||
log.Errorf("Error saving video publication to sync DB: %v", err)
|
log.Errorf("Error saving video publication to sync DB: %v", err)
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if syncContext.ReflectStreams {
|
if syncContext.ReflectStreams {
|
||||||
err = <-doneReflectingCh
|
err = <-doneReflectingCh
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error while wating for stream to reflect: %v", err)
|
log.Errorf("Error while wating for stream to reflect: %v", err)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Debugln("Not waiting for stream to reflect.")
|
log.Debugln("Not waiting for stream to reflect.")
|
||||||
|
@ -178,9 +205,11 @@ func localCmd(cmd *cobra.Command, args []string) {
|
||||||
err = videoSource.DeleteLocalCache(videoID)
|
err = videoSource.DeleteLocalCache(videoID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error deleting local files for video %s: %v", videoID, err)
|
log.Errorf("Error deleting local files for video %s: %v", videoID, err)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.Info("Done")
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type SourceVideo struct {
|
type SourceVideo struct {
|
||||||
|
|
|
@ -21,8 +21,8 @@ func NewYtdlVideoSource(downloadDir string, config *YouTubeSourceConfig) (*YtdlV
|
||||||
downloader: *ytdl,
|
downloader: *ytdl,
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.YouTubeAPIKey != "" {
|
if config.APIKey != "" {
|
||||||
ytapiEnricher := NewYouTubeAPIVideoEnricher(config.YouTubeAPIKey)
|
ytapiEnricher := NewYouTubeAPIVideoEnricher(config.APIKey)
|
||||||
source.enrichers = append(source.enrichers, ytapiEnricher)
|
source.enrichers = append(source.enrichers, ytapiEnricher)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue