-
Notifications
You must be signed in to change notification settings - Fork 1
/
hikikomori.go
256 lines (237 loc) · 8.15 KB
/
hikikomori.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
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/ghodss/yaml"
"github.com/nstratos/go-myanimelist/mal"
"github.com/pierrre/mangadownloader"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"io/ioutil"
"net/url"
"os"
"os/exec"
"sort"
"strconv"
"strings"
)
type Mangas struct {
Mangaseries map[string]Detail `json:"mangaseries"`
}
type Detail struct {
Name string `json:"name"`
Dir string `json:"dir, omitempty"`
Url string `json:"url, omitempty"`
RawUrl string `json:"rawurl, omitempty"`
Reading string `json:"reading, omitempty"`
Lastchapterdownloaded int `json:"lastchapterdownloaded"`
Lastchapterread int `json:"lastchapterread"`
}
type byName []os.FileInfo
func (f byName) Len() int { return len(f) }
func (f byName) Less(i, j int) bool {
chapter1 := strings.Split(f[i].Name(), ".")
chapter2 := strings.Split(f[j].Name(), ".")
return chapter1[0] < chapter2[0]
}
func (f byName) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
func main() {
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
viper.SetConfigName("config") // name of config file (without extension)
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
file2, err := ioutil.ReadFile(viper.GetString("yaml"))
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("Fatal error mangas file: %s \n", err))
}
mangas := &Mangas{}
file, _ := yaml.YAMLToJSON(file2)
json.Unmarshal(file, &mangas)
var syncMalCmd = &cobra.Command{
Use: "syncMal",
Short: "Sync MyAnimelist manga list with config file",
Long: `Sync MyAnimelist manga list to config file in Yaml.
It will make the difference between what alreay there.`,
Run: func(cmd *cobra.Command, args []string) {
c := mal.NewClient()
c.SetCredentials(viper.GetString("myanimelist.login"), viper.GetString("myanimelist.password"))
c.SetUserAgent(viper.GetString("myanimelist.apikey"))
list, _, _ := c.Manga.List("Noriak")
for _, manga := range list.Manga {
if manga.MyStatus == 1 {
mangaName := manga.SeriesTitle
mangaId := manga.SeriesMangaDBID
mangaIdString := strconv.Itoa(mangaId)
malLastChapter := manga.MyReadChapters
lastChapterRead := malLastChapter
_, ok := mangas.Mangaseries[mangaIdString]
mangaYaml := &Detail{}
mangaYaml.Name = mangaName
mangaYaml.Dir = mangaName
mangaYaml.Lastchapterdownloaded = 0
mangaYaml.Url = ""
mangaYaml.RawUrl = ""
mangaYaml.Reading = ""
if ok {
yamlLastChapterDownloaded := mangas.Mangaseries[mangaIdString].Lastchapterdownloaded
mangaYaml.Lastchapterdownloaded = yamlLastChapterDownloaded
yamlUrl := mangas.Mangaseries[mangaIdString].Url
mangaYaml.Url = yamlUrl
yamlRawUrl := mangas.Mangaseries[mangaIdString].RawUrl
mangaYaml.RawUrl = yamlRawUrl
yamlReading := mangas.Mangaseries[mangaIdString].Reading
mangaYaml.Reading = yamlReading
yamlDir := mangas.Mangaseries[mangaIdString].Dir
if yamlDir != "" {
mangaYaml.Dir = yamlDir
}
yamlLastChapterRead := mangas.Mangaseries[mangaIdString].Lastchapterread
if yamlLastChapterRead > malLastChapter {
//update mal
lastChapterRead = yamlLastChapterRead
_, err := c.Manga.Update(mangaId, mal.MangaEntry{Status: "reading", Chapter: lastChapterRead})
if err != nil {
fmt.Println("Error update chapter read to myanimelist")
} else {
fmt.Println(mangaName + " updated")
}
}
}
mangaYaml.Lastchapterread = lastChapterRead
mangas.Mangaseries[mangaIdString] = *mangaYaml
}
}
//Savetofile
b, _ := json.Marshal(mangas)
y, _ := yaml.JSONToYAML(b)
ioutil.WriteFile(viper.GetString("yaml"), y, 0777)
},
}
var downloadCmd = &cobra.Command{
Use: "download",
Short: "Download new chapters",
Long: `Try to see if there is new chapters and download them.
It will possible to read them after.`,
Run: func(cmd *cobra.Command, args []string) {
// Var for mangadownloader
//outFlag := flag.String("out", "", "Output directory")
cbzFlag := flag.Bool("cbz", true, "CBZ")
pageDigitCountFlag := flag.Int("pagedigitcount", 4, "Page digit count")
httpRetryFlag := flag.Int("httpretry", 10, "Http retry")
parallelChapterFlag := flag.Int("parallelchapter", 2, "Parallel chapter")
parallelPageFlag := flag.Int("parallelpage", 4, "Parallel page")
flag.Parse()
//out := *outFlag
options := &mangadownloader.Options{
Cbz: *cbzFlag,
PageDigitCount: *pageDigitCountFlag,
ParallelChapter: *parallelChapterFlag,
ParallelPage: *parallelPageFlag,
}
md := mangadownloader.CreateDefaultMangeDownloader()
md.HttpRetry = *httpRetryFlag
for key, value := range mangas.Mangaseries {
mangaName := value.Name
mangaDir := value.Dir
lastChapterDownloaded := value.Lastchapterdownloaded
urlManga := value.Url
if urlManga != "" {
fmt.Println("Downloading " + mangaName)
u, err := url.Parse(urlManga)
if err != nil {
panic(err)
}
o, err := md.Identify(u)
if err != nil {
panic(err)
}
dirOut := viper.GetString("downloadDir") + mangaDir + "/"
_ = md.DownloadManga(o.(*mangadownloader.Manga), "./mangas/", options)
files, _ := ioutil.ReadDir(dirOut)
var cbzFiles []os.FileInfo
for _, f := range files {
if strings.Contains(f.Name(), ".cbz") {
cbzFiles = append(cbzFiles, f)
}
}
sort.Sort(byName(cbzFiles))
newChapterCbz := ""
for _, f := range cbzFiles {
newChapterCbz = f.Name()
}
newChapter := strings.Split(newChapterCbz, ".")
newChapterInt, _ := strconv.Atoi(newChapter[0])
if newChapterInt > lastChapterDownloaded {
lastChapterDownloaded = newChapterInt
}
value.Lastchapterdownloaded = lastChapterDownloaded
mangas.Mangaseries[key] = value
//Savetofile
b, _ := json.Marshal(mangas)
y, _ := yaml.JSONToYAML(b)
ioutil.WriteFile(viper.GetString("yaml"), y, 0777)
fmt.Println("Finish downloading " + mangaName)
}
if value.RawUrl != "" && value.Reading != "" {
cmd := exec.Command("python2", "download_raw.py", value.RawUrl, value.Reading, viper.GetString("downloadDir"))
if err := cmd.Start(); err != nil {
panic(err)
}
}
}
},
}
var readCmd = &cobra.Command{
Use: "read",
Short: "Read new downloaded chapter",
Long: `Read new downloaded Mangas.
It will also permit to save the new state of reading.`,
Run: func(cmd *cobra.Command, args []string) {
id := 1
var listMangas []string
for key, value := range mangas.Mangaseries {
mangaName := value.Name
lastChapterDownloaded := value.Lastchapterdownloaded
lastChapterRead := value.Lastchapterread
if lastChapterDownloaded != lastChapterRead {
fmt.Printf("%3d | %3d/%3d | %s\n", id, lastChapterRead, lastChapterDownloaded, mangaName)
listMangas = append(listMangas, key)
id = id + 1
}
}
fmt.Print("Enter id of Manga to read: ")
var input int
fmt.Scanln(&input)
idMangaToRead := listMangas[input-1]
manga := mangas.Mangaseries[idMangaToRead]
mangaDir := manga.Dir
lastChapter := manga.Lastchapterread
lastChapter = lastChapter + 1
lastChapterStr := fmt.Sprintf("%03d", lastChapter)
linkChapter := viper.GetString("downloadDir") + mangaDir + "/" + lastChapterStr + ".cbz"
//Exec Mcomix
cmdMcomix := exec.Command("mcomix", "-w", linkChapter)
if err := cmdMcomix.Start(); err != nil {
panic(err)
}
fmt.Print("Enter last chapter read: ")
var newLastChapter int
fmt.Scanln(&newLastChapter)
manga.Lastchapterread = newLastChapter
mangas.Mangaseries[idMangaToRead] = manga
//Savetofile
b, _ := json.Marshal(mangas)
y, _ := yaml.JSONToYAML(b)
ioutil.WriteFile(viper.GetString("yaml"), y, 0777)
},
}
var rootCmd = &cobra.Command{Use: "hikikomori"}
rootCmd.AddCommand(syncMalCmd)
rootCmd.AddCommand(downloadCmd)
rootCmd.AddCommand(readCmd)
rootCmd.Execute()
}