-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrape_feeds.go
More file actions
52 lines (42 loc) · 1.16 KB
/
scrape_feeds.go
File metadata and controls
52 lines (42 loc) · 1.16 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
package main
import (
"context"
"database/sql"
"fmt"
"log"
"time"
"github.com/SamW94/GoGator/internal/database"
"github.com/SamW94/GoGator/internal/rss"
)
func scrapeFeeds(s *state, rssClient *rss.Client) error {
context := context.Background()
feed, err := s.db.GetNextFeedToFetch(context)
if err != nil {
return fmt.Errorf("error when calling database.GetNextFeedToFetch() function: %w", err)
}
feedID := feed.ID
nullTime := sql.NullTime{
Time: time.Now(),
Valid: true,
}
markFetchedParams := database.MarkFeedFetchedParams{
LastFetchedAt: nullTime,
ID: feedID,
}
feed, err = s.db.MarkFeedFetched(context, markFetchedParams)
if err != nil {
return fmt.Errorf("error when calling database.MarkFeedFetched() function: %w", err)
}
rssFeed, err := rssClient.FetchFeed(context, feed.Url)
if err != nil {
return fmt.Errorf("error calling rss.FetchFeed() function with URL %s: %w", feed.Url, err)
}
for i := 0; len(rssFeed.Channel.Item) > i; i++ {
_, err := createPostInDB(s, rssFeed.Channel.Item[i], feed.Url)
if err != nil {
log.Printf("error calling the createPostInDB() function: %v", err)
continue
}
}
return nil
}