Skip to content

Commit

Permalink
minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sagan committed Mar 5, 2024
1 parent 7994795 commit 8ea6932
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 23 deletions.
12 changes: 6 additions & 6 deletions cmd/add/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,17 @@ func add(cmd *cobra.Command, args []string) error {
option.Category = addCategory
option.Tags = fixedTags
if err = clientInstance.AddTorrent([]byte(torrent), option, nil); err != nil {
fmt.Printf("✕add (%d/%d) %s: failed to add to client: %v\n", i+1, cntAll, torrent, err)
fmt.Printf("✕ %s (%d/%d): failed to add to client: %v\n", torrent, i+1, cntAll, err)
errorCnt++
} else {
fmt.Printf("✓add (%d/%d) %s\n", i+1, cntAll, torrent)
fmt.Printf("✓ %s (%d/%d)\n", torrent, i+1, cntAll)
}
continue
}
content, tinfo, siteInstance, siteName, filename, id, err :=
helper.GetTorrentContent(torrent, defaultSite, forceLocal, false, stdinTorrentContents, true)
if err != nil {
fmt.Printf("✕add (%d/%d) %s: %v\n", i+1, cntAll, torrent, err)
fmt.Printf("✕ %s (%d/%d): %v\n", torrent, i+1, cntAll, err)
errorCnt++
continue
}
Expand Down Expand Up @@ -174,8 +174,8 @@ func add(cmd *cobra.Command, args []string) error {
}
err = clientInstance.AddTorrent(content, option, nil)
if err != nil {
fmt.Printf("✕add (%d/%d) %s (site=%s): failed to add torrent to client: %v // %s\n",
i+1, cntAll, torrent, siteName, err, contentPath)
fmt.Printf("✕ %s (%d/%d) (site=%s): failed to add torrent to client: %v // %s\n",
torrent, i+1, cntAll, siteName, err, contentPath)
errorCnt++
continue
}
Expand All @@ -192,7 +192,7 @@ func add(cmd *cobra.Command, args []string) error {
}
cntAdded++
sizeAdded += size
fmt.Printf("✓add (%d/%d) %s (site=%s). infoHash=%s // %s\n", i+1, cntAll, torrent, siteName, infoHash, contentPath)
fmt.Printf("✓ %s (%d/%d) (site=%s). infoHash=%s // %s\n", torrent, i+1, cntAll, siteName, infoHash, contentPath)
}
fmt.Printf("\nDone. Added torrent (Size/Cnt): %s / %d; ErrorCnt: %d\n",
util.BytesSize(float64(sizeAdded)), cntAdded, errorCnt)
Expand Down
6 changes: 3 additions & 3 deletions cmd/dltorrent/dltorrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func dltorrent(cmd *cobra.Command, args []string) error {
content, tinfo, _, siteName, filename, id, err :=
helper.GetTorrentContent(torrent, defaultSite, false, true, nil, true)
if err != nil {
fmt.Printf("✕download %s (site=%s): %v\n", torrent, siteName, err)
fmt.Printf("✕ %s (site=%s): %v\n", torrent, siteName, err)
errorCnt++
continue
}
Expand All @@ -63,10 +63,10 @@ func dltorrent(cmd *cobra.Command, args []string) error {
}
err = os.WriteFile(downloadDir+"/"+fileName, content, 0666)
if err != nil {
fmt.Printf("✕download %s (site=%s): failed to save to %s/: %v\n", fileName, siteName, downloadDir, err)
fmt.Printf("✕ %s (site=%s): failed to save to %s/: %v\n", fileName, siteName, downloadDir, err)
errorCnt++
} else {
fmt.Printf("✓download %s (site=%s): saved to %s/\n", fileName, siteName, downloadDir)
fmt.Printf("✓ %s (site=%s): saved to %s/\n", fileName, siteName, downloadDir)
}
}
if errorCnt > 0 {
Expand Down
4 changes: 2 additions & 2 deletions cmd/sites/sites.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ func init() {

func sites(cmd *cobra.Command, args []string) error {
fmt.Printf("<internal supported sites by this program. use --filter flag to find a specific site>\n")
fmt.Printf("%-15s %-15s %-30s %10s %s\n", "Type", "Aliases", "Url", "Schema", "Comment")
fmt.Printf("%-15s %-15s %-30s %13s %s\n", "Type", "Aliases", "Url", "Schema", "Comment")
for _, name := range tpl.SITENAMES {
siteInfo := tpl.SITES[name]
if filter != "" && (!util.ContainsI(siteInfo.GetName(), filter) &&
!util.ContainsI(siteInfo.Url, filter) && !util.ContainsI(siteInfo.Comment, filter)) {
continue
}
fmt.Printf("%-15s %-15s %-30s %10s %s\n",
fmt.Printf("%-15s %-15s %-30s %13s %s\n",
name, strings.Join(siteInfo.Aliases, ","), siteInfo.Url, siteInfo.Type, siteInfo.Comment)
}
return nil
Expand Down
1 change: 0 additions & 1 deletion cmd/status/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ type StatusResponse struct {
Error error
}

// side effect for now: close clientInstance before return
func fetchClientStatus(clientInstance client.Client, showTorrents bool, showAllTorrents bool,
category string, ch chan *StatusResponse) {
response := &StatusResponse{Name: clientInstance.GetName(), Kind: 1}
Expand Down
2 changes: 1 addition & 1 deletion cmd/xseedadd/xseedadd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

var command = &cobra.Command{
Use: "xseedadd {client} {torrentFilename | torrentId | torrentUrl}...",
Annotations: map[string]string{"cobra-prompt-dynamic-suggestions": "xseedcheck"},
Annotations: map[string]string{"cobra-prompt-dynamic-suggestions": "xseedadd"},
Short: "Add xseed torrents to client.",
Long: `Add xseed torrents to client.
Args is torrent list that each one could be a local filename (e.g. "*.torrent" or "[M-TEAM]CLANNAD.torrent"),
Expand Down
6 changes: 5 additions & 1 deletion util/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ func GetTorrentContent(torrent string, defaultSite string,
filename string, id string, err error) {
isLocal := !forceRemote && (forceLocal || torrent == "-" ||
!util.IsUrl(torrent) && strings.HasSuffix(torrent, ".torrent"))
// site torrent id or url
if !isLocal {
// site torrent
if util.IsTorrentUrl(torrent) {
err = fmt.Errorf("magnet or bt url is NOT supported")
return
}
siteName = defaultSite
if !util.IsUrl(torrent) {
if i := strings.Index(torrent, "."); i != -1 {
Expand Down
45 changes: 36 additions & 9 deletions util/torrentutil/torrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/anacrolix/torrent/metainfo"
"github.com/bradfitz/iter"
"github.com/sagan/ptool/client"
"github.com/sagan/ptool/site/public"
"github.com/sagan/ptool/site/tpl"
"github.com/sagan/ptool/util"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -82,27 +83,53 @@ func ParseTorrent(torrentdata []byte, fields int64) (*TorrentMeta, error) {
return torrentMeta, nil
}

// Generate magnet: url of this torrent.
// Must be used on meta parsed from ParseTorrent with fields >= 2
func (meta *TorrentMeta) MagnetUrl() string {
return meta.MetaInfo.Magnet(nil, &meta.Info).String()
}

func (meta *TorrentMeta) Print(name string, showAll bool) {
trackerUrl := ""
if len(meta.Trackers) > 0 {
trackerUrl = meta.Trackers[0]
}
sitenameStr, err := tpl.GuessSiteByTrackers(meta.Trackers, "")
if err != nil {
log.Warnf("Failed to find match site for %s by trackers: %v", name, err)
}
if sitenameStr != "" {

sitenameStr := ""
var err error
if sitenameStr, err = tpl.GuessSiteByTrackers(meta.Trackers, ""); sitenameStr != "" {
sitenameStr = fmt.Sprintf(" (site: %s)", sitenameStr)
} else if err != nil {
log.Warnf("Failed to find match site for %s by trackers: %v", name, err)
} else if site := public.GetSiteByDomain("", meta.Trackers...); site != nil {
sitenameStr = fmt.Sprintf(" (site: %s)", site.Name)
}
fmt.Printf("%s : infohash = %s ; size = %s (%d) ; tracker = %s%s\n",
name, meta.InfoHash, util.BytesSize(float64(meta.Size)), len(meta.Files), trackerUrl, sitenameStr)
if showAll {
comments := []string{}
if meta.MetaInfo.Comment != "" {
comments = append(comments, meta.MetaInfo.Comment)
}
if meta.Info.Private != nil && *meta.Info.Private {
comments = append(comments, "private")
}
if meta.Info.Source != "" {
comments = append(comments, fmt.Sprintf("source:%s", meta.Info.Source))
}
comment := ""
if len(comments) > 0 {
comment = " // " + strings.Join(comments, ", ")
}
if meta.SingleFileTorrent {
fmt.Printf("-- RawSize = %d ; SingleFile = %s ; AllTrackers: %s ; // %s\n",
meta.Size, meta.Files[0].Path, strings.Join(meta.Trackers, " | "), meta.MetaInfo.Comment)
fmt.Printf("! RawSize = %d ; SingleFile = %s ; AllTrackers: %s ;%s\n",
meta.Size, meta.Files[0].Path, strings.Join(meta.Trackers, " | "), comment)
} else {
fmt.Printf("-- RawSize = %d ; RootDir = %s ; AllTrackers: %s ; // %s\n",
meta.Size, meta.RootDir, strings.Join(meta.Trackers, " | "), meta.MetaInfo.Comment)
fmt.Printf("! RawSize = %d ; RootDir = %s ; AllTrackers: %s ;%s\n",
meta.Size, meta.RootDir, strings.Join(meta.Trackers, " | "), comment)
}
if meta.Info.Private == nil || !*meta.Info.Private {
fmt.Printf("! MagnetURI: %s\n", meta.MagnetUrl())
}
}
}
Expand Down

0 comments on commit 8ea6932

Please sign in to comment.