2018-02-13 18:47:05 +01:00
|
|
|
package sources
|
|
|
|
|
|
|
|
import (
|
2018-06-04 16:35:35 +02:00
|
|
|
"fmt"
|
2018-02-13 18:47:05 +01:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2018-06-04 16:35:35 +02:00
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
|
|
|
|
2018-02-13 18:47:05 +01:00
|
|
|
"github.com/lbryio/lbry.go/jsonrpc"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
var titleRegexp = regexp.MustCompile(`[^a-zA-Z0-9]+`)
|
|
|
|
|
2018-07-21 01:56:36 +02:00
|
|
|
type SyncSummary struct {
|
|
|
|
ClaimID string
|
|
|
|
ClaimName string
|
|
|
|
}
|
|
|
|
|
2018-02-13 18:47:05 +01:00
|
|
|
func getClaimNameFromTitle(title string, attempt int) string {
|
|
|
|
suffix := ""
|
|
|
|
if attempt > 1 {
|
|
|
|
suffix = "-" + strconv.Itoa(attempt)
|
|
|
|
}
|
|
|
|
maxLen := 40 - len(suffix)
|
|
|
|
|
|
|
|
chunks := strings.Split(strings.ToLower(strings.Trim(titleRegexp.ReplaceAllString(title, "-"), "-")), "-")
|
|
|
|
|
|
|
|
name := chunks[0]
|
|
|
|
if len(name) > maxLen {
|
|
|
|
return name[:maxLen]
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, chunk := range chunks[1:] {
|
|
|
|
tmpName := name + "-" + chunk
|
|
|
|
if len(tmpName) > maxLen {
|
|
|
|
if len(name) < 20 {
|
|
|
|
name = tmpName[:maxLen]
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
name = tmpName
|
|
|
|
}
|
|
|
|
|
|
|
|
return name + suffix
|
|
|
|
}
|
|
|
|
|
2018-08-23 00:28:31 +02:00
|
|
|
func publishAndRetryExistingNames(daemon *jsonrpc.Client, title, filename string, amount float64, options jsonrpc.PublishOptions, claimNames map[string]bool, syncedVideosMux *sync.RWMutex) (*SyncSummary, error) {
|
2018-02-13 18:47:05 +01:00
|
|
|
attempt := 0
|
|
|
|
for {
|
|
|
|
attempt++
|
|
|
|
name := getClaimNameFromTitle(title, attempt)
|
|
|
|
|
2018-08-23 00:28:31 +02:00
|
|
|
syncedVideosMux.Lock()
|
|
|
|
_, exists := claimNames[name]
|
2018-02-13 18:47:05 +01:00
|
|
|
if exists {
|
2018-08-23 00:28:31 +02:00
|
|
|
log.Printf("name exists, retrying (%d attempts so far)", attempt)
|
|
|
|
syncedVideosMux.Unlock()
|
2018-02-13 18:47:05 +01:00
|
|
|
continue
|
|
|
|
}
|
2018-08-23 00:28:31 +02:00
|
|
|
claimNames[name] = false
|
|
|
|
syncedVideosMux.Unlock()
|
|
|
|
|
2018-06-04 16:35:35 +02:00
|
|
|
//if for some reasons the title can't be converted in a valid claim name (too short or not latin) then we use a hash
|
|
|
|
if len(name) < 2 {
|
|
|
|
hasher := md5.New()
|
|
|
|
hasher.Write([]byte(title))
|
|
|
|
name = fmt.Sprintf("%s-%d", hex.EncodeToString(hasher.Sum(nil))[:15], attempt)
|
|
|
|
}
|
2018-02-13 18:47:05 +01:00
|
|
|
|
2018-07-21 01:56:36 +02:00
|
|
|
response, err := daemon.Publish(name, filename, amount, options)
|
2018-02-13 18:47:05 +01:00
|
|
|
if err == nil || strings.Contains(err.Error(), "failed: Multiple claims (") {
|
2018-08-23 00:28:31 +02:00
|
|
|
syncedVideosMux.Lock()
|
|
|
|
claimNames[name] = true
|
|
|
|
syncedVideosMux.Unlock()
|
2018-02-13 18:47:05 +01:00
|
|
|
if err == nil {
|
2018-07-21 01:56:36 +02:00
|
|
|
return &SyncSummary{ClaimID: response.ClaimID, ClaimName: name}, nil
|
2018-02-13 18:47:05 +01:00
|
|
|
} else {
|
2018-08-23 00:28:31 +02:00
|
|
|
log.Printf("name exists, retrying (%d attempts so far)", attempt)
|
2018-02-13 18:47:05 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else {
|
2018-07-21 01:56:36 +02:00
|
|
|
return nil, err
|
2018-02-13 18:47:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|