Added srpskainfo crawler
This commit is contained in:
101
internal/scraper/srpskainfo.go
Normal file
101
internal/scraper/srpskainfo.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package scraper
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gocolly/colly"
|
||||
"github.com/gosimple/slug"
|
||||
"gitlab.com/kbr4/svevijesti/internal/model"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var SrpskainfoArticles = make(chan model.ScrapedArticle)
|
||||
var SrpskainfoCandidates = make(chan string)
|
||||
var SrpskainfoApprovedSites = make(chan string, 2)
|
||||
|
||||
func CrawlSrpskainfo() {
|
||||
|
||||
crHomePage := colly.NewCollector(colly.AllowedDomains("srpskainfo.com"))
|
||||
crArticlePage := colly.NewCollector(colly.AllowedDomains("srpskainfo.com"))
|
||||
|
||||
setupSiArticlePageCrawler(crArticlePage)
|
||||
setupSiHomepageCrawler(crHomePage, crArticlePage)
|
||||
|
||||
go visitSiApprovedPages(crArticlePage)
|
||||
}
|
||||
|
||||
func visitSiApprovedPages(crArticlePage *colly.Collector) {
|
||||
fmt.Println("Consuming sites!")
|
||||
for url := range SrpskainfoApprovedSites {
|
||||
fmt.Println("Visiting: ", url)
|
||||
crArticlePage.Visit(url)
|
||||
}
|
||||
}
|
||||
|
||||
func setupSiHomepageCrawler(crHomePage *colly.Collector, crArticlePage *colly.Collector) {
|
||||
|
||||
crHomePage.OnHTML("a", func(e *colly.HTMLElement) {
|
||||
articleUrlR, _ := regexp.Compile("([A-Za-z0-9]+-){3,}([A-Za-z0-9]+)/$")
|
||||
url := e.Attr("href")
|
||||
completeUrl := url
|
||||
if articleUrlR.MatchString(url) {
|
||||
SrpskainfoCandidates <- completeUrl
|
||||
}
|
||||
})
|
||||
|
||||
crHomePage.OnScraped(func(_ *colly.Response) {
|
||||
time.Sleep(5 * time.Second)
|
||||
terminating := model.ScrapedArticle{}
|
||||
terminating.Title = model.Terminator
|
||||
SrpskainfoArticles <- terminating
|
||||
SrpskainfoApprovedSites <- model.Terminator
|
||||
SrpskainfoCandidates <- model.Terminator
|
||||
})
|
||||
|
||||
crHomePage.OnError(func(_ *colly.Response, _ error) {
|
||||
time.Sleep(5 * time.Second)
|
||||
terminating := model.ScrapedArticle{}
|
||||
terminating.Title = model.Terminator
|
||||
SrpskainfoArticles <- terminating
|
||||
SrpskainfoApprovedSites <- model.Terminator
|
||||
SrpskainfoCandidates <- model.Terminator
|
||||
})
|
||||
|
||||
go crHomePage.Visit("https://srpskainfo.com")
|
||||
}
|
||||
|
||||
func setupSiArticlePageCrawler(crArticlePage *colly.Collector) {
|
||||
crArticlePage.OnHTML("html", func(e *colly.HTMLElement) {
|
||||
|
||||
url := e.Request.URL.String()
|
||||
|
||||
title := ""
|
||||
e.ForEachWithBreak("h1", func(_ int, el *colly.HTMLElement) bool {
|
||||
title = el.Text
|
||||
return false
|
||||
})
|
||||
|
||||
text := ""
|
||||
|
||||
e.ForEach("p.article__top-content, p.article__content, h4.article__content, h3.article__content, h2.article__content, div.article__content", func(_ int, el *colly.HTMLElement) {
|
||||
text += extractJustText(el.DOM)
|
||||
})
|
||||
|
||||
article := model.ScrapedArticle{}
|
||||
|
||||
trimmedText := strings.TrimSpace(text)
|
||||
article.OriginalUrl = url
|
||||
article.Title = title
|
||||
article.Content = trimmedText
|
||||
article.SourceId = model.SrpskainfoSource
|
||||
article.Slug = slug.Make(title)
|
||||
|
||||
SrpskainfoArticles <- article
|
||||
})
|
||||
|
||||
crArticlePage.OnError(func(_ *colly.Response, _ error) {
|
||||
fmt.Println("Problem crawling!")
|
||||
})
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user