/
mirrors.go
68 lines (50 loc) · 1.48 KB
/
mirrors.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
package main
import (
"bytes"
"encoding/json"
"log"
"strings"
"github.com/olekukonko/tablewriter"
"gitlab.com/haath/goirate/pkg/torrents"
)
// MirrorsCommand defines the mirrors command and holds its options.
type MirrorsCommand struct {
SourceURL string `short:"s" long:"source" description:"Link to a list of PirateBay proxies. Default: proxybay.github.io"`
}
// Execute is the callback of the mirrors command.
func (m *MirrorsCommand) Execute(args []string) error {
var scraper torrents.MirrorScraper
scraper.SetProxySourceURL(m.SourceURL)
mirrors, err := scraper.GetMirrors()
if err != nil {
return err
}
if Options.JSON {
mirrorsJSON, err := json.MarshalIndent(mirrors, "", " ")
if err != nil {
return err
}
log.Println(string(mirrorsJSON))
} else {
log.Printf(getMirrorsTable(mirrors))
}
return nil
}
func getMirrorsTable(mirrors []torrents.Mirror) string {
buf := bytes.NewBufferString("")
table := tablewriter.NewWriter(buf)
table.SetHeader([]string{" ", "Country", "URL"})
table.SetColumnAlignment([]int{tablewriter.ALIGN_DEFAULT, tablewriter.ALIGN_CENTER, tablewriter.ALIGN_DEFAULT})
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
table.SetAutoFormatHeaders(false)
for _, mirror := range mirrors {
status := "x"
if !mirror.Status {
status = " "
}
table.Append([]string{status, strings.ToUpper(mirror.Country), mirror.URL})
}
table.Render()
return buf.String()
}