-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
238 lines (191 loc) · 7.18 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
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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"regexp"
"runtime"
"strconv"
"strings"
"sync"
)
var (
numWorkers = flag.Uint("workerCount", 10, "Number of concurrent workers")
allCores = flag.Bool("allCores", false, "Set to true if you want to use all processor cores for this.")
startingPage = flag.Int("page", 1, "Default is to start from first page, set it to some other page if you'd like to continue where you left of.")
continueWork = flag.Bool("continue", false, "Set to true if you want to continue from where you left of (last saved page number).")
fastMode = flag.Bool("fast", false, "Set this to true if you want to risk some books not persisting if you violently stop the program.")
)
const (
// Once we get to a page that contains this sentence, then we know we've hit the end
endReachedSentence = "No Posts Found."
bookLimitPerPage = 10
// This is where the books will be stored
baseDirPath = "allitebooks"
lastPageNumberFile = "lastpagenumber.txt"
defaultCategoryName = "Random"
)
var (
// Provide page urls to this channel in order for them to get scanned for the target (download) links
bookPages chan string
workPool chan Work
slowModeWG sync.WaitGroup
workWG sync.WaitGroup
)
var (
// This will provide us with individual book page links (not the actual download link this time,
// but the actual download link for a single book will be on that page).
rgxBookPage = regexp.MustCompile(`<h2 class="entry-title"><a href="(.*)" rel="bookmark"`)
// This will provide us with the actual download link
rgxBookDownloadLink = regexp.MustCompile(`<a href="(http://file.allitebooks.com/.*.pdf)" target="_blank">`)
// This will provide us with the category for the book we found
rgxBookCategoryFinder = regexp.MustCompile(`<dt>Category:</dt>.*>(.*)</a></dd>`)
)
func main() {
flag.Parse()
if *allCores {
log.Print("Using all cores! /flex")
runtime.GOMAXPROCS(runtime.NumCPU())
}
if *continueWork {
data, err := ioutil.ReadFile(lastPageNumberFile)
if err == nil {
lastPageNumber, err := strconv.Atoi(string(data))
if err != nil {
log.Printf("Invalid content in %s: %s", lastPageNumberFile, data)
os.Exit(1)
}
// If we've made it this far, override the startingPage param
*startingPage = lastPageNumber
}
}
if _, err := os.Stat(baseDirPath); os.IsNotExist(err) {
if err := os.Mkdir(baseDirPath, os.ModePerm); err != nil {
log.Printf("Error creating base directory: %s", err)
os.Exit(1)
}
}
workPool = InitializeWorkers(int(*numWorkers), bookLimitPerPage)
bookPages = make(chan string, bookLimitPerPage)
go processBookPages()
baseURL := "http://www.allitebooks.com/page/"
// Infinite loop to get through all the pages; stop only on errors or if previously defined page end content is encountered
for currentPageNumber := *startingPage; ; currentPageNumber++ {
currentURL := fmt.Sprintf("%s%d", baseURL, currentPageNumber)
response, err := http.Get(currentURL)
if err != nil {
//Shouldn't really continue if we get an error here
log.Printf("Error fetching the main page: %s", err)
os.Exit(1)
}
log.Printf("\n\n")
log.Printf("----------------------")
log.Printf("Currently at %s", currentURL)
log.Printf("----------------------\n\n")
sourceBytes, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Printf("Error reading the source code from the main page: %s", err)
continue
}
response.Body.Close()
sourceCode := string(sourceBytes)
if strings.Contains(sourceCode, endReachedSentence) {
log.Printf("The website reports that this page (%s) doesn't contain any books.", currentURL)
break
}
if !*fastMode {
// Before we proceed (and only in slow mode), let's update the lastPageNumber file
err = ioutil.WriteFile(lastPageNumberFile, []byte(strconv.Itoa(currentPageNumber)), os.ModePerm)
if err != nil {
log.Printf("Error writing to %s: %s", lastPageNumberFile, err)
break
}
}
// Alright, it seems like we might have some books here!
match := rgxBookPage.FindAllSubmatch(sourceBytes, -1)
individualBookPages := make([]string, 0, 10)
for i := 0; i < 10 && i < len(match); i++ {
if len(match[i]) > 0 {
individualBookPages = append(individualBookPages, string(match[i][1]))
}
}
if len(individualBookPages) < 1 {
log.Printf("Couldn't find any books on this page: (%s)", currentURL)
break
}
// Cool! We've made it this far, which means we've found some book pages.
if !*fastMode {
// First, let's allow only 1 page to be downloaded at time, to ensure corectness if a program is stopped at some point
// and then rerun with --continue flag
slowModeWG.Add(len(individualBookPages))
}
// Increment this counter, since we want to wait for all the book to finish downloading before the program exits.
workWG.Add(len(individualBookPages))
// Let's send them off to a bookPage channel and process them from there, but continue
// fetching more book pages here as well (unless we're processing 10 books at this very moment)
for _, bookPage := range individualBookPages {
bookPages <- bookPage
}
if !*fastMode {
log.Printf("Waiting for page [%d] to finish downloading all the books...", currentPageNumber)
slowModeWG.Wait()
}
}
log.Print("Waiting for all the book to finish downloading.")
workWG.Wait()
log.Print("All done! Enjoy and don't be lazy. Actually read some of them. :)")
}
func processBookPages() {
for {
bookPage, ok := <-bookPages
if !ok {
return
}
//Process them further untill we get the download link (which is our goal here)
response, err := http.Get(bookPage)
if err != nil {
//Shouldn't really continue if we get an error here
log.Printf("Error fetching a book page %s - %s", bookPage, err)
continue
}
sourceBytes, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Printf("Error reading the source code from the book page: %s", err)
continue
}
response.Body.Close()
match := rgxBookDownloadLink.FindSubmatch(sourceBytes)
if len(match) == 0 {
continue
}
// Bingo! We've got a book download URL. Let's download/process it in another place, and continue with what we're doing here.
downloadURL := string(match[1])
// Wait! Before that, let's also discover the category so we can place it in the corresponding directory.
category := defaultCategoryName
categoryMatch := rgxBookCategoryFinder.FindSubmatch(sourceBytes)
if len(categoryMatch) != 0 {
category = strings.Replace(string(categoryMatch[1]), "&", "&", -1)
}
// Create the directory structure for a book to be stored. For example: allitebooks/databases/some-book-file.pdf
bookDirPath := path.Join(baseDirPath, category)
if _, err := os.Stat(bookDirPath); os.IsNotExist(err) {
if err := os.Mkdir(bookDirPath, os.ModePerm); err != nil {
log.Printf("Error creating book (sub)directory: %s", err)
os.Exit(1)
}
}
lastSlashIndex := strings.LastIndex(downloadURL, "/")
bookFilePath := path.Join(bookDirPath, downloadURL[lastSlashIndex:])
workPool <- Work{URL: downloadURL, Filename: bookFilePath, SlowModeSync: getSlowModeWaitGroup(), WorkSync: &workWG}
}
}
func getSlowModeWaitGroup() *sync.WaitGroup {
if *fastMode {
return nil
}
return &slowModeWG
}