]> Sergey Matveev's repositories - rutrackerer.git/blob - cmd/search/main.go
Initial commit
[rutrackerer.git] / cmd / search / main.go
1 package main
2
3 import (
4         "bufio"
5         "encoding/gob"
6         "encoding/hex"
7         "fmt"
8         "io"
9         "os"
10         "os/exec"
11         "strconv"
12         "strings"
13
14         "github.com/dustin/go-humanize"
15         "go.stargrave.org/rutrackerer"
16 )
17
18 func main() {
19         cmd := exec.Command("grep", append([]string{"-n"}, os.Args[1:]...)...)
20         grepStdin, err := cmd.StdinPipe()
21         if err != nil {
22                 panic(err)
23         }
24         grepStdout, err := cmd.StdoutPipe()
25         if err != nil {
26                 panic(err)
27         }
28         if err = cmd.Start(); err != nil {
29                 panic(err)
30         }
31         torrents := make([]*rutrackerer.Torrent, 0, 1<<20)
32         printer := make(chan struct{})
33         go func() {
34                 scanner := bufio.NewScanner(grepStdout)
35                 var t string
36                 var i int
37                 var err error
38                 var torrent *rutrackerer.Torrent
39                 for scanner.Scan() {
40                         t = scanner.Text()
41                         i = strings.IndexByte(t, ':')
42                         i, err = strconv.Atoi(t[:i])
43                         if err != nil {
44                                 panic(err)
45                         }
46                         torrent = torrents[i-1]
47                         fmt.Printf(
48                                 `%s
49         %s %s %d
50         https://rutracker.org/forum/viewtopic.php?t=%d
51         magnet:?xt=urn:btih:%s
52 `,
53                                 torrent.Title,
54                                 torrent.Registered.Format("2006-01-02T15:04:05"),
55                                 humanize.IBytes(uint64(torrent.Size)),
56                                 torrent.Offset,
57                                 torrent.Id,
58                                 strings.ToUpper(hex.EncodeToString(torrent.Hash[:])),
59                         )
60                 }
61                 if err = scanner.Err(); err != nil {
62                         panic(err)
63                 }
64                 close(printer)
65         }()
66         gobDec := gob.NewDecoder(bufio.NewReader(os.Stdin))
67         for {
68                 var torrent rutrackerer.Torrent
69                 if err = gobDec.Decode(&torrent); err != nil {
70                         if err == io.EOF {
71                                 break
72                         }
73                         panic(err)
74                 }
75                 torrents = append(torrents, &torrent)
76                 if _, err = grepStdin.Write([]byte(torrent.Title + "\n")); err != nil {
77                         panic(err)
78                 }
79         }
80         grepStdin.Close()
81         <-printer
82         if err = cmd.Wait(); err != nil {
83                 panic(err)
84         }
85 }