package main import ( "bufio" "encoding/gob" "encoding/hex" "fmt" "io" "os" "os/exec" "strconv" "strings" "github.com/dustin/go-humanize" "go.stargrave.org/rutrackerer" ) func main() { cmd := exec.Command("grep", append([]string{"-n"}, os.Args[1:]...)...) grepStdin, err := cmd.StdinPipe() if err != nil { panic(err) } grepStdout, err := cmd.StdoutPipe() if err != nil { panic(err) } if err = cmd.Start(); err != nil { panic(err) } torrents := make([]*rutrackerer.Torrent, 0, 1<<20) printer := make(chan struct{}) go func() { scanner := bufio.NewScanner(grepStdout) var t string var i int var err error var torrent *rutrackerer.Torrent for scanner.Scan() { t = scanner.Text() i = strings.IndexByte(t, ':') i, err = strconv.Atoi(t[:i]) if err != nil { panic(err) } torrent = torrents[i-1] fmt.Printf( `%s %s %s %d https://rutracker.org/forum/viewtopic.php?t=%d magnet:?xt=urn:btih:%s `, torrent.Title, torrent.Registered.Format("2006-01-02T15:04:05"), humanize.IBytes(uint64(torrent.Size)), torrent.Offset, torrent.Id, strings.ToUpper(hex.EncodeToString(torrent.Hash[:])), ) } if err = scanner.Err(); err != nil { panic(err) } close(printer) }() gobDec := gob.NewDecoder(bufio.NewReader(os.Stdin)) for { var torrent rutrackerer.Torrent if err = gobDec.Decode(&torrent); err != nil { if err == io.EOF { break } panic(err) } torrents = append(torrents, &torrent) if _, err = grepStdin.Write([]byte(torrent.Title + "\n")); err != nil { panic(err) } } grepStdin.Close() <-printer if err = cmd.Wait(); err != nil { panic(err) } }