2018-10-08 22:19:17 +02:00
package main
import (
"fmt"
"math/rand"
2020-05-19 23:13:01 +02:00
"net/http"
2018-10-08 22:19:17 +02:00
"os"
"time"
2021-11-24 05:54:08 +01:00
"github.com/lbryio/ytsync/v5/configs"
2020-06-11 18:45:56 +02:00
"github.com/lbryio/ytsync/v5/manager"
"github.com/lbryio/ytsync/v5/sdk"
2020-08-08 01:12:55 +02:00
"github.com/lbryio/ytsync/v5/shared"
2020-06-11 18:45:56 +02:00
ytUtils "github.com/lbryio/ytsync/v5/util"
2019-08-13 05:20:09 +02:00
2021-11-24 05:54:08 +01:00
"github.com/lbryio/lbry.go/v2/extras/errors"
"github.com/lbryio/lbry.go/v2/extras/util"
"github.com/prometheus/client_golang/prometheus/promhttp"
2019-07-15 22:16:02 +02:00
log "github.com/sirupsen/logrus"
2018-10-08 22:19:17 +02:00
"github.com/spf13/cobra"
)
var Version string
const defaultMaxTries = 3
var (
2020-08-08 01:12:55 +02:00
cliFlags shared . SyncFlags
2020-07-28 01:35:07 +02:00
maxVideoLength int
2018-10-08 22:19:17 +02:00
)
func main ( ) {
rand . Seed ( time . Now ( ) . UnixNano ( ) )
log . SetLevel ( log . DebugLevel )
2020-05-19 23:13:01 +02:00
http . Handle ( "/metrics" , promhttp . Handler ( ) )
go func ( ) {
log . Error ( http . ListenAndServe ( ":2112" , nil ) )
} ( )
2018-10-08 22:19:17 +02:00
cmd := & cobra . Command {
Use : "ytsync" ,
Short : "Publish youtube channels into LBRY network automatically." ,
Run : ytSync ,
Args : cobra . RangeArgs ( 0 , 0 ) ,
}
2020-08-08 01:12:55 +02:00
cmd . Flags ( ) . IntVar ( & cliFlags . MaxTries , "max-tries" , defaultMaxTries , "Number of times to try a publish that fails" )
cmd . Flags ( ) . BoolVar ( & cliFlags . TakeOverExistingChannel , "takeover-existing-channel" , false , "If channel exists and we don't own it, take over the channel" )
cmd . Flags ( ) . IntVar ( & cliFlags . Limit , "limit" , 0 , "limit the amount of channels to sync" )
cmd . Flags ( ) . BoolVar ( & cliFlags . SkipSpaceCheck , "skip-space-check" , false , "Do not perform free space check on startup" )
cmd . Flags ( ) . BoolVar ( & cliFlags . SyncUpdate , "update" , false , "Update previously synced channels instead of syncing new ones" )
cmd . Flags ( ) . BoolVar ( & cliFlags . SingleRun , "run-once" , false , "Whether the process should be stopped after one cycle or not" )
cmd . Flags ( ) . BoolVar ( & cliFlags . RemoveDBUnpublished , "remove-db-unpublished" , false , "Remove videos from the database that are marked as published but aren't really published" )
cmd . Flags ( ) . BoolVar ( & cliFlags . UpgradeMetadata , "upgrade-metadata" , false , "Upgrade videos if they're on the old metadata version" )
cmd . Flags ( ) . BoolVar ( & cliFlags . DisableTransfers , "no-transfers" , false , "Skips the transferring process of videos, channels and supports" )
cmd . Flags ( ) . BoolVar ( & cliFlags . QuickSync , "quick" , false , "Look up only the last 50 videos from youtube" )
2020-10-27 19:50:10 +01:00
cmd . Flags ( ) . StringVar ( & cliFlags . Status , "status" , "" , "Specify which queue to pull from. Overrides --update" )
cmd . Flags ( ) . StringVar ( & cliFlags . SecondaryStatus , "status2" , "" , "Specify which secondary queue to pull from." )
2020-08-08 01:12:55 +02:00
cmd . Flags ( ) . StringVar ( & cliFlags . ChannelID , "channelID" , "" , "If specified, only this channel will be synced." )
cmd . Flags ( ) . Int64Var ( & cliFlags . SyncFrom , "after" , time . Unix ( 0 , 0 ) . Unix ( ) , "Specify from when to pull jobs [Unix time](Default: 0)" )
cmd . Flags ( ) . Int64Var ( & cliFlags . SyncUntil , "before" , time . Now ( ) . AddDate ( 1 , 0 , 0 ) . Unix ( ) , "Specify until when to pull jobs [Unix time](Default: current Unix time)" )
cmd . Flags ( ) . IntVar ( & cliFlags . ConcurrentJobs , "concurrent-jobs" , 1 , "how many jobs to process concurrently" )
2020-11-19 03:11:23 +01:00
cmd . Flags ( ) . IntVar ( & cliFlags . VideosLimit , "videos-limit" , 0 , "how many videos to process per channel (leave 0 for automatic detection)" )
2020-08-08 01:12:55 +02:00
cmd . Flags ( ) . IntVar ( & cliFlags . MaxVideoSize , "max-size" , 2048 , "Maximum video size to process (in MB)" )
2020-07-28 01:35:07 +02:00
cmd . Flags ( ) . IntVar ( & maxVideoLength , "max-length" , 2 , "Maximum video length to process (in hours)" )
2018-10-08 22:19:17 +02:00
if err := cmd . Execute ( ) ; err != nil {
fmt . Println ( err )
os . Exit ( 1 )
}
}
func ytSync ( cmd * cobra . Command , args [ ] string ) {
2021-11-24 05:54:08 +01:00
err := configs . Init ( "./config.json" )
if err != nil {
log . Fatalf ( "could not parse configuration file: %s" , errors . FullTrace ( err ) )
}
2018-10-08 22:19:17 +02:00
var hostname string
2021-11-24 05:54:08 +01:00
if configs . Configuration . SlackToken == "" {
log . Error ( "A slack token was not present in the config! Slack messages disabled!" )
2018-10-08 22:19:17 +02:00
} else {
var err error
hostname , err = os . Hostname ( )
if err != nil {
log . Error ( "could not detect system hostname" )
hostname = "ytsync-unknown"
}
2019-08-04 00:34:48 +02:00
if len ( hostname ) > 30 {
hostname = hostname [ 0 : 30 ]
}
2021-11-24 05:54:08 +01:00
util . InitSlack ( configs . Configuration . SlackToken , configs . Configuration . SlackChannel , hostname )
2018-10-08 22:19:17 +02:00
}
2020-10-27 19:50:10 +01:00
if cliFlags . Status != "" && ! util . InSlice ( cliFlags . Status , shared . SyncStatuses ) {
2020-08-08 01:12:55 +02:00
log . Errorf ( "status must be one of the following: %v\n" , shared . SyncStatuses )
2018-10-08 22:19:17 +02:00
return
}
2020-08-08 01:12:55 +02:00
if cliFlags . MaxTries < 1 {
2018-10-08 22:19:17 +02:00
log . Errorln ( "setting --max-tries less than 1 doesn't make sense" )
return
}
2020-08-08 01:12:55 +02:00
if cliFlags . Limit < 0 {
2018-10-08 22:19:17 +02:00
log . Errorln ( "setting --limit less than 0 (unlimited) doesn't make sense" )
return
}
2020-08-08 01:12:55 +02:00
cliFlags . MaxVideoLength = time . Duration ( maxVideoLength ) * time . Hour
2018-10-08 22:19:17 +02:00
2021-11-24 05:54:08 +01:00
if configs . Configuration . InternalApisEndpoint == "" {
log . Errorln ( "An Internal APIs Endpoint was not defined" )
2018-10-08 22:19:17 +02:00
return
}
2021-11-24 05:54:08 +01:00
if configs . Configuration . InternalApisAuthToken == "" {
log . Errorln ( "An Internal APIs auth token was not defined" )
2018-10-08 22:19:17 +02:00
return
}
2021-11-24 05:54:08 +01:00
if configs . Configuration . WalletS3Config . ID == "" || configs . Configuration . WalletS3Config . Region == "" || configs . Configuration . WalletS3Config . Bucket == "" || configs . Configuration . WalletS3Config . Secret == "" || configs . Configuration . WalletS3Config . Endpoint == "" {
log . Errorln ( "Wallet S3 configuration is incomplete" )
2018-10-08 22:19:17 +02:00
return
}
2021-11-24 05:54:08 +01:00
if configs . Configuration . BlockchaindbS3Config . ID == "" || configs . Configuration . BlockchaindbS3Config . Region == "" || configs . Configuration . BlockchaindbS3Config . Bucket == "" || configs . Configuration . BlockchaindbS3Config . Secret == "" || configs . Configuration . BlockchaindbS3Config . Endpoint == "" {
log . Errorln ( "Blockchain DBs S3 configuration is incomplete" )
2018-10-08 22:19:17 +02:00
return
}
2021-11-24 05:54:08 +01:00
if configs . Configuration . LbrycrdString == "" {
log . Infoln ( "Using default (local) lbrycrd instance. Set lbrycrd_string if you want to use something else" )
2018-10-08 22:19:17 +02:00
}
2019-07-31 05:32:02 +02:00
blobsDir := ytUtils . GetBlobsDir ( )
2018-10-08 22:19:17 +02:00
apiConfig := & sdk . APIConfig {
2021-11-24 05:54:08 +01:00
ApiURL : configs . Configuration . InternalApisEndpoint ,
ApiToken : configs . Configuration . InternalApisAuthToken ,
HostName : hostname ,
2020-08-08 01:12:55 +02:00
}
2021-11-24 05:54:08 +01:00
2019-01-11 03:02:26 +01:00
sm := manager . NewSyncManager (
2020-08-08 01:12:55 +02:00
cliFlags ,
2018-10-08 22:19:17 +02:00
blobsDir ,
2021-11-24 05:54:08 +01:00
configs . Configuration . LbrycrdString ,
2018-10-08 22:19:17 +02:00
apiConfig ,
)
2021-11-24 05:54:08 +01:00
err = sm . Start ( )
2018-10-08 22:19:17 +02:00
if err != nil {
2019-08-04 00:34:48 +02:00
ytUtils . SendErrorToSlack ( errors . FullTrace ( err ) )
2018-10-08 22:19:17 +02:00
}
2019-07-31 05:32:02 +02:00
ytUtils . SendInfoToSlack ( "Syncing process terminated!" )
2018-10-08 22:19:17 +02:00
}