-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmopds.go
268 lines (255 loc) · 8.78 KB
/
mopds.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/mak-alex/mopds/models"
"github.com/mak-alex/mopds/modules/books"
"github.com/mak-alex/mopds/modules/datastore"
"github.com/mak-alex/mopds/modules/new_rest"
"github.com/mak-alex/mopds/utils"
"github.com/namsral/flag"
)
func findINPX(catalog string) []string {
inpx_files, err := filepath.Glob(filepath.Join(catalog, "*.inpx"))
if err != nil {
log.Fatal(err)
}
return inpx_files
}
func setDatabaseConfig(Catalog string, username string, password string, host string, dbname string, dbtype string, dblog bool, sslmode string) *models.DBConfig {
result := new(models.DBConfig)
result.DBType = dbtype
result.DBLog = dblog
if dbtype == "sqlite3" {
fileData, err := ioutil.ReadFile(filepath.Join(Catalog, "mopds_db.sqlite"))
if err == nil {
err = json.Unmarshal(fileData, result)
}
if err != nil { // fallback to sqlite
result.DBParams = filepath.Join(Catalog, "mopds_db.sqlite")
}
} else if dbtype == "postgres" {
if sslmode == "" {
sslmode = "disable"
}
result.DBParams = fmt.Sprintf("user=%s password=%s DB.name=%s sslmode=%s", username, password, dbname, sslmode)
}
return result
}
func main() {
curDir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
var (
config string
Catalog string
Page int
PerPage int
GetAuthor uint
GetBook uint
GetGenre uint
GetSerie uint
GetAuthors bool
GetBooks bool
GetGenres bool
GetSeries bool
GetBooksByAuthor uint
GetBooksByGenre uint
GetBooksBySerie uint
SearchAuthor string
SearchGenre string
SearchSerie string
Random bool
Stat bool
Listen string
Scan bool
Parse bool
Save bool
SearchLibID string
SearchBookByTitle string
Verbose bool
Debug bool
About bool
Host string
Username string
Password string
DBName string
DBType string
DBLog bool
SSLMode string
)
flag.StringVar(&config, "config", "./conf/mopds.conf", "Default configuration file")
flag.StringVar(&Catalog, "catalog", "", "Directory of library (mandatory)")
flag.IntVar(&Page, "page", 0, "Pagination 1...n")
flag.IntVar(&PerPage, "per_page", 25, "Limit results (-1 for no limit)")
flag.UintVar(&GetAuthor, "get_author", 0, "Get author by id")
flag.UintVar(&GetBook, "get_book", 0, "Get book by id")
flag.UintVar(&GetGenre, "get_genre", 0, "Get genre by id")
flag.UintVar(&GetSerie, "get_serie", 0, "Get serie by id")
flag.BoolVar(&GetAuthors, "get_authors", false, "List all authors")
flag.BoolVar(&GetBooks, "get_books", false, "List all books")
flag.BoolVar(&GetGenres, "get_genres", false, "List all genres")
flag.BoolVar(&GetSeries, "get_series", false, "List all series")
flag.UintVar(&GetBooksByAuthor, "get_books_by_author", 0, "List all author's books by id")
flag.UintVar(&GetBooksByGenre, "get_books_by_genre", 0, "List all genre's books by id")
flag.UintVar(&GetBooksBySerie, "get_books_by_serie", 0, "List all serie's books by id")
flag.StringVar(&SearchAuthor, "search_author", "", "Search authors, or books by author if comes with search-title")
flag.StringVar(&SearchGenre, "search_genre", "", "Search genre by genre name or section, or subsection")
flag.StringVar(&SearchSerie, "search_serie", "", "Search serie by serie name")
flag.BoolVar(&Random, "random", false, "Random for -get_books/-get_books_by_genre/-get_books_by_author/-get_books_by_serie)")
flag.BoolVar(&Stat, "stat", false, "Book library statistics")
flag.StringVar(&Listen, "listen", ":8000", "Set server listen address:port")
flag.BoolVar(&Scan, "scan", false, "Start scanning books")
flag.BoolVar(&Parse, "inpx", true, "Parse inpx to the local database. If not used, mopds continue work to zip archives and *.fb2 files")
flag.BoolVar(&Save, "save", false, "Save book file to the disk (ex.: --get_book 1 --save)")
flag.StringVar(&SearchLibID, "search_lib_id", "", "Search book(s) by its libId")
flag.StringVar(&SearchBookByTitle, "search_book", "", "Search books by their title")
flag.BoolVar(&Verbose, "verbose", false, "Verbose output")
flag.BoolVar(&Debug, "debug", false, "Debug output")
flag.BoolVar(&About, "about", false, "About author and this project")
flag.StringVar(&Host, "host", "modps", "IP address for connect to database")
flag.StringVar(&Username, "username", "mopds", "Username for connect to database")
flag.StringVar(&Password, "password", "mopds", "Password for connect to database")
flag.StringVar(&DBType, "dbtype", "sqlite3", "Type used database: sqlite3, mysql or postgres")
flag.StringVar(&DBName, "database", "", "Database name connect to database")
flag.StringVar(&SSLMode, "sslmode", "", "Whether to use ssl mode or not, here's the question: disable or enable")
flag.Parse()
DBLog = Debug
conf := setDatabaseConfig(Catalog, Username, Password, Host, DBName, DBType, DBLog, SSLMode)
store, err := datastore.NewDBStore(conf)
if err != nil {
log.Fatalln("Failed to open database")
}
defer store.Close()
if Scan {
log.Printf("Opening %s to parse data\n", Catalog)
go func() {
books.ProcessALL(Catalog, Parse, store)
}()
rest.NewRestService(Listen, store, Catalog).StartListen()
} else if SearchLibID != "" {
result, err := store.GetBooksByLibID(SearchLibID, true, Page, PerPage)
if err == nil {
utils.PrintJson(result, true)
} else {
log.Println("Nothing found")
}
} else if SearchBookByTitle != "" {
result, err := store.GetBooksBySerie(SearchBookByTitle, SearchAuthor, true, Page, PerPage)
if err == nil {
utils.PrintJson(result, true)
} else {
log.Println("Nothing found")
}
} else if SearchAuthor != "" {
result, err := store.GetAuthors(SearchAuthor, Page, PerPage)
if err == nil {
utils.PrintJson(result, true)
} else {
log.Println("Nothing found")
}
} else if GetAuthors {
result, err := store.GetAuthors("", Page, PerPage)
if err == nil {
utils.PrintJson(result, true)
} else {
log.Println("Nothing found")
}
} else if GetBooks {
result, err := store.GetBooks("", true, Random, Page, PerPage)
if err == nil {
utils.PrintJson(result, true)
} else {
log.Println("Nothing found")
}
} else if GetGenres {
result, err := store.GetGenres("", Page, PerPage) // фильтр добавить
if err == nil {
utils.PrintJson(result, true)
} else {
log.Println("Nothing found")
}
} else if GetSeries {
result, err := store.GetSeries("", Page, PerPage) // фильтр добавить
if err == nil {
utils.PrintJson(result, true)
} else {
log.Println("Nothing found")
}
} else if GetBooksByAuthor != 0 {
result, err := store.GetBooksByAuthorID(GetBooksByAuthor, true, Random, Page, PerPage, models.Search{})
if err == nil {
utils.PrintJson(result, true)
} else {
log.Println("Nothing found")
}
} else if GetBooksByGenre != 0 {
result, err := store.GetBooksByGenreID(GetBooksByGenre, true, Random, Page, PerPage, models.Search{})
if err == nil {
utils.PrintJson(result, true)
} else {
log.Println("Nothing found")
}
} else if GetBooksBySerie != 0 {
result, err := store.GetBooksBySerieID(GetBooksBySerie, true, Random, Page, PerPage, models.Search{})
if err == nil {
utils.PrintJson(result, true)
} else {
log.Println("Nothing found")
}
} else if GetAuthor != 0 {
result, err := store.GetAuthor(GetAuthor)
if err == nil {
utils.PrintJson(result, true)
} else {
log.Println("Nothing found")
}
} else if GetBook != 0 {
result, err := store.GetBook(GetBook)
if err == nil {
utils.PrintJson(result, true)
if Save {
err = books.UnzipBookFile(Catalog, result, curDir, true)
if err != nil {
log.Fatalln("Failed to save file", err)
}
}
} else {
log.Println("Nothing found")
}
} else if GetGenre != 0 {
result, err := store.GetGenre(GetGenre)
if err == nil {
utils.PrintJson(result, true)
} else {
log.Println("Nothing found")
}
} else if GetSerie != 0 {
result, err := store.GetSerie(GetSerie)
if err == nil {
utils.PrintJson(result, true)
} else {
log.Println("Nothing found")
}
} else if Stat {
result, err := store.GetSummary()
if err == nil {
utils.PrintJson(result, true)
} else {
log.Println("Nothing found")
}
} else if About {
devinfo := models.DevInfo{}
devinfo.Author = "Alexandr Mikhailenko a.k.a Alex M.A.K."
devinfo.Email = "[email protected]"
devinfo.Project.Name = "mOPDS"
devinfo.Project.Version = "0.1.0"
devinfo.Project.Link = "github.com/mak-alex/mopds"
devinfo.Project.Created = "24.03.18 22:59"
utils.PrintJson(devinfo, true)
} else {
rest.NewRestService(Listen, store, Catalog).StartListen()
}
}