2014-12-23 17:38:01 -05:00
|
|
|
// Communicates with rtorrent's XMLRPC interface, and can gather info regarding a .torrent file.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
bencode "github.com/jackpal/bencode-go"
|
|
|
|
)
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
// Torrent Keeps track of a torrent file's and rtorrent's XMLRPC information.
|
2014-12-23 17:38:01 -05:00
|
|
|
type Torrent struct {
|
|
|
|
path string
|
|
|
|
hash string
|
2014-12-29 18:58:23 -05:00
|
|
|
xmlUser string
|
|
|
|
xmlPass string
|
|
|
|
xmlAddress string
|
2014-12-23 17:38:01 -05:00
|
|
|
}
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
// NewTorrent Create a new Torrent instance while computing its hash.
|
|
|
|
func NewTorrent(xmlUser string, xmlPass string, xmlAddress string, filePath string) (*Torrent, error) {
|
|
|
|
hash, err := getTorrentHash(filePath)
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
return &Torrent{filePath, hash, xmlUser, xmlPass, xmlAddress}, nil
|
2014-12-23 17:38:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the torrent hash for a given torrent file path returning an all caps sha1 hash as a string.
|
2014-12-29 18:58:23 -05:00
|
|
|
func getTorrentHash(filePath string) (string, error) {
|
|
|
|
file, err := os.Open(filePath)
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
data, err := bencode.Decode(file)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
decoded, ok := data.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return "", errors.New("unable to convert data to map")
|
|
|
|
}
|
|
|
|
|
|
|
|
var encoded bytes.Buffer
|
|
|
|
bencode.Marshal(&encoded, decoded["info"])
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
encodedString := encoded.String()
|
2014-12-23 17:38:01 -05:00
|
|
|
|
|
|
|
hash := sha1.New()
|
2014-12-29 18:58:23 -05:00
|
|
|
io.WriteString(hash, encodedString)
|
2014-12-23 17:38:01 -05:00
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
hashString := strings.ToUpper(hex.EncodeToString(hash.Sum(nil)))
|
2014-12-23 17:38:01 -05:00
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
return hashString, nil
|
2014-12-23 17:38:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send a command and its argument to the rtorrent XMLRPC and get the response.
|
2014-12-29 18:58:23 -05:00
|
|
|
func (t Torrent) xmlRPCSend (command string, arg string) (string, error) {
|
2014-12-23 17:38:01 -05:00
|
|
|
// This is hacky XML to send to the server
|
|
|
|
buf := []byte("<?xml version='1.0'?>\n" +
|
|
|
|
"<methodCall>\n" +
|
|
|
|
"<methodName>" + command + "</methodName>\n" +
|
|
|
|
"<params>\n" +
|
|
|
|
"<param>\n" +
|
|
|
|
"<value><string>" + arg + "</string></value>\n" +
|
|
|
|
"</param>\n" +
|
|
|
|
"</params>\n" +
|
|
|
|
"</methodCall>\n")
|
|
|
|
|
|
|
|
buffer := bytes.NewBuffer(buf)
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
request, err := http.NewRequest("POST", t.xmlAddress, buffer)
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the basic HTTP auth if we have a user or password
|
2014-12-29 18:58:23 -05:00
|
|
|
if t.xmlUser != "" || t.xmlPass != "" {
|
|
|
|
request.SetBasicAuth(t.xmlUser, t.xmlPass)
|
2014-12-23 17:38:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
resp, err := client.Do(request)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
re, err := regexp.Compile("<value><.*>(.*)</.*></value>")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
values := re.FindAllStringSubmatch(string(body), -1)
|
|
|
|
|
|
|
|
if len(values) != 1 {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return values[0][1], nil
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
// GetTorrentName Get the torrent's name from rtorrent.
|
2014-12-23 17:38:01 -05:00
|
|
|
func (t Torrent) GetTorrentName() (string, error) {
|
2014-12-29 18:58:23 -05:00
|
|
|
return t.xmlRPCSend("d.get_name", t.hash)
|
2014-12-23 17:38:01 -05:00
|
|
|
}
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
// GetTorrentComplete Get the completion status of the torrent from rtorrent.
|
2014-12-23 17:38:01 -05:00
|
|
|
func (t Torrent) GetTorrentComplete() (bool, error) {
|
2014-12-29 18:58:23 -05:00
|
|
|
complete, err := t.xmlRPCSend("d.get_complete", t.hash)
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return complete == "1", nil
|
|
|
|
}
|