-
Notifications
You must be signed in to change notification settings - Fork 3
/
ptt.go
118 lines (98 loc) · 2.45 KB
/
ptt.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
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 *PTT, count int) {
for i := 0; i < count; i++ {
title := p.GetPostTitleByIndex(i)
likeCount := p.GetPostStarByIndex(i)
fmt.Printf("%d:[%d★]%s\n", i, likeCount, 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))
ptt := NewPTT()
usr, _ := user.Current()
ptt.BaseDir = fmt.Sprintf("%v/Pictures/iloveptt", usr.HomeDir)
var workerNum int
rootCmd := &cobra.Command{
Use: "iloveptt",
Short: "Download all the images in given post url",
Run: func(cmd *cobra.Command, args []string) {
page := 0
pagePostCoubt := 0
pagePostCoubt = ptt.ParsePttPageByIndex(page)
printPageResult(ptt, pagePostCoubt)
scanner := bufio.NewScanner(os.Stdin)
quit := false
for !quit {
fmt.Print("ptt:> ")
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 = ptt.ParsePttPageByIndex(page)
printPageResult(ptt, pagePostCoubt)
case "p":
if page > 0 {
page = page - 1
}
pagePostCoubt = ptt.ParsePttPageByIndex(page)
printPageResult(ptt, pagePostCoubt)
case "s":
page = 0
pagePostCoubt = ptt.ParsePttPageByIndex(page)
printPageResult(ptt, pagePostCoubt)
case "o":
open.Run(filepath.FromSlash(ptt.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 := ptt.GetPostUrlByIndex(index)
if int(index) >= len(url) {
fmt.Println("Invalid index")
continue
}
if ptt.HasValidURL(url) {
ptt.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()
}