2018-04-27 15:14:23 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2018-05-17 02:08:52 +02:00
|
|
|
|
2018-05-26 02:43:16 +02:00
|
|
|
"time"
|
|
|
|
|
2018-07-24 02:01:35 +02:00
|
|
|
"os/user"
|
|
|
|
|
2018-04-27 15:14:23 +02:00
|
|
|
"github.com/lbryio/lbry.go/util"
|
2018-05-17 01:42:06 +02:00
|
|
|
sync "github.com/lbryio/lbry.go/ytsync"
|
2018-04-27 15:14:23 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2018-07-17 18:54:22 +02:00
|
|
|
const defaultMaxTries = 3
|
|
|
|
|
|
|
|
var (
|
|
|
|
stopOnError bool
|
|
|
|
maxTries int
|
|
|
|
takeOverExistingChannel bool
|
|
|
|
refill int
|
|
|
|
limit int
|
|
|
|
skipSpaceCheck bool
|
|
|
|
syncUpdate bool
|
|
|
|
syncStatus string
|
|
|
|
channelID string
|
|
|
|
syncFrom int64
|
|
|
|
syncUntil int64
|
|
|
|
concurrentJobs int
|
2018-08-01 14:56:04 +02:00
|
|
|
videosLimit int
|
|
|
|
maxVideoSize int
|
2018-07-17 18:54:22 +02:00
|
|
|
)
|
2018-05-26 02:43:16 +02:00
|
|
|
|
2018-04-27 15:14:23 +02:00
|
|
|
func init() {
|
2018-07-21 01:56:36 +02:00
|
|
|
var ytSyncCmd = &cobra.Command{
|
|
|
|
Use: "ytsync",
|
2018-06-18 01:50:59 +02:00
|
|
|
Args: cobra.RangeArgs(0, 0),
|
2018-04-27 15:14:23 +02:00
|
|
|
Short: "Publish youtube channels into LBRY network automatically.",
|
2018-07-21 01:56:36 +02:00
|
|
|
Run: ytSync,
|
2018-04-27 15:14:23 +02:00
|
|
|
}
|
2018-07-21 01:56:36 +02:00
|
|
|
ytSyncCmd.Flags().BoolVar(&stopOnError, "stop-on-error", false, "If a publish fails, stop all publishing and exit")
|
|
|
|
ytSyncCmd.Flags().IntVar(&maxTries, "max-tries", defaultMaxTries, "Number of times to try a publish that fails")
|
|
|
|
ytSyncCmd.Flags().BoolVar(&takeOverExistingChannel, "takeover-existing-channel", false, "If channel exists and we don't own it, take over the channel")
|
|
|
|
ytSyncCmd.Flags().IntVar(&limit, "limit", 0, "limit the amount of channels to sync")
|
|
|
|
ytSyncCmd.Flags().BoolVar(&skipSpaceCheck, "skip-space-check", false, "Do not perform free space check on startup")
|
2018-08-01 14:56:04 +02:00
|
|
|
ytSyncCmd.Flags().BoolVar(&syncUpdate, "update", false, "Update previously synced channels instead of syncing new ones")
|
2018-07-26 01:45:09 +02:00
|
|
|
ytSyncCmd.Flags().StringVar(&syncStatus, "status", "", "Specify which queue to pull from. Overrides --update")
|
2018-07-21 01:56:36 +02:00
|
|
|
ytSyncCmd.Flags().StringVar(&channelID, "channelID", "", "If specified, only this channel will be synced.")
|
|
|
|
ytSyncCmd.Flags().Int64Var(&syncFrom, "after", time.Unix(0, 0).Unix(), "Specify from when to pull jobs [Unix time](Default: 0)")
|
|
|
|
ytSyncCmd.Flags().Int64Var(&syncUntil, "before", time.Now().Unix(), "Specify until when to pull jobs [Unix time](Default: current Unix time)")
|
2018-08-01 14:56:04 +02:00
|
|
|
ytSyncCmd.Flags().IntVar(&concurrentJobs, "concurrent-jobs", 1, "how many jobs to process concurrently")
|
|
|
|
ytSyncCmd.Flags().IntVar(&videosLimit, "videos-limit", 1000, "how many videos to process per channel")
|
|
|
|
ytSyncCmd.Flags().IntVar(&maxVideoSize, "max-size", 2048, "Maximum video size to process (in MB)")
|
2018-05-17 01:42:06 +02:00
|
|
|
|
2018-07-21 01:56:36 +02:00
|
|
|
RootCmd.AddCommand(ytSyncCmd)
|
2018-05-25 04:03:54 +02:00
|
|
|
}
|
|
|
|
|
2018-07-21 01:56:36 +02:00
|
|
|
func ytSync(cmd *cobra.Command, args []string) {
|
2018-07-17 18:54:22 +02:00
|
|
|
var hostname string
|
2018-05-25 04:03:54 +02:00
|
|
|
slackToken := os.Getenv("SLACK_TOKEN")
|
|
|
|
if slackToken == "" {
|
|
|
|
log.Error("A slack token was not present in env vars! Slack messages disabled!")
|
|
|
|
} else {
|
2018-07-17 18:54:22 +02:00
|
|
|
var err error
|
|
|
|
hostname, err = os.Hostname()
|
2018-06-15 23:03:28 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error("could not detect system hostname")
|
2018-07-17 18:54:22 +02:00
|
|
|
hostname = "ytsync-unknown"
|
2018-06-15 23:03:28 +02:00
|
|
|
}
|
2018-07-17 18:54:22 +02:00
|
|
|
util.InitSlack(os.Getenv("SLACK_TOKEN"), os.Getenv("SLACK_CHANNEL"), hostname)
|
2018-05-26 02:43:16 +02:00
|
|
|
}
|
2018-04-27 15:14:23 +02:00
|
|
|
|
2018-07-26 01:45:09 +02:00
|
|
|
if syncStatus != "" && !util.InSlice(syncStatus, sync.SyncStatuses) {
|
2018-07-17 18:54:22 +02:00
|
|
|
log.Errorf("status must be one of the following: %v\n", sync.SyncStatuses)
|
2018-05-24 02:32:11 +02:00
|
|
|
return
|
|
|
|
}
|
2018-05-26 02:43:16 +02:00
|
|
|
|
2018-04-27 15:14:23 +02:00
|
|
|
if stopOnError && maxTries != defaultMaxTries {
|
|
|
|
log.Errorln("--stop-on-error and --max-tries are mutually exclusive")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if maxTries < 1 {
|
|
|
|
log.Errorln("setting --max-tries less than 1 doesn't make sense")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if limit < 0 {
|
|
|
|
log.Errorln("setting --limit less than 0 (unlimited) doesn't make sense")
|
|
|
|
return
|
|
|
|
}
|
2018-07-24 02:01:35 +02:00
|
|
|
|
|
|
|
apiURL := os.Getenv("LBRY_API")
|
|
|
|
apiToken := os.Getenv("LBRY_API_TOKEN")
|
|
|
|
youtubeAPIKey := os.Getenv("YOUTUBE_API_KEY")
|
|
|
|
blobsDir := os.Getenv("BLOBS_DIRECTORY")
|
|
|
|
if apiURL == "" {
|
|
|
|
log.Errorln("An API URL was not defined. Please set the environment variable LBRY_API")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if apiToken == "" {
|
|
|
|
log.Errorln("An API Token was not defined. Please set the environment variable LBRY_API_TOKEN")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if youtubeAPIKey == "" {
|
|
|
|
log.Errorln("A Youtube API key was not defined. Please set the environment variable YOUTUBE_API_KEY")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if blobsDir == "" {
|
|
|
|
usr, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
blobsDir = usr.HomeDir + "/.lbrynet/blobfiles/"
|
|
|
|
}
|
|
|
|
|
2018-07-17 18:54:22 +02:00
|
|
|
sm := sync.SyncManager{
|
|
|
|
StopOnError: stopOnError,
|
|
|
|
MaxTries: maxTries,
|
|
|
|
TakeOverExistingChannel: takeOverExistingChannel,
|
|
|
|
Refill: refill,
|
|
|
|
Limit: limit,
|
|
|
|
SkipSpaceCheck: skipSpaceCheck,
|
|
|
|
SyncUpdate: syncUpdate,
|
|
|
|
SyncStatus: syncStatus,
|
|
|
|
SyncFrom: syncFrom,
|
|
|
|
SyncUntil: syncUntil,
|
|
|
|
ConcurrentJobs: concurrentJobs,
|
|
|
|
ConcurrentVideos: concurrentJobs,
|
|
|
|
HostName: hostname,
|
|
|
|
YoutubeChannelID: channelID,
|
2018-07-24 02:01:35 +02:00
|
|
|
YoutubeAPIKey: youtubeAPIKey,
|
|
|
|
ApiURL: apiURL,
|
|
|
|
ApiToken: apiToken,
|
|
|
|
BlobsDir: blobsDir,
|
2018-08-01 14:56:04 +02:00
|
|
|
VideosLimit: videosLimit,
|
|
|
|
MaxVideoSize: maxVideoSize,
|
2018-07-17 18:54:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err := sm.Start()
|
2018-05-25 19:57:03 +02:00
|
|
|
if err != nil {
|
2018-07-24 02:01:35 +02:00
|
|
|
sync.SendErrorToSlack(err.Error())
|
2018-06-04 16:35:35 +02:00
|
|
|
}
|
2018-07-24 02:01:35 +02:00
|
|
|
sync.SendInfoToSlack("Syncing process terminated!")
|
2018-04-27 15:14:23 +02:00
|
|
|
}
|