/
main.go
68 lines (59 loc) · 1.3 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
package main
import (
"fmt"
"github.com/PuerkitoBio/goquery"
"github.com/joho/godotenv"
"github.com/mikinano7/dropbox4go"
"net/http"
"os"
"strings"
"time"
)
const (
fqdn = "https://danbooru.donmai.us"
popular = fqdn + "/explore/posts/popular"
)
func main() {
godotenv.Load("go.env")
doc, err := goquery.NewDocument(popular)
if err != nil {
panic(err)
}
var arr []string
doc.Find("#a-index article").Each(func(_ int, s *goquery.Selection) {
a, _ := s.Attr("data-file-url")
arr = append(arr, a)
})
httpClient := http.DefaultClient
svc := dropbox4go.New(httpClient, os.Getenv("DB_ACCESS_TOKEN"))
now := time.Now().UTC()
for _, img := range arr {
resp, err := httpClient.Get(fqdn + img)
defer resp.Body.Close()
if err != nil {
panic(err)
}
pos := strings.LastIndex(img, "/")
fileName := img[pos+1:]
req := dropbox4go.Request{
File: resp.Body,
Parameters: dropbox4go.Parameters{
Path: fmt.Sprintf(
"/danbooru/popular/%s/%s",
now.Format("2006-01-02"),
fileName,
),
Mode: "overwrite",
AutoRename: false,
ClientModified: now.Format(time.RFC3339),
Mute: true,
},
}
a, err := svc.Upload(req)
if err != nil {
panic(err)
} else {
fmt.Println(fmt.Sprintf("file %s has uploaded.", a.Name))
}
}
}