/
iloveck101.go
117 lines (97 loc) · 2.38 KB
/
iloveck101.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
package main
import (
"bufio"
"fmt"
"log"
"os"
"os/user"
"path/filepath"
"strconv"
"strings"
"github.com/skratchdot/open-golang/open"
"github.com/spf13/cobra"
. "github.com/kkdai/photomgr"
)
func printPageResult(p *CK101, count int) {
for i := 0; i < count; i++ {
title := p.GetPostTitleByIndex(i)
fmt.Printf("%d: %s\n", i, title)
}
fmt.Printf("(o: open file in fider, s: top page, n:next, p:prev, quit: quit program)\n")
}
type NullWriter int
func (NullWriter) Write([]byte) (int, error) { return 0, nil }
func main() {
log.SetOutput(new(NullWriter))
c := NewCK101()
usr, _ := user.Current()
c.BaseDir = fmt.Sprintf("%v/Pictures/iloveCK101", usr.HomeDir)
var workerNum int
rootCmd := &cobra.Command{
Use: "iloveCK101",
Short: "Download all the images in given post url",
Run: func(cmd *cobra.Command, args []string) {
page := 0
pagePostCoubt := 0
pagePostCoubt = c.ParseCK101PageByIndex(page)
printPageResult(c, pagePostCoubt)
scanner := bufio.NewScanner(os.Stdin)
quit := false
for !quit {
fmt.Print("CK101:> ")
if !scanner.Scan() {
break
}
line := scanner.Text()
parts := strings.Split(line, " ")
cmd := parts[0]
args := parts[1:]
switch cmd {
case "quit":
quit = true
case "n":
page = page + 1
pagePostCoubt = c.ParseCK101PageByIndex(page)
printPageResult(c, pagePostCoubt)
case "p":
if page > 0 {
page = page - 1
}
pagePostCoubt = c.ParseCK101PageByIndex(page)
printPageResult(c, pagePostCoubt)
case "s":
page = 0
pagePostCoubt = c.ParseCK101PageByIndex(page)
printPageResult(c, pagePostCoubt)
case "o":
open.Run(filepath.FromSlash(c.BaseDir))
case "d":
if len(args) == 0 {
fmt.Println("You don't input any article index. Input as 'd 1'")
continue
}
index, err := strconv.Atoi(args[0])
if err != nil {
fmt.Println(err)
continue
}
url := c.GetPostUrlByIndex(index)
if int(index) >= len(url) {
fmt.Println("Invalid index")
continue
}
if c.HasValidURL(url) {
c.Crawler(url, 25)
fmt.Println("Done!")
} else {
fmt.Println("Unsupport url:", url)
}
default:
fmt.Println("Unrecognized command:", cmd, args)
}
}
},
}
rootCmd.Flags().IntVarP(&workerNum, "worker", "w", 25, "Number of workers")
rootCmd.Execute()
}