-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: qbt hash torrent/magnet to print hash (#29)
- Loading branch information
Showing
2 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"github.com/anacrolix/torrent/metainfo" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// RunAdd cmd to add torrents | ||
func RunHash() *cobra.Command { | ||
var command = &cobra.Command{ | ||
Use: "hash", | ||
Short: "Print the hash of a torrent/magnet", | ||
Example: ` qbt hash file.torrent`, | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if len(args) < 1 { | ||
return errors.New("requires a torrent file or magnet URI as first argument") | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
command.Run = func(cmd *cobra.Command, args []string) { | ||
filePath := args[0] | ||
hash := "" | ||
if strings.HasPrefix(filePath, "magnet:") { | ||
metadata, err := metainfo.ParseMagnetURI(filePath) | ||
if err != nil { | ||
log.Fatalf("could not parse magnet URI. error: %v", err) | ||
} | ||
hash = metadata.InfoHash.HexString() | ||
} else { | ||
metadata, err := metainfo.LoadFromFile(filePath) | ||
if err != nil { | ||
log.Fatalf("could not parse torrent file. error: %v", err) | ||
} | ||
hash = metadata.HashInfoBytes().HexString() | ||
} | ||
fmt.Println(hash) | ||
} | ||
return command | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters