From 12af1ce85024ecac10e0fba32b700ccbddc07cad Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 6 Jun 2019 04:50:51 -0700 Subject: [PATCH] Turn off crazy noise from anacrolix/torrent https://github.com/anacrolix/torrent/issues/271 --- .gitignore | 4 +++- README.md | 12 +++++++++++- main.go | 22 +++++++++++++++++++--- torrent/client.go | 23 ++++++++++++++++++++++- 4 files changed, 55 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index a337b9a..c8f08ce 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,6 @@ bin # I don't know why github.com/anacrolix/torrent is using these for storage .torrent.bolt.db -.torrent.bolt.db.lock \ No newline at end of file +.torrent.bolt.db.lock + +*.log \ No newline at end of file diff --git a/README.md b/README.md index 818a8b5..a40956a 100644 --- a/README.md +++ b/README.md @@ -33,4 +33,14 @@ $ bin/kabedon-nyaa ``` # Notes -github.com/anacrolix/torrent uses github.com/anacrolix/go-libutp which is a go wrapper for github.com/bittorrent/libutp and it requires gcc to compile bittorrent's transport protocol library. \ No newline at end of file +github.com/anacrolix/torrent uses github.com/anacrolix/go-libutp which is a go wrapper for github.com/bittorrent/libutp and it requires gcc to compile bittorrent's transport protocol library. + +github.com/anacrolix/torrent client doesn't expose the logger. Unfortunately, it's outputting a lot of noise. +```bash +$ ./kabedon-nyaa +go-libutp: 2019/06/06 03:53:10 socket.go:172: ignoring socket read error: read udp4 0.0.0.0:58865: wsarecvfrom: The connection has been broken due to keep-alive activity detecting a failure while the operation was in progress. +go-libutp: 2019/06/06 03:53:11 socket.go:172: ignoring socket read error: read udp4 0.0.0.0:58865: wsarecvfrom: The connection has been broken due to keep-alive activity detecting a failure while the operation was in progress. +go-libutp: 2019/06/06 03:53:11 socket.go:172: ignoring socket read error: read udp4 0.0.0.0:58865: wsarecvfrom: The connection has been broken due to keep-alive activity detecting a failure while the operation was in progress. +2019-06-06 03:53:12 portfwd.go:31: discovered 0 upnp devices +``` +Turn it off by setting os.Stdout = nil and just log to a file instead, though this can be disadvantageous... \ No newline at end of file diff --git a/main.go b/main.go index 19cacbe..6075b8d 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "os" "fmt" + "log" "github.com/greenmochi/kabedon-nyaa/torrent" ) @@ -13,12 +14,27 @@ const ( func main() { // Run gRPC service, uncomment/comment when you don't want it running //grpc.RunGRPC(port) + + // anacrolix/torrent is outputting crazy noise, turn it off and log to file instead + os.Stdout = nil + + f, err := os.Create("kabedon-nyaa.log") + if err != nil { + log.Println("unable to create log file") + } + defer f.Close() + client, err := torrent.NewClient() if err != nil { - fmt.Println(err) + fmt.Fprintf(f, "%+v\n", err) os.Exit(1) } - if client != nil { - fmt.Println("client no nil") + defer client.Close() + + fmt.Fprintf(f, "Adding magnet") + magnet := "magnet:?xt=urn:btih:464aa94a230061049a1017c7550e6cf724aa9767&dn=%5BHorribleSubs%5D%20Sewayaki%20Kitsune%20no%20Senko-san%20-%2009%20%5B360p%5D.mkv&tr=http%3A%2F%2Fnyaa.tracker.wf%3A7777%2Fannounce&tr=udp%3A%2F%2Fopen.stealth.si%3A80%2Fannounce&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969%2Fannounce&tr=udp%3A%2F%2Fexodus.desync.com%3A6969%2Fannounce" + if ok := client.AddMagnet(magnet); !ok { + fmt.Fprintf(f, "unable to add magnet") } + fmt.Fprintf(f, "done") } diff --git a/torrent/client.go b/torrent/client.go index 9e92770..d33ce49 100644 --- a/torrent/client.go +++ b/torrent/client.go @@ -5,15 +5,36 @@ import "github.com/anacrolix/torrent" // Client wraps the torrent client type Client struct { client *torrent.Client + torrents []*torrent.Torrent } -// NewClient wraps anacrolix torrent new client +// NewClient wraps NewClient func NewClient() (*Client, error) { client, err := torrent.NewClient(nil) if err != nil { return nil, err } + return &Client{ client: client, }, nil +} + +// Close wraps Close +func (c *Client) Close() { + c.Close() +} + +// AddMagnet wraps AddMagnet +func (c *Client) AddMagnet(uri string) bool { + torrent, err := c.client.AddMagnet(uri) + if err != nil { + return false + } + + <- torrent.GotInfo() + torrent.DownloadAll() + c.client.WaitAll() + c.torrents = append(c.torrents, torrent) + return true } \ No newline at end of file