Skip to content

Commit

Permalink
feat: qbt hash torrent/magnet to print hash (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
klahaha committed Jan 26, 2022
1 parent 928035e commit d702357
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
47 changes: 47 additions & 0 deletions cmd/hash.go
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
}
1 change: 1 addition & 0 deletions cmd/qbt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Documentation is available at https://github.com/ludviglundgren/qbittorrent-cli`
rootCmd.AddCommand(cmd.RunMove())
rootCmd.AddCommand(cmd.RunCompare())
rootCmd.AddCommand(cmd.RunEdit())
rootCmd.AddCommand(cmd.RunHash())

if err := rootCmd.Execute(); err != nil {
os.Exit(1)
Expand Down

0 comments on commit d702357

Please sign in to comment.