forked from AlexiaAshford/pineapple-backups
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
208 lines (196 loc) · 7 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"fmt"
"github.com/VeronicaAlexia/BoluobaoAPI/boluobao"
BoluobaoConfig "github.com/VeronicaAlexia/BoluobaoAPI/pkg/config"
HbookerAPI "github.com/VeronicaAlexia/HbookerAPI/ciweimao/book"
HbookerConfig "github.com/VeronicaAlexia/HbookerAPI/pkg/config"
"github.com/VeronicaAlexia/pineapple-backups/config"
"github.com/VeronicaAlexia/pineapple-backups/pkg/file"
"github.com/VeronicaAlexia/pineapple-backups/pkg/threading"
"github.com/VeronicaAlexia/pineapple-backups/pkg/tools"
"github.com/VeronicaAlexia/pineapple-backups/src"
"github.com/urfave/cli"
"log"
"os"
"strings"
)
func init() {
if !config.Exist("./config.json") || file.SizeFile("./config.json") == 0 {
fmt.Println("config.json not exist, create a new one!")
} else {
config.LoadJson()
}
config.UpdateConfig()
InitApp := cli.NewApp()
InitApp.Name = "pineapple-backups"
InitApp.Version = "V.1.6.9"
InitApp.Usage = "https://github.com/VeronicaAlexia/pineapple-backups"
InitApp.Flags = config.Args
HbookerConfig.AppConfig.AppVersion = config.Apps.Hbooker.AppVersion
HbookerConfig.AppConfig.Account = config.Apps.Hbooker.Account
HbookerConfig.AppConfig.LoginToken = config.Apps.Hbooker.LoginToken
HbookerConfig.AppConfig.DeviceToken = config.Apps.Hbooker.DeviceToken
BoluobaoConfig.AppConfig.App = false // set boluobao app mode
BoluobaoConfig.AppConfig.AppKey = "FMLxgOdsfxmN!Dt4"
BoluobaoConfig.AppConfig.DeviceId = "240a90cc-4c40-32c7-b44e-d4cf9e670605"
BoluobaoConfig.AppConfig.Cookie = config.Apps.Sfacg.Cookie
InitApp.Action = func(c *cli.Context) {
fmt.Println("you can input -h and --help to see the command list.")
if config.Command.AppType != "cat" && config.Command.AppType != "sfacg" {
fmt.Println(config.Command.AppType, "app type error")
os.Exit(1)
}
}
if err := InitApp.Run(os.Args); err != nil {
log.Fatal(err)
}
config.Vars.ThreadNum = config.Command.MaxThread
config.Vars.AppType = config.Command.AppType
//config.Vars.Epub = config.Command.Epub
fmt.Println("current app type:", config.Vars.AppType)
}
func current_download_book_function(book_id string) {
catalogue := src.SettingBooks(book_id) // get book catalogues
if !catalogue.Test {
fmt.Println(catalogue.BookMessage)
os.Exit(1)
}
DownloadList := catalogue.GetDownloadsList()
if DownloadList != nil && len(DownloadList) > 0 {
thread := threading.NewGoLimit(uint(config.Vars.MaxRetry))
fmt.Println(len(DownloadList), " chapters will be downloaded.")
catalogue.ChapterBar = src.New(len(DownloadList))
catalogue.ChapterBar.Describe("working...")
//fmt.Println(DownloadList)
for _, chapterID := range DownloadList {
thread.Add()
go catalogue.DownloadContent(thread, chapterID)
}
thread.WaitZero()
fmt.Printf("\nNovel:%v download complete!\n", config.Current.NewBooks["novel_name"])
} else {
fmt.Println(config.Current.NewBooks["novel_name"] + " No chapter need to download!\n")
}
catalogue.MergeTextAndEpubFiles()
}
func update_local_booklist() {
if config.Exist("./bookList.txt") {
for _, i := range strings.ReplaceAll(file.Open("./bookList.json", "", "r"), "\n", "") {
if !strings.Contains(string(i), "#") {
current_download_book_function(string(i))
}
}
} else {
fmt.Println("bookList.txt not exist, create a new one!")
}
}
func shell(inputs []string) {
switch inputs[0] { // switch command
case "up", "update":
update_local_booklist()
case "a", "app":
if tools.TestList([]string{"sfacg", "cat"}, inputs[1]) {
config.Vars.AppType = inputs[1]
} else {
fmt.Println("app type error, please input again.")
}
case "d", "b", "book", "download":
if len(inputs) == 2 {
if book_id := config.FindID(inputs[1]); book_id != "" {
current_download_book_function(book_id)
} else {
fmt.Println("book id is empty, please input again.")
}
} else {
fmt.Println("input book id or url, like:download <bookid/url>")
}
case "s", "search":
if len(inputs) == 2 && inputs[1] != "" {
s := src.Search{Keyword: inputs[1], Page: 0}
current_download_book_function(s.SearchBook())
} else {
fmt.Println("input search keyword, like:search <keyword>")
}
case "l", "login":
if config.Vars.AppType == "cat" && len(inputs) >= 3 {
//hbooker.GET_LOGIN_TOKEN(inputs[1], inputs[2])
fmt.Println("hbooker login function is not available now.")
} else if config.Vars.AppType == "sfacg" && len(inputs) >= 3 {
src.LoginAccount(inputs[1], inputs[2], 0)
} else {
fmt.Println("you must input account and password, like: -login account password")
}
case "t", "token":
if ok := src.InputAccountToken(); !ok {
fmt.Println("you must input account and token.")
}
default:
fmt.Println("command not found,please input help to see the command list:", inputs[0])
}
}
func shell_run_console_and_bookshelf() {
bookshelf_book_index, book_shelf_bookcase := src.InitBookShelf() // init bookshelf information
for {
if comment := tools.GET(">"); comment != nil {
if tools.TestIntList(bookshelf_book_index, comment[0]) {
current_download_book_function(book_shelf_bookcase[tools.StrToInt(comment[0])]["novel_id"])
} else if comment[0] == "load" || comment[0] == "bookshelf" {
bookshelf_book_index, book_shelf_bookcase = src.InitBookShelf() // load bookshelf information
} else if comment[0] == "q" || comment[0] == "quit" {
shell(comment)
} else {
fmt.Println("input book index to download book")
fmt.Println("input load to reload bookshelf")
fmt.Println("input quit to exit bookshelf")
}
}
}
}
func main() {
if len(os.Args) > 1 {
if config.Command.Account != "" && config.Command.Password != "" {
shell([]string{"login", config.Command.Account, config.Command.Password})
} else if config.Command.Login {
src.TestAppTypeAndAccount()
} else if config.Command.BookId != "" {
current_download_book_function(config.Command.BookId)
} else if config.Command.SearchKey != "" {
s := src.Search{Keyword: config.Command.SearchKey, Page: 0}
current_download_book_function(s.SearchBook())
} else if config.Command.Update {
update_local_booklist()
} else if config.Command.Token {
src.InputAccountToken()
} else {
shell_run_console_and_bookshelf()
}
} else {
for _, message := range config.HelpMessage {
fmt.Println("[info]", message)
}
if config.Vars.AppType == "cat" {
// recommend list for hbooker app
//if book_id := recommend.NEW_RECOMMEND().CHANGE_NEW_RECOMMEND(); book_id != "" {
// current_download_book_function(book_id)
//}
response := HbookerAPI.GET_BOOK_INFORMATION("100280239")
//fmt.Println(response)
if response.Code == "100000" {
shell_run_console_and_bookshelf()
} else {
fmt.Println("hbooker error:", response.Tip)
}
} else if config.Vars.AppType == "sfacg" {
boluobao.API.User.UserInformation()
shell_run_console_and_bookshelf()
//if accounts.Data.AccountID > 0 {
// Tasks := task.Task{AccountId: strconv.Itoa(accounts.Data.AccountID)}
// Tasks.NovelCompleteTas()
// shell_run_console_and_bookshelf()
//} else {
// fmt.Println("you need to login to use the automatic sign-in and task function")
//}
}
}
}