-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
331 lines (269 loc) · 8.37 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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package main
import (
"encoding/json"
"fmt"
"flag"
"net/url"
"os"
"reddit-scraper/util"
"reddit-scraper/http"
"reddit-scraper/reddit"
"regexp"
"strconv"
"sync"
"time"
)
/******************************************
* *
* Global variables and structs *
* *
******************************************/
const defaultConfigFile = "conf.json"
const defaultMaxThreads = 10
const redditUrl = "http://www.reddit.com"
var config Configuration
type Configuration struct {
Subreddits []SubredditConfig `json:"subreddits"`
OutputPath string `json:"outputPath"`
MaxThreads int `json:"maxThreads"`
Stats bool `json:"stats"`
FileTypes []string `json:"fileTypes"`
}
type SubredditConfig struct {
Name string `json:"subredditName"`
Limit int `json:"numberOfPosts"`
Count int `json:"count"`
SortBy string `json:"sortBy"`
Time string `json:"time"`
After string `json:"after"`
Before string `json:"before"`
MinScore int `json:"minScore"`
SearchFor string `json:"searchFor"`
CustomFolderName string `json:"customFolderName"`
}
type ScrapeData struct {
Subreddit string `json:"subreddit"`
After string `json:"after"`
Before string `json:"before"`
}
/******************************************
* *
* Main function *
* *
******************************************/
func main() {
// Set up the program (config file and command-line flags)
config = setup()
debug := false
// Loop through subreddit list
for _, subreddit := range config.Subreddits {
fmt.Println("-----------------------------\n")
fmt.Println(subreddit.Name)
posts := []reddit.Post{}
scrapeData := ScrapeData{}
scrapeData.Subreddit = subreddit.Name
if subreddit.Limit == 0 {
subreddit.Limit = 20
}
for subreddit.Limit > 0 {
// Get subreddit JSON
listing := reddit.ListingJson{}
redditReq := createRedditJsonReq(subreddit)
fmt.Println("Requesting data from", redditReq)
// Modify limit, as reddit only returns 100 max per request
if subreddit.Limit > 100 {
subreddit.Count += 100
subreddit.Limit -= 100
} else {
subreddit.Count += subreddit.Limit
subreddit.Limit = 0
}
// Send request
headers := make(map[string]string)
headers["User-Agent"] = "Scrappit by /u/username"
http.GetJson(redditReq, &listing, headers)
// Get download links
newPosts := []reddit.Post(listing.Data.Children)
posts = append(posts, newPosts...)
subreddit.After = listing.Data.After
subreddit.Before = listing.Data.Before
if len(newPosts) < 100 {
subreddit.Limit = 0
}
}
fmt.Println(len(posts), "posts to download")
if !debug {
// Get output directory path
outputPath := config.OutputPath
if outputPath == "" {
outputPath = "output/"
}
r, _ := regexp.Compile(`.*/$`)
if !r.MatchString(outputPath) {
outputPath = outputPath + "/"
}
if subreddit.CustomFolderName != "" {
outputPath = outputPath + subreddit.CustomFolderName + "/"
} else {
outputPath = outputPath + subreddit.Name[3:] + "/"
}
err := os.MkdirAll(outputPath, 0755)
util.Check(err)
// Download to folder
var wg sync.WaitGroup
startTime := time.Now()
postsPerThread := len(posts)/config.MaxThreads
currentStart, currentEnd := 0, 0
remainder := len(posts)%config.MaxThreads
for i := 0; i < config.MaxThreads; i++ {
currentEnd = currentStart + postsPerThread
if remainder > 0 {
currentEnd++
remainder--
}
wg.Add(1)
go downloadToFolder(outputPath, posts[currentStart: currentEnd], subreddit, &wg)
currentStart = currentEnd
}
// Block and wait for go routines to complete
wg.Wait()
endTime := time.Now()
fmt.Println("Total time taken:", endTime.Sub(startTime))
}
}
}
/******************************************
* *
* Helper functions *
* *
******************************************/
/*
* Loads a configuration file to the program
* If no configuration file exists, creates a new one
*/
func configSettings(filename string) Configuration {
// Open config file
configuration := Configuration{}
file, err := os.Open(filename)
// File does not exist, create it
if os.IsNotExist(err) {
// Create file
fmt.Println("No configuration file found at", filename)
fmt.Println("Initiating new configuration file...")
file, err = os.Create(filename)
util.Check(err)
// Setup and encode the JSON
var b []byte
s1 := SubredditConfig{"/r/subreddit1", 50, 0, "new", "all", "", "", 0, "", ""}
s2 := SubredditConfig{"/r/subreddit2", 20, 0, "hot", "all", "", "", 0, "", ""}
configuration.Subreddits = append(configuration.Subreddits, s1, s2)
configuration.OutputPath = "Path/To/Output/Folder"
configuration.MaxThreads = defaultMaxThreads
b, err = json.MarshalIndent(configuration, "", " ")
util.Check(err)
// Write to the new file
_, err = file.Write(b)
util.Check(err)
// Close the fd
err = file.Close()
util.Check(err)
// Exit
fmt.Println("Please edit", filename)
os.Exit(0)
}
util.Check(err)
// Parse JSON
err = json.NewDecoder(file).Decode(&configuration)
util.Check(err)
return configuration
}
/*
* Creates the JSON request URL for Reddit given a configuration
*/
func createRedditJsonReq(subreddit SubredditConfig) string {
// Base URL
redditReq, err := url.Parse(redditUrl + subreddit.Name)
util.Check(err)
// Search vs Sort
if subreddit.SearchFor != "" {
redditReq.Path = redditReq.Path + "/search"
} else if subreddit.SortBy != "" {
redditReq.Path = redditReq.Path + "/" + subreddit.SortBy
}
// End URL
redditReq.Path = redditReq.Path + "/.json"
// Query parameters
values := url.Values{}
// Limiting
values.Set("limit", "20")
if subreddit.Limit > 100 {
values.Set("limit", "100")
} else if subreddit.Limit != 0 {
values.Set("limit", strconv.Itoa(subreddit.Limit))
}
// Pagination
values.Set("count", strconv.Itoa(subreddit.Count))
if subreddit.After != "" {
values.Set("after", subreddit.After)
}
// Searching
if subreddit.SearchFor != "" {
values.Set("q", subreddit.SearchFor)
values.Set("restrict_sr", "on")
values.Set("sort", "relevance")
}
values.Set("t", "all")
if subreddit.Time != "" {
values.Set("t", subreddit.Time)
}
redditReq.RawQuery = values.Encode()
return redditReq.String()
}
/*
* Downloads a file from a URL to the given folder
* Go routine thread function
* Outputs success messages to main function
*/
func downloadToFolder(folder string, posts []reddit.Post, config SubredditConfig, wg *sync.WaitGroup) {
fmt.Println("Go routine to download", len(posts), "posts")
defer wg.Done()
for _, post := range posts {
// Get the post data
downloadPost := reddit.GetDownloadPost(post)
if downloadPost.Score < config.MinScore && config.MinScore > 0 {
continue
}
// Determine output location
outputFile := folder + downloadPost.Title + downloadPost.FileType
// Download the file
err := http.DownloadFile(outputFile, downloadPost.Url, nil)
util.CheckWarn(err)
fmt.Println("Downloaded:", downloadPost.Title, "\n\tID:", downloadPost.Id, "\tScore:", downloadPost.Score)
}
}
/*
* Set up function to be run when program is loading
* Handles command-line flags and configurations file
* Prints help statements
*/
func setup() Configuration {
fmt.Println("Scraper v0.1")
fmt.Println("Created by Curtis Li")
// Command-line flags
configFile := flag.String("c", defaultConfigFile, "Path to configuration file")
maxThreads := flag.Int("t", 0, "Maximum number of concurrent downloads")
getHelp := flag.Bool("h", false, "Help")
flag.Parse()
if *getHelp {
flag.PrintDefaults()
os.Exit(0)
}
// Get configuration settings
config = configSettings(*configFile)
if *maxThreads > 0 {
config.MaxThreads = *maxThreads
} else if config.MaxThreads <= 0 {
config.MaxThreads = defaultMaxThreads
}
return config
}