This repository has been archived by the owner on Apr 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
datawrite.go
96 lines (86 loc) · 2.64 KB
/
datawrite.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"encoding/csv" // csv encoding support
"fmt"
"os"
"strconv"
"github.com/jroimartin/gocui"
)
func torrentdatawrite(g *gocui.Gui) error {
// Try to open specified file
datafile, err := os.Create(datafilename)
if err != nil {
errorui = err
g.SetManagerFunc(errorfunc)
}
defer datafile.Close()
waitforme.Wait() // Wait for gentrackers to fetch trackers from trackerlist
switch datatype {
case 2:
// Write CSV with torrent details(with magnet with trackers) to the file
csvw := csv.NewWriter(datafile)
csvw.Write([]string{"Id", "Name", "Size", "Seeds", "Leeches", "Infohash", "Magnet"})
for cidno, ceacht := range torrents {
csvw.Write(
[]string{
strconv.Itoa(cidno),
ceacht.Text,
fmt.Sprintf("%f", ceacht.Length),
strconv.Itoa(ceacht.Seeds),
strconv.Itoa(ceacht.Leechs),
ceacht.Id,
magnetheader + ceacht.Id + trackerurl,
})
}
csvw.Flush() // Flushing is necessary when csv.Writer.Write() is used
if csverr := csvw.Error(); err != nil {
errorui = csverr
g.SetManagerFunc(errorfunc)
}
case 3:
// Write Magnet Links to the file. That's it
for _, ceacht := range torrents {
if _, fprinterr := fmt.Fprintln(datafile, magnetheader+ceacht.Id+trackerurl); err != nil {
errorui = fprinterr
g.SetManagerFunc(errorfunc)
}
}
case 4:
// Write magnet link of selected torrent
if _, fprinterr := fmt.Fprintln(datafile, magnetheader+torrents[selid].Id+trackerurl); err != nil {
errorui = fprinterr
g.SetManagerFunc(errorfunc)
}
case 5:
// Write Torrent details of selected torrent
if _, fprinterr := fmt.Fprint(datafile, "Name: "+torrents[selid].Text, "\nSize: ", torrents[selid].Length, "\nSeeds: ", torrents[selid].Seeds, "\nLeechs: ", torrents[selid].Leechs, "\nInfohash: ", torrents[selid].Id, "\n\nMagnet: \n\n", magnetheader+torrents[selid].Id+trackerurl); err != nil {
errorui = fprinterr
g.SetManagerFunc(errorfunc)
}
default:
// Write CSV data of torrent to the file
csvw := csv.NewWriter(datafile)
csvw.Write([]string{"Id", "Name", "Size", "Seeds", "Leeches", "Infohash", "Magnet"})
for cidno, ceacht := range torrents {
csvw.Write(
[]string{
strconv.Itoa(cidno),
ceacht.Text,
fmt.Sprintf("%f", ceacht.Length),
strconv.Itoa(ceacht.Seeds),
strconv.Itoa(ceacht.Leechs),
ceacht.Id,
magnetheader + ceacht.Id,
})
}
csvw.Flush() // Flushing is necessary when csv.Writer.Write() is used
if csverr := csvw.Error(); err != nil {
errorui = csverr
g.SetManagerFunc(errorfunc)
}
}
g.Close()
fmt.Println("Data has been written to file ", datafilename)
os.Exit(1)
return gocui.ErrQuit
}