From e560b590f55da698aac8d0579ffbf323a4d3b58c Mon Sep 17 00:00:00 2001 From: Niko Storni Date: Mon, 24 Dec 2018 19:23:40 -0500 Subject: [PATCH] add support for channel claimid --- manager.go | 2 ++ sdk/api.go | 29 +++++++++++++++++++++++++++++ setup.go | 13 ++++++++++--- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/manager.go b/manager.go index 73b3b5a..477e5b3 100644 --- a/manager.go +++ b/manager.go @@ -110,6 +110,7 @@ func (s *SyncManager) Start() error { APIConfig: s.apiConfig, YoutubeChannelID: s.syncProperties.YoutubeChannelID, LbryChannelName: lbryChannelName, + lbryChannelID: channels[0].ChannelClaimID, StopOnError: s.stopOnError, MaxTries: s.maxTries, ConcurrentVideos: s.concurrentVideos, @@ -143,6 +144,7 @@ func (s *SyncManager) Start() error { APIConfig: s.apiConfig, YoutubeChannelID: c.ChannelId, LbryChannelName: c.DesiredChannelName, + lbryChannelID: c.ChannelClaimID, StopOnError: s.stopOnError, MaxTries: s.maxTries, ConcurrentVideos: s.concurrentVideos, diff --git a/sdk/api.go b/sdk/api.go index 1a80226..73217ad 100644 --- a/sdk/api.go +++ b/sdk/api.go @@ -39,6 +39,7 @@ type YoutubeChannel struct { Address string `json:"address"` Currency string `json:"currency"` } `json:"fee"` + ChannelClaimID string `json:"channel_claim_id"` } func (a *APIConfig) FetchChannels(status string, cp *SyncProperties) ([]YoutubeChannel, error) { @@ -117,6 +118,34 @@ func (a *APIConfig) SetChannelStatus(channelID string, status string, failureRea return nil, nil, errors.Err("invalid API response. Status code: %d", res.StatusCode) } +func (a *APIConfig) SetChannelClaimID(channelID string, channelClaimID string) error { + type apiChannelStatusResponse struct { + Success bool `json:"success"` + Error null.String `json:"error"` + Data string `json:"data"` + } + endpoint := a.ApiURL + "/yt/set_channel_claim_id" + res, _ := http.PostForm(endpoint, url.Values{ + "channel_id": {channelID}, + "auth_token": {a.ApiToken}, + "channel_claim_id": {channelClaimID}, + }) + defer res.Body.Close() + body, _ := ioutil.ReadAll(res.Body) + var response apiChannelStatusResponse + err := json.Unmarshal(body, &response) + if err != nil { + return err + } + if !response.Error.IsNull() { + return errors.Err(response.Error.String) + } + if response.Data != "ok" { + return errors.Err("Unexpected API response") + } + return nil +} + const ( VideoStatusPublished = "published" VideoStatusFailed = "failed" diff --git a/setup.go b/setup.go index cc601cf..a0640ae 100644 --- a/setup.go +++ b/setup.go @@ -190,6 +190,10 @@ func (s *Sync) ensureChannelOwnership() error { isChannelMine := false for _, channel := range *channels { if channel.Name == s.LbryChannelName { + //TODO: eventually get rid of this when the whole db is filled + if s.lbryChannelID == "" { + err = s.Manager.apiConfig.SetChannelClaimID(s.YoutubeChannelID, s.lbryChannelID) + } s.lbryChannelID = channel.ClaimID isChannelMine = true } else { @@ -197,7 +201,7 @@ func (s *Sync) ensureChannelOwnership() error { } } if isChannelMine { - return nil + return err } channelBidAmount := channelClaimAmount @@ -211,7 +215,10 @@ func (s *Sync) ensureChannelOwnership() error { balance := decimal.Decimal(*balanceResp) if balance.LessThan(decimal.NewFromFloat(channelBidAmount)) { - s.addCredits(channelBidAmount + 0.1) + err = s.addCredits(channelBidAmount + 0.1) + if err != nil { + return err + } } c, err := s.daemon.ChannelNew(s.LbryChannelName, channelBidAmount) @@ -219,7 +226,7 @@ func (s *Sync) ensureChannelOwnership() error { return err } s.lbryChannelID = c.ClaimID - return nil + return s.Manager.apiConfig.SetChannelClaimID(s.YoutubeChannelID, s.lbryChannelID) } func allUTXOsConfirmed(utxolist *jsonrpc.UTXOListResponse) bool {