-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrapper.go
106 lines (91 loc) · 2.68 KB
/
scrapper.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
package main
import (
"context"
"database/sql"
"fmt"
"log"
"strings"
"sync"
"time"
"github.com/google/uuid"
"github.com/wreckitral/RSS-feed-aggregator/internal/database"
)
func startScrapping(db *database.Queries, concurrency int, timeBetweenRequest time.Duration) {
log.Printf("Scrapping on %v goroutines every %s duration", concurrency, timeBetweenRequest)
ticker := time.NewTicker(timeBetweenRequest)
for ; ; <-ticker.C {
feeds, err := db.GetNextFeedsToFetch(
context.Background(),
int32(concurrency),
)
if err != nil {
log.Println("error fetching feeds:", err)
continue
}
wg := &sync.WaitGroup{}
for _, feed := range feeds {
wg.Add(1)
go scrapeFeed(db, wg, feed)
}
wg.Wait()
}
}
func scrapeFeed(db *database.Queries, wg *sync.WaitGroup, feed database.Feed) {
defer wg.Done()
_, err := db.MarkFeedAsFetched(context.Background(), feed.ID)
if err != nil {
log.Println("Error marking feed as fetched:", err)
return
}
rssFeed, err := urlToFeed(feed.Url)
if err != nil {
log.Println("Error fetching feed:", err)
}
for _, item := range rssFeed.Channel.Item {
desc := sql.NullString{}
if item.Description != "" {
desc.String = item.Description
desc.Valid = true
}
pubAt, err := parseTime(item.PubDate)
if err != nil {
log.Println("error parsing time:", err, "on item", item.Title)
}
_, err = db.CreatePost(context.Background(),
database.CreatePostParams{
ID: uuid.New(),
CreatedAt: time.Now().UTC(),
UpdatedAt: time.Now().UTC(),
Title: item.Title,
Url: item.Link,
Description: desc,
PublishedAt: pubAt,
FeedID: feed.ID,
},
)
if err != nil {
if strings.Contains(err.Error(), "duplicate key") {
continue
}
log.Println("failed to create new post:", err)
}
}
log.Printf("Feed %s collected, %v posts found", feed.Name, len(rssFeed.Channel.Item))
}
func parseTime(timeStr string) (time.Time, error) {
// Primary format to try
formats := []string{
time.RFC1123Z,
time.RFC1123,
time.RFC3339,
}
var t time.Time
var err error
for _, format := range formats {
t, err = time.Parse(format, timeStr)
if err == nil {
return t, nil
}
}
return time.Time{}, fmt.Errorf("unable to parse time: %v", timeStr)
}