-
Notifications
You must be signed in to change notification settings - Fork 28
/
main.go
178 lines (169 loc) · 4.13 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
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"github.com/fatih/color"
"github.com/howie6879/NIYT/common"
"github.com/howie6879/NIYT/fetcher"
"github.com/modood/table"
)
type novleDemo struct {
Index int
Name string
URL string
}
type chapterDemo struct {
Index int
Title string
Href string
}
// Token from https://github.com/amyhaber/cnki-downloader/blob/master/main.go getInputString
func getInputString() string {
buf := bufio.NewReader(os.Stdin)
s, err := buf.ReadString('\n')
if err != nil {
return ""
}
return strings.TrimSpace(s)
}
func showHelp() {
fmt.Println()
fmt.Println("**** 请先输入小说名")
fmt.Println("**** show : 显示此时资源 如书源以及最新章节")
fmt.Println("**** get : 如 get 0 ,获取此资源并显示")
fmt.Println("**** q : 返回")
fmt.Println("****")
}
func idJudge(cmdSplit []string) (uint64, bool) {
if len(cmdSplit) < 2 {
color.Red("输入格式不对")
return 0, false
}
id, err := strconv.ParseUint(cmdSplit[1], 10, 32)
if err != nil {
fmt.Fprintf(color.Output, "输入格式不对 %s\n", color.RedString(err.Error()))
return 0, false
}
return id, true
}
func main() {
color.Cyan(common.LOGO)
defer func() {
color.Yellow("下次再见^_^\n")
}()
for {
fmt.Fprintf(color.Output, "$ %s", color.CyanString("请输入小说名~ "))
name := getInputString()
if len(name) == 0 {
continue
}
if name == "q" {
break
}
if name == "help" {
showHelp()
continue
}
query := name + " 小说 最新章节"
resultData, _ := fetcher.FetchResult(query)
if len(resultData) > 0 {
var novelData []novleDemo
quickest := make(chan int, len(resultData))
for index, data := range resultData {
novelData = append(novelData, novleDemo{Index: index, Name: data.Title, URL: data.URL})
go func(index int, url string) {
quickest <- common.QuickestURL(index, url)
}(index, data.URL)
}
currentIndex := <-quickest
if currentIndex != -1 {
novelData[currentIndex].URL = novelData[currentIndex].URL + "(响应最快)"
}
table.Output(novelData)
fmt.Fprintf(color.Output, "if you don't know how to do next, just type '%s' \n", color.RedString("help"))
for {
flag := false
fmt.Fprintf(color.Output, "$ %s", color.CyanString(name+" 源 ~ "))
command := getInputString()
cmdSplit := strings.Split(command, " ")
switch strings.ToLower(cmdSplit[0]) {
case "show":
{
table.Output(novelData)
}
case "help":
{
showHelp()
}
case "get":
{
id, isOk := idJudge(cmdSplit)
if !isOk {
break
}
if id >= uint64(len(resultData)) {
color.Red("请输入正确编号")
break
}
resultData[id].FetchChapters()
if len(resultData[id].Chapters) > 0 {
var chapterData []chapterDemo
for index, data := range resultData[id].Chapters {
chapterData = append(chapterData, chapterDemo{Index: index, Title: data.ChapterName, Href: data.Href})
}
table.Output(chapterData)
for {
chapterFlag := false
fmt.Fprintf(color.Output, "$ %s", color.CyanString(name+" 章节 ~ "))
chapterCommand := getInputString()
chapterCmdSplit := strings.Split(chapterCommand, " ")
switch strings.ToLower(chapterCmdSplit[0]) {
case "show":
{
table.Output(chapterData)
}
case "help":
{
showHelp()
}
case "get":
{
chapterID, isOk := idJudge(chapterCmdSplit)
if !isOk {
break
}
if chapterID >= uint64(len(resultData[id].Chapters)) {
color.Red("请输入正确编号")
break
}
resultData[id].Chapters[chapterID].FetchContent()
fmt.Println(resultData[id].Chapters[chapterID].Content)
}
case "q":
{
chapterFlag = true
}
}
if chapterFlag {
break
}
}
}
}
case "q":
{
flag = true
}
}
if flag {
break
}
}
} else {
fmt.Println("暂无结果,请重试!")
}
}
}