-
Notifications
You must be signed in to change notification settings - Fork 18
/
xseedcheck.go
87 lines (78 loc) · 3.25 KB
/
xseedcheck.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package xseedcheck
import (
"fmt"
"github.com/spf13/cobra"
"github.com/sagan/ptool/client"
"github.com/sagan/ptool/cmd"
"github.com/sagan/ptool/util/helper"
)
var command = &cobra.Command{
Use: "xseedcheck {client} {infoHash} {torrentFilename | torrentId | torrentUrl}",
Annotations: map[string]string{"cobra-prompt-dynamic-suggestions": "xseedcheck"},
Short: "Check whether a torrent in client is identical with a torrent file.",
Long: `Check whether a torrent in client is identical with a torrent file.
{torrentFilename | torrentId | torrentUrl}: could be a local filename (e.g. "*.torrent" or "[M-TEAM]CLANNAD.torrent"),
site torrent id (e.g.: "mteam.488424") or url (e.g.: "https://kp.m-team.cc/details.php?id=488424").
Torrent url that does NOT belong to any site (e.g.: a public site url) is also supported.
Use a single "-" to read .torrent file contents from stdin.
Only filename and size will be compared. Not the disk file contents themselves.`,
Args: cobra.MatchAll(cobra.ExactArgs(3), cobra.OnlyValidArgs),
RunE: xseedcheck,
}
var (
showAll = false
forceLocal = false
defaultSite = ""
)
func init() {
command.Flags().BoolVarP(&showAll, "all", "a", false, "show full comparison result")
command.Flags().BoolVarP(&forceLocal, "force-local", "", false, "Force treat the arg as local torrent filename")
command.Flags().StringVarP(&defaultSite, "site", "", "", "Set default site of torrent url")
cmd.RootCmd.AddCommand(command)
}
func xseedcheck(cmd *cobra.Command, args []string) error {
clientName := args[0]
infoHash := args[1]
torrent := args[2]
clientInstance, err := client.CreateClient(clientName)
if err != nil {
return fmt.Errorf("failed to create client: %v", err)
}
_, tinfo, _, _, _, _, err := helper.GetTorrentContent(torrent, defaultSite, forceLocal, false, nil, false)
if err != nil {
return fmt.Errorf("failed to get %s: %v", torrent, err)
}
if tinfo.InfoHash == infoHash {
fmt.Printf("Result: identical. Torrent %s has the same infoHash with client %s torrent.\n", torrent, clientName)
return nil
}
clientTorrentContents, err := clientInstance.GetTorrentContents(infoHash)
if err != nil {
return fmt.Errorf("failed to get client torrent contents info: %v", err)
}
compareResult := tinfo.XseedCheckWithClientTorrent(clientTorrentContents)
if compareResult == 0 {
fmt.Printf("Result: ✓. Torrent %s has the same contents with client %s torrent.\n", torrent, clientName)
} else if compareResult == 1 {
fmt.Printf("Result: ✓*. Torrent %s has the same (partial) contents with client %s torrent.\n", torrent, clientName)
} else if compareResult == -2 {
fmt.Printf("Result: X*. Torrent %s has the DIFFERENT root folder, but same contents with client %s torrent.\n",
torrent, clientName)
} else {
fmt.Printf("Result: X. Torrent %s does NOT has the same contents with client %s torrent.\n", torrent, clientName)
}
if showAll {
fmt.Printf("\n")
fmt.Printf("Client: %s torrent\n", infoHash)
for i, clientTorrentFile := range clientTorrentContents {
fmt.Printf("%-5d %-15d %s\n", i+1, clientTorrentFile.Size, clientTorrentFile.Path)
}
fmt.Printf("\n")
fmt.Printf("Torrent file: %s\n", torrent)
tinfo.PrintFiles(true, true)
}
if compareResult < 0 {
return fmt.Errorf("not match")
}
return nil
}