-
Notifications
You must be signed in to change notification settings - Fork 8
/
search_torrents.go
70 lines (64 loc) · 1.66 KB
/
search_torrents.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"flag"
"fmt"
"os"
"text/tabwriter"
"github.com/qopher/go-torrentapi"
)
// flags
var (
ranked = flag.Bool("ranked", true, "Should results be ranked")
tvdbid = flag.String("tvdb", "", "TheTVDB ID to search")
imdb = flag.String("imdb", "", "IMDb ID to search")
search = flag.String("search", "", "Search string")
sort = flag.String("sort", "seeders", "Sort order (seeders, leechers, last)")
limit = flag.Int("limit", 25, "Limit of results (25, 50, 100)")
)
func humanizeSize(s uint64) string {
size := float64(s)
switch {
case size < 1024:
return fmt.Sprintf("%d", uint64(size))
case size < 1024*1014:
return fmt.Sprintf("%.2fk", size/1024)
case size < 1024*1024*1024:
return fmt.Sprintf("%.2fM", size/1024/1024)
default:
return fmt.Sprintf("%.2fG", size/1024/1024/1024)
}
}
func main() {
flag.Parse()
if *tvdbid == "" && *search == "" && *imdb == "" {
flag.PrintDefaults()
return
}
api, err := torrentapi.New("cli")
if err != nil {
fmt.Printf("Error while starting torrentapi %s", err)
return
}
if *tvdbid != "" {
api.SearchTVDB(*tvdbid)
}
if *imdb != "" {
api.SearchIMDb(*imdb)
}
if *search != "" {
api.SearchString(*search)
}
api.Ranked(*ranked).Sort(*sort).Format("json_extended").Limit(*limit)
results, err := api.Search()
if err != nil {
fmt.Printf("Error while querying torrentapi %s", err)
return
}
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 8, 1, '\t', 0)
fmt.Fprintln(w, "File Name\tCategory\tSeeders\tLeechers\tRanked\tSize")
for _, r := range results {
fmt.Fprintf(w, "%s\t%s\t%d\t%d\t%d\t%s\n", r.Title, r.Category, r.Seeders, r.Leechers, r.Ranked, humanizeSize(r.Size))
}
w.Flush()
}