forked from torta/dark-dmzj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dark_dmzj.go
188 lines (171 loc) · 4.57 KB
/
dark_dmzj.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
package main
import (
"crypto/tls"
"encoding/json"
"fmt"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/tidwall/gjson"
"gopkg.in/cheggaaa/pb.v1"
"io/ioutil"
"math/rand"
"net/http"
"os"
"os/signal"
"sort"
"syscall"
"time"
)
var client *http.Client
var bar *pb.ProgressBar
var isDownloading bool
type dmzjBook struct {
ID uint64 `json:"id"`
Title string `json:"title"`
IsLong int64 `json:"islong"`
Authors []string `json:"authors"`
Types []string `json:"types"`
Status []string `json:"status"`
Cover string `json:"cover"`
LastUpdateChapterName string `json:"last_update_chapter_name"`
LastUpdateChapterID uint64 `json:"last_update_chapter_id"`
LastUpdateTime int64 `json:"last_updatetime"`
}
func init() {
client = &http.Client{
Transport: &http.Transport{
MaxIdleConnsPerHost: 256,
MaxIdleConns: 256,
ResponseHeaderTimeout: 10 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
},
Timeout: time.Second * 10,
}
}
func apiWithRetry(id int, try int) string {
for i := 0; i < try; i++ {
domains := []string{"v2.api.dmzj.com", "v3api.dmzj.com"}
res, err := client.Get(fmt.Sprintf("http://%s/comic/%d.json", domains[rand.Intn(2)], id))
resCk, errCk := client.Head(fmt.Sprintf("https://m.dmzj.com/info/%d.html", id))
if err == nil {
defer res.Body.Close()
}
if errCk == nil {
defer resCk.Body.Close()
}
if err == nil && res.StatusCode == 200 {
body, _ := ioutil.ReadAll(res.Body)
if errCk == nil && resCk.StatusCode == 200 && resCk.ContentLength > 0 {
if resCk.ContentLength > 1024 {
return ""
}
return string(body)
}
}
}
return ""
}
func getItem(id int, c chan<- string) {
c <- apiWithRetry(id, 5)
bar.Increment()
}
func arrayMap(vs []string, f func(string) []string) [][]string {
vsm := make([][]string, len(vs))
for i, v := range vs {
vsm[i] = f(v)
}
return vsm
}
func downloadBooks() {
if isDownloading {
return
}
isDownloading = true
defer func() { isDownloading = false }()
MaxRoutines := 50
MaxBooks := 60000
bar = pb.New(MaxBooks - 1).Prefix("Updating ")
bar.SetWidth(60)
bar.ShowTimeLeft = true
bar.ShowCounters = false
bar.ShowSpeed = true
bar.Start()
defer bar.FinishPrint("Finish!")
c := make(chan string, MaxBooks)
jobs := make(chan int, MaxBooks)
for i := 0; i < MaxRoutines; i++ {
go func() {
for e := range jobs {
getItem(e, c)
}
}()
}
for i := 1; i < MaxBooks; i++ {
jobs <- i
}
items := []dmzjBook{}
for p := 1; p < MaxBooks; p++ {
dat := gjson.Parse(<-c)
if dat.Get("id").Exists() {
tags := arrayMap([]string{"authors", "types", "status"}, func(v string) []string {
tag := []string{}
for _, e := range dat.Get(v).Array() {
tag = append(tag, e.Get("tag_name").String())
}
return tag
})
items = append(items, dmzjBook{
ID: dat.Get("id").Uint(),
Title: dat.Get("title").String(),
IsLong: dat.Get("islong").Int(),
Authors: tags[0],
Types: tags[1],
Status: tags[2],
Cover: dat.Get("cover").String(),
LastUpdateChapterName: dat.Get("chapters.0.data.0.chapter_title").String(),
LastUpdateChapterID: dat.Get("chapters.0.data.0.chapter_id").Uint(),
LastUpdateTime: dat.Get("last_updatetime").Int(),
})
}
}
sort.Slice(items, func(a, b int) bool {
return items[a].LastUpdateTime > items[b].LastUpdateTime
})
jsonDat, _ := json.Marshal(items)
ioutil.WriteFile("public/data.json", jsonDat, 0644)
}
func main() {
go func() {
time.Sleep(1 * time.Second)
downloadBooks()
}()
func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGUSR1)
go func() {
for {
<-c
downloadBooks()
}
}()
}()
e := echo.New()
e.HideBanner = true
e.Static("/", "public")
e.Use(middleware.GzipWithConfig(middleware.GzipConfig{
Level: 5,
}))
e.GET("/webpic/*", func(c echo.Context) error {
req, _ := http.NewRequest("GET", fmt.Sprintf("http://images.dmzj.com/%s", c.Request().URL.Path), nil)
req.Header.Set("Referer", "https://m.dmzj.com/")
res, err := client.Do(req)
if err != nil {
return c.NoContent(http.StatusBadGateway)
}
defer res.Body.Close()
data, _ := ioutil.ReadAll(res.Body)
return c.Blob(res.StatusCode, res.Header.Get("Content-Type"), data)
})
e.Logger.Fatal(e.Start(":7777"))
}