-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrymscrape.go
More file actions
424 lines (369 loc) · 10.9 KB
/
rymscrape.go
File metadata and controls
424 lines (369 loc) · 10.9 KB
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"sort"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/mozillazg/go-slugify"
"github.com/uber-go/zap"
"gopkg.in/go-playground/pool.v3"
)
// rymscrape is the core of the project where:
// workers = number of concurrent goroutines.
// timeout = per request based timeout for http connections.
// timestamp = the timestamp at which procedure was started.
type rymscrape struct {
workers uint
timeout uint
timestamp string
reportFolder string
myclients []myclient
jseed JSeed
}
// postProcess removes duplicate lines in the debug file
func (rym *rymscrape) postProcess() {
logger.Info("Starting post processing")
defer logger.Info("Finished post processing")
removeDuplicatesUnordered := func(elements []string) []string {
encountered := map[string]bool{}
// Create a map of all unique elements.
for v := range elements {
encountered[elements[v]] = true
}
// Place all keys from the map into a slice.
result := []string{}
for key := range encountered {
result = append(result, key)
}
return result
}
data := readFileIntoList(rym.reportFolder + "/debug")
data = removeDuplicatesUnordered(data)
err := os.Remove(rym.reportFolder + "/debug")
if err != nil {
panic(err)
}
writeToFile(rym.reportFolder+"/debug", strings.Join(data, "\n"))
}
// storeClients stores clients into myclient struct from ./myclients folder
func (dm *rymscrape) storeClients() {
myclientsDir, err := ioutil.ReadDir("./myclients")
if err != nil {
if strings.Contains(err.Error(), "The system cannot find the file specified") {
logger.Info("No client files detected, progressing without clients...")
return
} else {
panic(err)
}
}
for _, f := range myclientsDir {
if f.IsDir() {
continue
}
sClient := myclient{}
sClient.fileName = strings.TrimSuffix(f.Name(), ".txt")
sClient.data = readFileIntoList("./myclients/" + f.Name())
sort.Strings(sClient.data)
dm.myclients = append(dm.myclients, sClient)
}
logger.Info("Loaded clients", zap.Int("dm.myclients len", len(dm.myclients)))
}
// start starts the process of collecting links
func (rym *rymscrape) start() {
var (
fullLinkList []string
episodeLinkList []string
)
fullLinkList = rym.getFullList()
if len(fullLinkList) <= 0 {
logger.Error("Something wrong with fetching a complete list of brand links")
return
}
if rym.jseed.EpisodeListAcquire.IsTrue {
np := pool.NewLimited(rym.workers)
npBatch := np.Batch()
for _, brandLink := range fullLinkList {
npBatch.Queue(rym.workerGetEpisodeList(brandLink))
}
npBatch.QueueComplete()
for work := range npBatch.Results() {
if err := work.Error(); err != nil {
logger.Error(err.Error())
continue
}
res := work.Value().([]string)
for _, el := range res {
if stringInSlice(el, episodeLinkList) {
continue
}
episodeLinkList = append(episodeLinkList, el)
}
}
np.Close()
} else {
episodeLinkList = fullLinkList
}
if rym.jseed.VideoListAcquire.IsTrue {
npf := pool.NewLimited(rym.workers)
npfBatch := npf.Batch()
for _, episodeLink := range episodeLinkList {
npfBatch.Queue(rym.workerGetVideoList(episodeLink))
}
npfBatch.QueueComplete()
for work := range npfBatch.Results() {
if err := work.Error(); err != nil {
logger.Error(err.Error())
continue
}
result := work.Value().([]reportStructure)
var data []string
for _, report := range result {
data = append(data, fmt.Sprintf(
"%s\t%s\t%s\t%s",
report.siteUrl,
report.pageTitle,
report.licensor,
report.cyberlockerLink,
))
}
writeToFile(rym.reportFolder+"/debug", strings.Join(data, "\n"))
}
npf.Close()
}
}
// workerGetEpisodeList is a helper pool function for concurrent routines using "gopkg.in/go-playground/pool.v3" package
func (rym *rymscrape) workerGetEpisodeList(brandLink string) pool.WorkFunc {
return func(wu pool.WorkUnit) (interface{}, error) {
if wu.IsCancelled() {
// return values not used
return nil, nil
}
links, err := rym.getEpisodeList(brandLink)
if err != nil {
return nil, err
}
return links, nil
}
}
// workerGetVideoList is a helper pool function for concurrent routines using "gopkg.in/go-playground/pool.v3" package
func (rym *rymscrape) workerGetVideoList(episodeList string) pool.WorkFunc {
return func(wu pool.WorkUnit) (interface{}, error) {
if wu.IsCancelled() {
// return values not used
return nil, nil
}
reports, err := rym.getVideoList(episodeList)
if err != nil {
return nil, err
}
return reports, nil
}
}
// getFullList parses through the jseed file and operates based on the commands given to fetch
// the target full brand page links.
func (rym *rymscrape) getFullList() (fullListLinks []string) {
if len(rym.jseed.FullListLinks) <= 0 {
logger.Debug("rym.jseed.FullListLinks <= 0")
return
}
for _, fullLink := range rym.jseed.FullListLinks {
logger.Debug("Discovered fullLink entity", zap.String("fullLink", fullLink))
fullLink = rym.jseed.SiteProtocol + "://" + rym.jseed.SiteLink + "/" + fullLink
doc, err := rym.getGoqueryDocument(fullLink)
if err != nil {
logger.Error(err.Error())
continue
}
links, err := processSeedBlock(
doc,
rym.jseed.FullListAcquire.LookFor,
rym.jseed.FullListAcquire.Under,
rym.jseed.FullListAcquire.Res,
rym.jseed.SiteProtocol,
rym.jseed.SiteLink,
)
if err != nil {
logger.Error(err.Error())
continue
}
for _, link := range links {
if stringInSlice(link, fullListLinks) {
continue
}
fullListLinks = append(fullListLinks, link)
}
}
return fullListLinks
}
// getEpisodeList parses through the jseed file and operates based on the commands given to fetch
// the target full brand episode links from the brand page links provided.
func (rym *rymscrape) getEpisodeList(brandLink string) (episodeLinks []string, err error) {
doc, err := rym.getGoqueryDocument(brandLink)
if err != nil {
return []string{}, err
}
episodeLinks, err = processSeedBlock(
doc,
rym.jseed.EpisodeListAcquire.LookFor,
rym.jseed.EpisodeListAcquire.Under,
rym.jseed.EpisodeListAcquire.Res,
rym.jseed.SiteProtocol,
rym.jseed.SiteLink,
)
if err != nil {
return []string{}, err
}
return episodeLinks, nil
}
// getVideoList parses through the jseed file and operates based on the commands given to fetch
// the video links from the episode link provided.
func (rym *rymscrape) getVideoList(episodeLink string) (reports []reportStructure, err error) {
// get page
pageGoqueryDocument, err := rym.getGoqueryDocument(episodeLink)
if err != nil {
return []reportStructure{}, err
}
// get title
pageTitle := pageGoqueryDocument.Find("title").First().Text()
// get licensor
licensor := rym.findLicensor(pageTitle)
switch rym.jseed.VideoListAcquire.Paginate.IsTrue {
case true:
var paginatedLinks []string
p, err := processSeedBlock(
pageGoqueryDocument,
rym.jseed.VideoListAcquire.Paginate.LookFor,
rym.jseed.VideoListAcquire.Paginate.Under,
rym.jseed.VideoListAcquire.Paginate.Res,
rym.jseed.SiteProtocol,
rym.jseed.SiteLink,
)
if err != nil {
return []reportStructure{}, err
}
for _, link := range p {
if stringInSlice(link, paginatedLinks) {
continue
}
paginatedLinks = append(paginatedLinks, link)
}
for _, plink := range paginatedLinks {
doc, err := rym.getGoqueryDocument(plink)
if err != nil {
return []reportStructure{}, err
}
v, err := processSeedBlock(
doc,
rym.jseed.VideoListAcquire.LookFor,
rym.jseed.VideoListAcquire.Under,
rym.jseed.VideoListAcquire.Res,
rym.jseed.SiteProtocol,
rym.jseed.SiteLink,
)
if err != nil {
return []reportStructure{}, err
}
for _, link := range v {
reports = append(reports, reportStructure{
siteUrl: episodeLink,
licensor: licensor,
cyberlockerLink: link,
pageTitle: pageTitle,
})
}
}
case false:
videoLinks, err := processSeedBlock(
pageGoqueryDocument,
rym.jseed.VideoListAcquire.LookFor,
rym.jseed.VideoListAcquire.Under,
rym.jseed.VideoListAcquire.Res,
rym.jseed.SiteProtocol,
rym.jseed.SiteLink,
)
if err != nil {
return []reportStructure{}, err
}
for _, link := range videoLinks {
reports = append(reports, reportStructure{
siteUrl: episodeLink,
licensor: licensor,
cyberlockerLink: link,
pageTitle: pageTitle,
})
}
}
// check deep required
if rym.jseed.VideoListAcquire.GoDeeper.IsTrue {
var newReports []reportStructure
switch rym.jseed.VideoListAcquire.GoDeeper.ByPattern.IsTrue {
case true:
for _, report := range reports {
deepLink, err := rym.getDeepVideoLinkByPattern(report.cyberlockerLink,
rym.jseed.VideoListAcquire.GoDeeper.ByPattern.PatternStart,
rym.jseed.VideoListAcquire.GoDeeper.ByPattern.PatternEnd,
)
if err != nil {
logger.Error("Error getting deep link", zap.String("Error", err.Error()))
} else {
report.cyberlockerLink = deepLink
}
newReports = append(newReports, report)
}
case false:
for _, report := range reports {
deepLink, err := rym.getDeepVideoLinkByRedirect(report.cyberlockerLink)
if err != nil {
logger.Error("Error getting deep link", zap.String("Error", err.Error()))
} else {
report.cyberlockerLink = deepLink
}
newReports = append(newReports, report)
}
}
reports = newReports
}
return reports, nil
}
// findLicensor finds licensor name by analysing all the loaded brand names. The name of the licensor is the filename
// and the brand name is what's contained in the files separated by lines.
func (rym *rymscrape) findLicensor(brandName string) string {
for _, c := range rym.myclients {
for _, b := range c.data {
if strings.Contains(slugify.Slugify(brandName), slugify.Slugify(b)) {
return c.fileName
}
}
}
return ""
}
// getGoqueryDocument retrieves the page content in goquery.Document format
func (rym *rymscrape) getGoqueryDocument(link string) (pageGoqueryDocument *goquery.Document, err error) {
pageRawHTML, _, err := requestGet(link, rym.timeout, false, rym.jseed.SiteSignature)
if err != nil {
return pageGoqueryDocument, err
}
pageGoqueryDocument, err = goquery.NewDocumentFromReader(bytes.NewReader(pageRawHTML))
if err != nil {
return pageGoqueryDocument, err
}
return pageGoqueryDocument, nil
}
// getDeepVideoLinkByPattern retrieves deep link by pattern
// if rym.jseed.VideoListAcquire.GoDeeper.ByPattern.isTrue is true
func (rym *rymscrape) getDeepVideoLinkByPattern(link, patternStart, patternEnd string) (deepLink string, err error) {
return "", nil
}
// getDeepVideoLinkByRedirect retrieves deep link by redirect
// if rym.jseed.VideoListAcquire.GoDeeper.ByRedirect.isTrue is true
func (rym *rymscrape) getDeepVideoLinkByRedirect(link string) (deepLink string, err error) {
r, _, err := requestGet(link, rym.timeout, true, rym.jseed.SiteSignature)
if err != nil {
return "", err
}
deepLink = string(r)
return deepLink, nil
}