-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_agg.go
81 lines (71 loc) · 2.01 KB
/
handler_agg.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
package main
import (
"context"
"database/sql"
"fmt"
"log"
"strings"
"time"
"github.com/Rodabaugh/gator/internal/database"
"github.com/google/uuid"
)
func handlerAgg(s *state, cmd command) error {
if len(cmd.arguments) != 1 {
return fmt.Errorf("usage: %s timeBetweenRequests (e.g. 30s, 1m, 1h)", cmd.name)
}
time_between_reqs := cmd.arguments[0]
timeBetweenRequests, err := time.ParseDuration(time_between_reqs)
if err != nil {
return fmt.Errorf("unable to parse %s as a time duration\n%w", time_between_reqs, err)
}
ticker := time.NewTicker(timeBetweenRequests)
for ; ; <-ticker.C {
scrapeFeeds(s)
}
}
func scrapeFeeds(s *state) error {
nextFeed, err := s.db.GetNextFeedToFetch(context.Background())
if err != nil {
return fmt.Errorf("couldn't get next feed to fetch: %w", err)
}
currentTime := time.Now()
_, err = s.db.MarkFeedFetched(context.Background(), database.MarkFeedFetchedParams{ID: nextFeed.ID, UpdatedAt: currentTime})
if err != nil {
return fmt.Errorf("couldn't mark feed as fetched: %w", err)
}
feed, err := fetchFeed(context.Background(), nextFeed.Url)
if err != nil {
return fmt.Errorf("was unable to fetch feed: %w", err)
}
for _, item := range feed.Channel.Item {
publishedAt := sql.NullTime{}
if t, err := time.Parse(time.RFC1123Z, item.PubDate); err == nil {
publishedAt = sql.NullTime{
Time: t,
Valid: true,
}
}
_, err = s.db.CreatePost(context.Background(), database.CreatePostParams{
ID: uuid.New(),
CreatedAt: time.Now().UTC(),
UpdatedAt: time.Now().UTC(),
FeedID: nextFeed.ID,
Title: item.Title,
Description: sql.NullString{
String: item.Description,
Valid: true,
},
Url: item.Link,
PublishedAt: publishedAt,
})
if err != nil {
if strings.Contains(err.Error(), "duplicate key value violates unique constraint") {
continue
}
log.Printf("Couldn't create post: %v", err)
continue
}
}
log.Printf("Collected feed %s. %v posts found", nextFeed.Name, len(feed.Channel.Item))
return nil
}