This repository has been archived by the owner on Sep 19, 2019. It is now read-only.
/
main.go
159 lines (139 loc) · 2.92 KB
/
main.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/codegangsta/cli"
"github.com/naoina/toml"
"github.com/zaibon/tget/api/t411"
)
type config struct {
Torrent struct {
OutputDir string
Language string
}
T411 struct {
Token string
}
Betaseries struct {
Login string
Password string
APIKey string
}
}
func loadConfig() (config config) {
home := os.Getenv("HOME")
f, err := os.Open(filepath.Join(home, ".tget.toml"))
if err != nil {
if os.IsNotExist(err) {
fmt.Printf("Please create configuration file at %s", f.Name())
os.Exit(1)
} else {
log.Fatal(err)
}
}
if err := toml.NewDecoder(f).Decode(&config); err != nil {
log.Fatal(err)
}
return
}
func main() {
app := cli.NewApp()
app.Name = "autot411"
var (
title string
season int
episode int
t411Auth t411.AuthPair
auto = false
)
app.Commands = []cli.Command{
{
Name: "t411-auth",
Usage: "get t411 token",
Flags: []cli.Flag{
cli.StringFlag{
Name: "u,username",
Usage: "t411 username",
Destination: &t411Auth.Username,
},
cli.StringFlag{
Name: "p, password",
Usage: "t411 password",
Destination: &t411Auth.Password,
},
},
Action: func(c *cli.Context) {
if t411Auth.Username == "" || t411Auth.Password == "" {
fmt.Println("Missing arguments")
return
}
var err error
cfg := config{}
home := os.Getenv("HOME")
path := filepath.Join(home, ".tget.toml")
data, err := ioutil.ReadFile(path)
if err == nil {
if err = toml.Unmarshal(data, &cfg); err != nil {
log.Fatal(err)
}
}
cfg.T411.Token, err = t411.Auth(t411Auth)
if err != nil {
log.Fatal(err)
}
if data, err = toml.Marshal(cfg); err != nil {
log.Fatal(err)
}
if err = ioutil.WriteFile(path, data, 0660); err != nil {
log.Fatal(err)
}
log.Printf("config file updated (%s)", path)
},
},
{
Name: "download",
Usage: "download a show",
Flags: []cli.Flag{
cli.StringFlag{
Name: "show, t",
Usage: "Title of the show to dowload",
Destination: &title,
},
cli.IntFlag{
Name: "season, s",
Usage: "Season number",
Destination: &season,
},
cli.IntFlag{
Name: "episode, e",
Usage: "episode number",
Destination: &episode,
},
cli.BoolFlag{
Name: "auto",
Usage: "automaticly download the torrent with more seeders. don't ask",
Destination: &auto,
},
},
Action: func(c *cli.Context) {
if title == "" {
fmt.Println("Please specify a title")
return
}
if season == 0 && episode == 0 {
downloadShow(title)
return
}
if season != 0 && episode == 0 {
downloadSaison(title, season)
return
}
downloadEpisode(title, season, episode)
},
},
}
app.Run(os.Args)
}