-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler_agg.go
More file actions
120 lines (106 loc) · 2.94 KB
/
handler_agg.go
File metadata and controls
120 lines (106 loc) · 2.94 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"context"
"errors"
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
"github.com/google/uuid"
"github.com/mikarwacki/gator/internal/database"
"github.com/mikarwacki/gator/internal/rss"
)
const url = "https://www.wagslane.dev/index.xml"
func browse(s *state, c command) error {
limit := 2
if len(c.Args) == 1 {
temp, err := strconv.Atoi(c.Args[0])
if err != nil {
return fmt.Errorf("Command requires one argument that is the number %v", err)
}
limit = temp
}
user, err := s.db.GetUser(context.Background(), s.cfg.CurrentUserName)
if err != nil {
return fmt.Errorf("User is not registered %v", err)
}
params := database.GetPostsForUserParams{UserID: user.ID, Limit: int32(limit)}
posts, err := s.db.GetPostsForUser(context.Background(), params)
if err != nil {
return fmt.Errorf("Posts fetching failed %v", err)
}
for _, post := range posts {
printPost(post)
}
return nil
}
func agg(st *state, c command) error {
if len(c.Args) != 1 {
return errors.New("This command requieres one argument (duration between fetches)")
}
timeBetweenReqs := c.Args[0]
duration, err := time.ParseDuration(timeBetweenReqs)
if err != nil {
return fmt.Errorf("Cound't parse duration %v", err)
}
ticker := time.NewTicker(duration)
for ; ; <-ticker.C {
err = scrapeFeeds(st)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
}
func scrapeFeeds(s *state) error {
nextFeed, err := s.db.GetNextFeedToFetch(context.Background())
if err != nil {
return fmt.Errorf("Failed getting next feed from db\n %v", err)
}
markedFeed, err := s.db.MarkFeedFetched(context.Background(), nextFeed.ID)
if err != nil {
return fmt.Errorf("Failed marking feed as fetched\n %v", err)
}
rssFeed, err := rss.FetchFeed(context.Background(), markedFeed.Url)
if err != nil {
return fmt.Errorf("Failed fetching the feed\n %v", err)
}
for _, rssItem := range rssFeed.Channel.Item {
id := uuid.New()
dt := time.Now().UTC()
date, err := time.Parse("Mon, 02 Jan 2006 15:04:05 -0700", rssItem.PubDate)
if err != nil {
return fmt.Errorf("Coudn't parse Publish date of a post %v", err)
}
fmt.Println(rssItem.Description)
params := database.CreatePostParams{
ID: id,
CreatedAt: dt,
UpdatedAt: dt,
Url: rssItem.Link,
FeedID: markedFeed.ID,
Title: rssItem.Title,
Description: rssItem.Description,
PostedAt: date,
}
_, err = s.db.CreatePost(context.Background(), params)
if err != nil {
if strings.Contains(err.Error(), "duplicate key value violates unique constraint") {
continue
} else {
log.Printf("Couldn't create post: %v", err)
continue
}
}
}
return nil
}
func printPost(post database.GetPostsForUserRow) {
fmt.Printf("%s from %s\n", post.PostedAt.Format("Mon Jan 2"), post.FeedName)
fmt.Printf("--- %s ---\n", post.Title)
fmt.Printf(" %v\n", post.Description)
fmt.Printf("Link: %s\n", post.Url)
fmt.Println("=====================================")
}