-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit cec7d8d
Showing
7 changed files
with
315 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,19 @@ | ||
vendor/ | ||
|
||
*.o | ||
*.a | ||
*.so | ||
_obj | ||
_test | ||
*.[568vq] | ||
[568vq].out | ||
*.cgo1.go | ||
*.cgo2.c | ||
_cgo_defun.c | ||
_cgo_gotypes.go | ||
_cgo_export.* | ||
_testmain.go | ||
*.exe | ||
*.test | ||
*.prof | ||
*.out |
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,22 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016 Matthias Loibl | ||
Copyright (c) 2014 Long Nguyen, Tobias Blom | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
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,3 @@ | ||
.PHONY: install | ||
install: | ||
go install ./cmd/transmission-exporter |
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,45 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"sort" | ||
"text/tabwriter" | ||
"time" | ||
|
||
transmission "github.com/metalmatze/transmission-exporter" | ||
) | ||
|
||
func main() { | ||
client := transmission.New("http://localhost:9091", nil) | ||
|
||
for { | ||
torrents, err := client.GetTorrents() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
sort.Sort(sort.Reverse(transmission.ByRatio(torrents))) | ||
//sort.Sort(sort.Reverse(transmission.ByDate(torrents))) | ||
|
||
print("\033[H\033[2J") | ||
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0) | ||
|
||
fmt.Fprintln(w, "Name\tRatio\tUp\tDown\tPercent\tDate") | ||
for _, t := range torrents { | ||
fmt.Fprintf(w, | ||
"%s\t%f\t%d\t%d\t%f\t%v\n", | ||
t.Name, | ||
t.UploadRatio, | ||
t.RateUpload, | ||
t.RateDownload, | ||
t.PercentDone, | ||
time.Unix(int64(t.Date), 0), | ||
) | ||
} | ||
w.Flush() | ||
|
||
time.Sleep(time.Second) | ||
} | ||
} |
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,24 @@ | ||
package transmission | ||
|
||
type ( | ||
RPCCommand struct { | ||
Method string `json:"method,omitempty"` | ||
Arguments RPCArguments `json:"arguments,omitempty"` | ||
Result string `json:"result,omitempty"` | ||
} | ||
RPCArguments struct { | ||
Fields []string `json:"fields,omitempty"` | ||
Torrents []Torrent `json:"torrents,omitempty"` | ||
Ids []int `json:"ids,omitempty"` | ||
DeleteData bool `json:"delete-local-data,omitempty"` | ||
DownloadDir string `json:"download-dir,omitempty"` | ||
MetaInfo string `json:"metainfo,omitempty"` | ||
Filename string `json:"filename,omitempty"` | ||
TorrentAdded RPCTorrentAdded `json:"torrent-added"` | ||
} | ||
RPCTorrentAdded struct { | ||
HashString string `json:"hashString"` | ||
ID int `json:"id"` | ||
Name string `json:"name"` | ||
} | ||
) |
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,44 @@ | ||
package transmission | ||
|
||
type ( | ||
//Torrent represents a transmission torrent | ||
Torrent struct { | ||
ID int `json:"id"` | ||
Name string `json:"name"` | ||
Status int `json:"status"` | ||
Date int `json:"addedDate"` | ||
LeftUntilDone int `json:"leftUntilDone"` | ||
Eta int `json:"eta"` | ||
UploadRatio float64 `json:"uploadRatio"` | ||
RateDownload int `json:"rateDownload"` | ||
RateUpload int `json:"rateUpload"` | ||
DownloadDir string `json:"downloadDir"` | ||
IsFinished bool `json:"isFinished"` | ||
PercentDone float64 `json:"percentDone"` | ||
SeedRatioMode int `json:"seedRatioMode"` | ||
HashString string `json:"hashString"` | ||
Error int `json:"error"` | ||
ErrorString string `json:"errorString"` | ||
} | ||
|
||
ByID []Torrent | ||
ByName []Torrent | ||
ByDate []Torrent | ||
ByRatio []Torrent | ||
) | ||
|
||
func (t ByID) Len() int { return len(t) } | ||
func (t ByID) Swap(i, j int) { t[i], t[j] = t[j], t[i] } | ||
func (t ByID) Less(i, j int) bool { return t[i].ID < t[j].ID } | ||
|
||
func (t ByName) Len() int { return len(t) } | ||
func (t ByName) Swap(i, j int) { t[i], t[j] = t[j], t[i] } | ||
func (t ByName) Less(i, j int) bool { return t[i].Name < t[j].Name } | ||
|
||
func (t ByDate) Len() int { return len(t) } | ||
func (t ByDate) Swap(i, j int) { t[i], t[j] = t[j], t[i] } | ||
func (t ByDate) Less(i, j int) bool { return t[i].Date < t[j].Date } | ||
|
||
func (t ByRatio) Len() int { return len(t) } | ||
func (t ByRatio) Swap(i, j int) { t[i], t[j] = t[j], t[i] } | ||
func (t ByRatio) Less(i, j int) bool { return t[i].UploadRatio < t[j].UploadRatio } |
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,158 @@ | ||
package transmission | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
const endpoint = "/transmission/rpc" | ||
|
||
type ( | ||
User struct { | ||
Username string | ||
Password string | ||
} | ||
Client struct { | ||
URL string | ||
token string | ||
|
||
User *User | ||
client http.Client | ||
} | ||
) | ||
|
||
//New create new transmission torrent | ||
func New(url string, user *User) Client { | ||
return Client{ | ||
URL: url + endpoint, | ||
User: user, | ||
} | ||
} | ||
|
||
func (c *Client) post(body []byte) ([]byte, error) { | ||
authRequest, err := c.authRequest("POST", body) | ||
if err != nil { | ||
return make([]byte, 0), err | ||
} | ||
|
||
res, err := c.client.Do(authRequest) | ||
if err != nil { | ||
return make([]byte, 0), err | ||
} | ||
defer res.Body.Close() | ||
|
||
if res.StatusCode == 409 { | ||
c.getToken() | ||
authRequest, err := c.authRequest("POST", body) | ||
if err != nil { | ||
return make([]byte, 0), err | ||
} | ||
res, err = c.client.Do(authRequest) | ||
if err != nil { | ||
return make([]byte, 0), err | ||
} | ||
} | ||
|
||
resBody, err := ioutil.ReadAll(res.Body) | ||
if err != nil { | ||
return make([]byte, 0), err | ||
} | ||
return resBody, nil | ||
} | ||
|
||
func (c *Client) getToken() error { | ||
req, err := http.NewRequest("POST", c.URL, strings.NewReader("")) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if c.User != nil { | ||
req.SetBasicAuth(c.User.Username, c.User.Password) | ||
} | ||
|
||
res, err := c.client.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
defer res.Body.Close() | ||
c.token = res.Header.Get("X-Transmission-Session-Id") | ||
return nil | ||
} | ||
|
||
func (c *Client) authRequest(method string, body []byte) (*http.Request, error) { | ||
if c.token == "" { | ||
err := c.getToken() | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
req, err := http.NewRequest(method, c.URL, bytes.NewReader(body)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.Header.Add("X-Transmission-Session-Id", c.token) | ||
|
||
if c.User != nil { | ||
req.SetBasicAuth(c.User.Username, c.User.Password) | ||
} | ||
|
||
return req, nil | ||
} | ||
|
||
func (c *Client) ExecuteCommand(cmd *RPCCommand) (*RPCCommand, error) { | ||
var out RPCCommand | ||
|
||
body, err := json.Marshal(&cmd) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
output, err := c.post(body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = json.Unmarshal(output, &out) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &out, nil | ||
} | ||
|
||
//GetTorrents get a list of torrents | ||
func (ac *Client) GetTorrents() ([]Torrent, error) { | ||
cmd := &RPCCommand{ | ||
Method: "torrent-get", | ||
Arguments: RPCArguments{ | ||
Fields: []string{ | ||
"id", | ||
"name", | ||
"hashString", | ||
"status", | ||
"addedDate", | ||
"leftUntilDone", | ||
"eta", | ||
"uploadRatio", | ||
"rateDownload", | ||
"rateUpload", | ||
"downloadDir", | ||
"isFinished", | ||
"percentDone", | ||
"seedRatioMode", | ||
"error", | ||
"errorString", | ||
}, | ||
}, | ||
} | ||
|
||
out, err := ac.ExecuteCommand(cmd) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return out.Arguments.Torrents, nil | ||
} |