2017-09-27 20:10:41 +02:00
|
|
|
package cmd
|
2017-09-15 13:58:54 +02:00
|
|
|
|
2017-09-16 00:13:13 +02:00
|
|
|
import (
|
2018-04-27 15:14:23 +02:00
|
|
|
"os"
|
2018-05-17 01:42:06 +02:00
|
|
|
"os/user"
|
2018-04-27 15:14:23 +02:00
|
|
|
|
2018-05-17 01:42:06 +02:00
|
|
|
"github.com/lbryio/lbry.go/errors"
|
2018-04-26 12:12:54 +02:00
|
|
|
"github.com/lbryio/lbry.go/util"
|
2018-05-17 01:42:06 +02:00
|
|
|
sync "github.com/lbryio/lbry.go/ytsync"
|
2017-09-16 00:13:13 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2017-09-27 20:10:41 +02:00
|
|
|
"github.com/spf13/cobra"
|
2017-09-16 00:13:13 +02:00
|
|
|
)
|
|
|
|
|
2017-09-27 20:10:41 +02:00
|
|
|
func init() {
|
|
|
|
var ytSyncCmd = &cobra.Command{
|
2017-12-30 01:21:16 +01:00
|
|
|
Use: "ytsync <youtube_api_key> <lbry_channel_name> [<youtube_channel_id>]",
|
2017-09-27 20:10:41 +02:00
|
|
|
Args: cobra.RangeArgs(2, 3),
|
|
|
|
Short: "Publish youtube channel into LBRY network.",
|
|
|
|
Run: ytsync,
|
|
|
|
}
|
2017-10-11 02:53:11 +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")
|
2017-11-06 22:42:52 +01:00
|
|
|
ytSyncCmd.Flags().BoolVar(&takeOverExistingChannel, "takeover-existing-channel", false, "If channel exists and we don't own it, take over the channel")
|
2018-03-12 21:58:37 +01:00
|
|
|
ytSyncCmd.Flags().IntVar(&refill, "refill", 0, "Also add this many credits to the wallet")
|
2018-05-17 01:42:06 +02:00
|
|
|
ytSyncCmd.Flags().BoolVar(&skipSpaceCheck, "skip-space-check", false, "Do not perform free space check on startup")
|
2017-09-27 20:10:41 +02:00
|
|
|
RootCmd.AddCommand(ytSyncCmd)
|
2018-04-27 15:14:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func ytsync(cmd *cobra.Command, args []string) {
|
2018-05-17 02:08:52 +02:00
|
|
|
slackToken := os.Getenv("SLACK_TOKEN")
|
|
|
|
if slackToken == "" {
|
|
|
|
log.Error("A slack token was not present in env vars! Slack messages disabled!")
|
|
|
|
} else {
|
|
|
|
util.InitSlack(os.Getenv("SLACK_TOKEN"))
|
|
|
|
}
|
2018-05-17 01:42:06 +02:00
|
|
|
usr, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
util.SendToSlackError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
usedPctile, err := util.GetUsedSpace(usr.HomeDir + "/.lbrynet/blobfiles/")
|
|
|
|
if err != nil {
|
|
|
|
util.SendToSlackError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if usedPctile > 0.9 && !skipSpaceCheck {
|
2018-05-17 02:08:52 +02:00
|
|
|
util.SendToSlackError("more than 90%% of the space has been used. use --skip-space-check to ignore. Used: %.1f%%", usedPctile*100)
|
2018-05-17 01:42:06 +02:00
|
|
|
return
|
|
|
|
}
|
2018-05-17 02:08:52 +02:00
|
|
|
util.SendToSlackInfo("disk usage: %.1f%%", usedPctile*100)
|
|
|
|
|
2017-10-11 04:02:16 +02:00
|
|
|
ytAPIKey := args[0]
|
2017-12-30 01:21:16 +01:00
|
|
|
lbryChannelName := args[1]
|
|
|
|
if string(lbryChannelName[0]) != "@" {
|
|
|
|
log.Errorln("LBRY channel name must start with an @")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
channelID := ""
|
2017-09-27 20:10:41 +02:00
|
|
|
if len(args) > 2 {
|
2017-12-30 01:21:16 +01:00
|
|
|
channelID = args[2]
|
2017-09-16 00:13:13 +02:00
|
|
|
}
|
|
|
|
|
2017-10-11 02:53:11 +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")
|
2017-10-04 16:31:03 +02:00
|
|
|
return
|
|
|
|
}
|
2018-05-05 13:22:33 +02:00
|
|
|
util.SendToSlackInfo("Syncing " + lbryChannelName + " to LBRY!")
|
2017-10-04 16:31:03 +02:00
|
|
|
|
2017-10-11 04:02:16 +02:00
|
|
|
s := sync.Sync{
|
2017-11-06 22:42:52 +01:00
|
|
|
YoutubeAPIKey: ytAPIKey,
|
|
|
|
YoutubeChannelID: channelID,
|
|
|
|
LbryChannelName: lbryChannelName,
|
|
|
|
StopOnError: stopOnError,
|
|
|
|
MaxTries: maxTries,
|
|
|
|
ConcurrentVideos: 1,
|
|
|
|
TakeOverExistingChannel: takeOverExistingChannel,
|
2018-03-12 21:58:37 +01:00
|
|
|
Refill: refill,
|
2017-09-19 20:14:52 +02:00
|
|
|
}
|
|
|
|
|
2018-05-17 01:42:06 +02:00
|
|
|
err = s.FullCycle()
|
2017-12-30 01:21:16 +01:00
|
|
|
|
2017-09-16 00:13:13 +02:00
|
|
|
if err != nil {
|
2018-05-05 13:22:33 +02:00
|
|
|
util.SendToSlackError(errors.FullTrace(err))
|
2017-09-16 00:13:13 +02:00
|
|
|
}
|
2018-05-05 13:22:33 +02:00
|
|
|
util.SendToSlackInfo("Syncing " + lbryChannelName + " reached an end.")
|
2017-09-15 13:58:54 +02:00
|
|
|
}
|