-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.go
91 lines (78 loc) · 2.09 KB
/
scraper.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
package main
import (
"context"
"database/sql"
"fmt"
"strings"
"sync"
"time"
"github.com/ahmed-khlifi/go-rss-aggregator/internal/database"
"github.com/google/uuid"
)
func startScraping(db *database.Queries,
concurency int,
timeBetweenRequest time.Duration){
fmt.Printf("Starting scraping on %v GoRoutines every %s duration\n", concurency, timeBetweenRequest)
ticker := time.NewTicker(timeBetweenRequest)
for ; ; <-ticker.C {
feeds, err := db.GetNextFeedToFetch(context.Background(), int32(concurency))
if err != nil {
fmt.Printf("Error getting feeds to fetch: %v\n", err)
continue
}
wg := &sync.WaitGroup{}
for _, feed := range feeds {
wg.Add(1)
go scrapeFeed(db, wg, feed)
}
wg.Wait() // Wait for all goroutine to finish before moving to the next loop iteration
}
}
func scrapeFeed(db *database.Queries,
wg *sync.WaitGroup,
feed database.Feed,
) {
defer wg.Done()
_,err:= db.MarkFeedAsFetched(context.Background(), feed.ID)
if err != nil {
fmt.Println("Error making feed as fetched")
}
resFedd, err := urlToFeed(feed.Url)
if err != nil {
fmt.Printf("Error fetching feed %s: %v\n", feed.Url, err)
return
}
for _, item := range resFedd.Channel.Item {
description := sql.NullString{}
if item.Description != "" {
description.String = item.Description
description.Valid = true
}
publishTime, err := time.Parse(time.RFC1123Z, item.PubDate)
if err != nil {
fmt.Printf("Error parsing time %s: %v\n", item.PubDate, err)
continue
}
url := sql.NullString{
String: item.Link,
Valid: true,
}
_, err = db.CreatePost(context.Background(), database.CreatePostParams{
ID: uuid.New(),
CreatedAt: time.Now().UTC(),
UpdatedAt: time.Now().UTC(),
Title: item.Title,
Description: description,
PublishedAt: publishTime,
Url: url,
FeedID: feed.ID,
})
if err != nil {
if strings.Contains(err.Error(), "Duplicate entry") {
continue
}
fmt.Printf("Error creating post: %v\n", err)
}
}
fmt.Printf("Feed %s collected, %v posts found\n", feed.Url, len(resFedd.Channel.Item))
}