-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.go
249 lines (237 loc) · 6.24 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package main
import (
"drivedlgo/db"
"drivedlgo/drive"
"drivedlgo/utils"
"errors"
"fmt"
"log"
"net/url"
"os"
"path"
"regexp"
"github.com/urfave/cli"
)
const DRIVE_LINK_REGEX string = `https://drive\.google\.com/(drive)?/?u?/?\d?/?(mobile)?/?(file)?(folders)?/?d?/([-\w]+)[?+]?/?(w+)?`
func getFileIdByLink(link string) string {
match := regexp.MustCompile(DRIVE_LINK_REGEX)
matches := match.FindStringSubmatch(link)
if len(matches) >= 2 {
return matches[len(matches)-2]
}
urlParsed, err := url.Parse(link)
if err != nil {
return ""
}
values := urlParsed.Query()
if len(values) == 0 {
return ""
}
for i, j := range values {
if i == "id" {
return j[0]
}
}
return ""
}
func downloadCallback(c *cli.Context) error {
arg := c.Args().Get(0)
if arg == "" {
return errors.New(fmt.Sprintf("Required argument <fileid/link> is missing. \nUsage: %s\nFor more info: %s --help ", c.App.UsageText, os.Args[0]))
}
fileId := getFileIdByLink(arg)
if fileId == "" {
fileId = arg
}
fmt.Printf("Detected File-Id: %s\n", fileId)
GD := drive.NewDriveClient()
GD.Init()
GD.Authorize(c.String("db-path"), c.Bool("usesa"), c.Int("port"))
GD.SetConcurrency(c.Int("conn"))
GD.SetAbusiveFileDownload(c.Bool("acknowledge-abuse"))
cus_path, err := db.GetDLDirDb(c.String("db-path"))
if err == nil {
if c.String("path") == "." {
path.Join(cus_path, c.String("path"))
} else {
cus_path = c.String("path")
}
} else {
cus_path = c.String("path")
}
log.SetOutput(GD.Progress)
GD.Download(fileId, cus_path, c.String("output"))
return nil
}
func setCredsCallback(c *cli.Context) error {
arg := c.Args().Get(0)
if arg == "" {
return errors.New("Provide a proper credentials.json file path.")
}
fmt.Printf("Detected credentials.json Path: %s\n", arg)
if !db.IsCredentialsInDb(c.String("db-path")) {
if db.IsTokenInDb(c.String("db-path")) {
db.RemoveTokenDb(c.String("db-path"))
}
db.AddCredentialsDb(c.String("db-path"), arg)
fmt.Printf("%s added in database.\n", arg)
} else {
fmt.Println("A credentials file already exists in databse, use rm command to remove it first.")
}
return nil
}
func rmCredsCallback(c *cli.Context) error {
if db.IsCredentialsInDb(c.String("db-path")) {
db.RemoveCredentialsDb(c.String("db-path"))
db.RemoveTokenDb(c.String("db-path"))
fmt.Println("credentials removed from database successfully.")
} else {
fmt.Println("Database doesnt contain any credentials.")
}
return nil
}
func setJWTConfigCallback(c *cli.Context) error {
arg := c.Args().Get(0)
if arg == "" {
return errors.New("Provide a proper service account file path.")
}
fmt.Printf("Detected service account Path: %s\n", arg)
if !db.IsJWTConfigInDb(c.GlobalString("db-path")) {
db.AddJWTConfigDb(c.GlobalString("db-path"), arg)
fmt.Printf("%s added in database.\n", arg)
} else {
fmt.Println("A service account already exists in databse, use rmsa command to remove it first.")
}
return nil
}
func rmJWTConfigCallback(c *cli.Context) error {
if db.IsJWTConfigInDb(c.GlobalString("db-path")) {
db.RemoveJWTConfigDb(c.GlobalString("db-path"))
fmt.Println("service account removed from database successfully.")
} else {
fmt.Println("Database doesnt contain any service account.")
}
return nil
}
func setDLDirCallback(c *cli.Context) error {
arg := c.Args().Get(0)
if arg == "" {
return errors.New("Provide a proper download directory path.")
}
fmt.Printf("Detected download directory path: %s\n", arg)
_, err := db.GetDLDirDb(c.String("db-path"))
if err == nil {
db.RemoveDLDirDb(c.String("db-path"))
}
_, err = db.AddDLDirDb(c.String("db-path"), arg)
return err
}
func rmDLDirCallback(c *cli.Context) error {
_, err := db.GetDLDirDb(c.String("db-path"))
if err != nil {
fmt.Println("DB doesnt contain default directory path, try --help.")
} else {
_, err = db.RemoveDLDirDb(c.String("db-path"))
if err == nil {
fmt.Println("Default directory removed successfully, now application will download in current working directory.")
} else {
fmt.Println("Error while removing default directory: ", err.Error())
}
}
return nil
}
func main() {
dlFlags := []cli.Flag{
&cli.StringFlag{
Name: "path",
Usage: "Folder path to store the download.",
Value: ".",
},
&cli.StringFlag{
Name: "output",
Usage: "File/folder name of the download.",
},
&cli.StringFlag{
Name: "db-path",
Usage: "File path to store the database.",
Value: utils.GetDefaultDbPath(),
},
&cli.IntFlag{
Name: "conn",
Usage: "Number of Concurrent File Downloads.",
Value: 2,
},
&cli.BoolFlag{
Name: "acknowledge-abuse",
Usage: "Enable downloading of files marked as abusive by google drive.",
},
&cli.BoolFlag{
Name: "usesa",
Usage: "Use service accounts instead of OAuth.",
},
&cli.IntFlag{
Name: "port",
Usage: "Port for the OAuth web server.",
Value: 8096,
},
}
subCommandFlags := []cli.Flag{
&cli.StringFlag{
Name: "db-path",
Usage: "File path to store the database.",
Value: utils.GetDefaultDbPath(),
},
}
app := cli.NewApp()
app.Name = "Google Drive Downloader"
app.Usage = "A minimal Google Drive Downloader written in Go."
app.UsageText = fmt.Sprintf("%s [global options] [arguments...]", os.Args[0])
app.Authors = []cli.Author{
{Name: "JaskaranSM"},
}
app.Action = downloadCallback
app.Flags = dlFlags
app.Commands = []cli.Command{
{
Name: "set",
Usage: "add credentials.json file to database",
Action: setCredsCallback,
Flags: subCommandFlags,
},
{
Name: "rm",
Usage: "remove credentials from database",
Action: rmCredsCallback,
Flags: subCommandFlags,
},
{
Name: "setsa",
Usage: "add service account to database",
Action: setJWTConfigCallback,
Flags: subCommandFlags,
},
{
Name: "rmsa",
Usage: "remove service account from database",
Action: rmJWTConfigCallback,
Flags: subCommandFlags,
},
{
Name: "setdldir",
Usage: "set default download directory",
Action: setDLDirCallback,
Flags: subCommandFlags,
},
{
Name: "rmdldir",
Usage: "remove default download directory and set the application to download in current folder.",
Action: rmDLDirCallback,
Flags: subCommandFlags,
},
}
app.Version = "1.6"
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}