-
Notifications
You must be signed in to change notification settings - Fork 0
/
putio.go
70 lines (64 loc) · 1.89 KB
/
putio.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 (
"context"
"fmt"
"log"
"strings"
"github.com/putdotio/go-putio"
"github.com/sherifabdlnaby/gpool"
"golang.org/x/oauth2"
)
func ListToplevelFolders(client *putio.Client) (files []putio.File, directories []string, err error) {
files, _, err = client.Files.List(context.Background(), 0)
if err != nil {
return nil, nil, err
}
for _, file := range files {
if file.IsDir() {
directories = append(directories, file.Name)
}
}
return files, directories, nil
}
func GetPutioClient(token string) *putio.Client {
tokenSource := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
oauthClient := oauth2.NewClient(context.Background(), tokenSource)
return putio.NewClient(oauthClient)
}
func TraversePutioFolder(root putio.File, parents []string, client *putio.Client, pool *gpool.Pool, resultsChan chan<- string, localFolder string) error {
if !root.IsDir() {
return nil
}
parents = append(parents, root.Name)
files, _, err := client.Files.List(context.Background(), root.ID)
if err != nil {
return err
}
for _, file := range files {
if file.IsDir() {
TraversePutioFolder(file, parents, client, pool, resultsChan, localFolder)
} else {
processFile(file, parents, client, pool, resultsChan, localFolder)
}
}
return nil
}
func processFile(file putio.File, parents []string, client *putio.Client, pool *gpool.Pool, resultsChan chan<- string, localFolder string) {
// TODO: make path handling OS independent
destinationDir := fmt.Sprintf("%s/%s", localFolder, strings.Join(parents, "/"))
downloadUrl, err := client.Files.URL(context.Background(), file.ID, true)
if err != nil {
fmt.Printf("Cannot get download url for %s\n", file.Name)
} else {
job := func() {
DownloadFile(downloadUrl, destinationDir)
resultsChan <- file.Name
}
err := pool.Enqueue(context.Background(), job)
if err != nil {
log.Fatal("Error queueing", err)
}
}
}