forked from metafates/mangal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
232 lines (182 loc) · 4.87 KB
/
config.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
package main
import (
"errors"
"fmt"
"github.com/BurntSushi/toml"
"os"
"path/filepath"
"strings"
)
type FormatType string
const (
PDF FormatType = "pdf"
CBZ FormatType = "cbz"
Zip FormatType = "zip"
Plain FormatType = "plain"
Epub FormatType = "epub"
)
type UI struct {
Fullscreen bool
Prompt string
Title string
Placeholder string
Mark string
}
type Config struct {
Scrapers []*Scraper
Format FormatType
UI UI
UseCustomReader bool
CustomReader string
Path string
CacheImages bool
}
type _tempConfig struct {
Use []string
Format string
UI UI `toml:"ui"`
UseCustomReader bool `toml:"use_custom_reader"`
CustomReader string `toml:"custom_reader"`
Path string `toml:"download_path"`
CacheImages bool `toml:"cache_images"`
Sources map[string]Source
}
func GetConfigPath() (string, error) {
configDir, err := os.UserConfigDir()
if err != nil {
return "", err
}
return filepath.Join(configDir, strings.ToLower(AppName), "config.toml"), nil
}
func DefaultConfig() *Config {
conf, _ := ParseConfig(string(DefaultConfigBytes))
return conf
}
var UserConfig *Config
var DefaultConfigBytes = []byte(`# Which sources to use. You can use several sources, it won't affect perfomance'
use = ['manganelo']
# Available options: pdf, epub, cbz, zip, plain (just images)
format = "pdf"
# If false, then OS default pdf reader will be used
use_custom_reader = false
custom_reader = "zathura"
# Custom download path, can be either relative (to the current directory) or absolute
download_path = '.'
# Add images to cache
# If set to true mangal could crash when trying to redownload something really quickly
# Usually happens on slow machines
cache_images = false
[ui]
# Fullscreen mode
fullscreen = true
# Input prompt icon
prompt = ">"
# Input placeholder
placeholder = "What shall we look for?"
# Selected chapter mark
mark = "▼"
# Search window title
title = "Mangal"
[sources]
[sources.manganelo]
# Base url
base = 'https://ww5.manganelo.tv'
# Search endpoint. Put %s where the query should be
search = 'https://ww5.manganelo.tv/search/%s'
# Selector of entry anchor (<a></a>) on search page
manga_anchor = '.search-story-item a.item-title'
# Selector of entry title on search page
manga_title = '.search-story-item a.item-title'
# Manga chapters anchors selector
chapter_anchor = 'li.a-h a.chapter-name'
# Manga chapters titles selector
chapter_title = 'li.a-h a.chapter-name'
# Reader page images selector
reader_page = '.container-chapter-reader img'
# Random delay between requests
random_delay_ms = 500 # ms
# Are chapters listed in reversed order on that source?
# reversed order -> from newest chapter to oldest
reversed_chapters_order = true
`)
// GetConfig from given path. If path is empty string default config path is used
func GetConfig(path string) *Config {
var (
configPath string
err error
)
if path == "" {
configPath, err = GetConfigPath()
} else {
configPath = path
}
if err != nil {
return DefaultConfig()
}
configExists, err := Afero.Exists(configPath)
if err != nil || !configExists {
return DefaultConfig()
}
contents, err := Afero.ReadFile(configPath)
if err != nil {
return DefaultConfig()
}
config, err := ParseConfig(string(contents))
if err != nil {
return DefaultConfig()
}
return config
}
func ParseConfig(configString string) (*Config, error) {
var (
tempConf _tempConfig
conf Config
)
_, err := toml.Decode(configString, &tempConf)
if err != nil {
return nil, err
}
conf.CacheImages = tempConf.CacheImages
// Convert sources to scrapers
for sourceName, source := range tempConf.Sources {
if !Contains[string](tempConf.Use, sourceName) {
continue
}
source.Name = sourceName
scraper := MakeSourceScraper(source)
if !conf.CacheImages {
scraper.FilesCollector.CacheDir = ""
}
conf.Scrapers = append(conf.Scrapers, scraper)
}
conf.UI = tempConf.UI
conf.Path = tempConf.Path
// Default format is pdf
conf.Format = IfElse(tempConf.Format == "", PDF, FormatType(tempConf.Format))
conf.UseCustomReader = tempConf.UseCustomReader
conf.CustomReader = tempConf.CustomReader
return &conf, err
}
func ValidateConfig(config *Config) error {
if config.UseCustomReader && config.CustomReader == "" {
return errors.New("use_custom_reader is set to true but reader isn't specified")
}
if !Contains(AvailableFormats, config.Format) {
msg := fmt.Sprintf(
`unknown format '%s'
type %s to show available formats`,
string(config.Format),
accentStyle.Render(strings.ToLower(AppName)+" formats"),
)
return errors.New(msg)
}
for _, scraper := range config.Scrapers {
if scraper.Source == nil {
return errors.New("internal error: scraper source is nil")
}
if err := ValidateSource(scraper.Source); err != nil {
return err
}
}
return nil
}