#12 - update feeds
This commit is contained in:
@@ -22,7 +22,7 @@ VALUES (
|
|||||||
$5,
|
$5,
|
||||||
$6
|
$6
|
||||||
)
|
)
|
||||||
RETURNING id, created_at, updated_at, name, url, user_id
|
RETURNING id, created_at, updated_at, name, url, user_id, last_fetched_at
|
||||||
`
|
`
|
||||||
|
|
||||||
type CreateFeedParams struct {
|
type CreateFeedParams struct {
|
||||||
@@ -51,6 +51,7 @@ func (q *Queries) CreateFeed(ctx context.Context, arg CreateFeedParams) (Feed, e
|
|||||||
&i.Name,
|
&i.Name,
|
||||||
&i.Url,
|
&i.Url,
|
||||||
&i.UserID,
|
&i.UserID,
|
||||||
|
&i.LastFetchedAt,
|
||||||
)
|
)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const getFeedByUrl = `-- name: GetFeedByUrl :one
|
const getFeedByUrl = `-- name: GetFeedByUrl :one
|
||||||
SELECT id, created_at, updated_at, name, url, user_id FROM feeds WHERE url = $1
|
SELECT id, created_at, updated_at, name, url, user_id, last_fetched_at FROM feeds WHERE url = $1
|
||||||
`
|
`
|
||||||
|
|
||||||
func (q *Queries) GetFeedByUrl(ctx context.Context, url string) (Feed, error) {
|
func (q *Queries) GetFeedByUrl(ctx context.Context, url string) (Feed, error) {
|
||||||
@@ -23,6 +23,7 @@ func (q *Queries) GetFeedByUrl(ctx context.Context, url string) (Feed, error) {
|
|||||||
&i.Name,
|
&i.Name,
|
||||||
&i.Url,
|
&i.Url,
|
||||||
&i.UserID,
|
&i.UserID,
|
||||||
|
&i.LastFetchedAt,
|
||||||
)
|
)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
|
|||||||
29
internal/database/getNextFeedToFetch.sql.go
Normal file
29
internal/database/getNextFeedToFetch.sql.go
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
// Code generated by sqlc. DO NOT EDIT.
|
||||||
|
// versions:
|
||||||
|
// sqlc v1.28.0
|
||||||
|
// source: getNextFeedToFetch.sql
|
||||||
|
|
||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
)
|
||||||
|
|
||||||
|
const getNextFeedToFetch = `-- name: GetNextFeedToFetch :one
|
||||||
|
SELECT id, created_at, updated_at, name, url, user_id, last_fetched_at FROM feeds ORDER BY last_fetched_at NULLS FIRST LIMIT 1
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) GetNextFeedToFetch(ctx context.Context) (Feed, error) {
|
||||||
|
row := q.db.QueryRowContext(ctx, getNextFeedToFetch)
|
||||||
|
var i Feed
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
&i.Name,
|
||||||
|
&i.Url,
|
||||||
|
&i.UserID,
|
||||||
|
&i.LastFetchedAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
32
internal/database/markFeeedFetched.sql.go
Normal file
32
internal/database/markFeeedFetched.sql.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
// Code generated by sqlc. DO NOT EDIT.
|
||||||
|
// versions:
|
||||||
|
// sqlc v1.28.0
|
||||||
|
// source: markFeeedFetched.sql
|
||||||
|
|
||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
const markFeedFetched = `-- name: MarkFeedFetched :one
|
||||||
|
UPDATE feeds SET last_fetched_at = CURRENT_TIMESTAMP, updated_at = CURRENT_TIMESTAMP WHERE id = $1
|
||||||
|
RETURNING id, created_at, updated_at, name, url, user_id, last_fetched_at
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) MarkFeedFetched(ctx context.Context, id uuid.UUID) (Feed, error) {
|
||||||
|
row := q.db.QueryRowContext(ctx, markFeedFetched, id)
|
||||||
|
var i Feed
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
&i.Name,
|
||||||
|
&i.Url,
|
||||||
|
&i.UserID,
|
||||||
|
&i.LastFetchedAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@
|
|||||||
package database
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"database/sql"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
@@ -17,6 +18,7 @@ type Feed struct {
|
|||||||
Name string
|
Name string
|
||||||
Url string
|
Url string
|
||||||
UserID uuid.UUID
|
UserID uuid.UUID
|
||||||
|
LastFetchedAt sql.NullTime
|
||||||
}
|
}
|
||||||
|
|
||||||
type FeedFollow struct {
|
type FeedFollow struct {
|
||||||
|
|||||||
@@ -1,30 +1,28 @@
|
|||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"github.com/rdarius/boot-dev-blog-aggregator/internal/config"
|
"github.com/rdarius/boot-dev-blog-aggregator/internal/config"
|
||||||
"github.com/rdarius/boot-dev-blog-aggregator/internal/rss"
|
"log"
|
||||||
"os"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func FetchFeedHandler(s *config.State, cmd config.Command) error {
|
func FetchFeedHandler(s *config.State, cmd config.Command) error {
|
||||||
var feedUrl string
|
|
||||||
if len(cmd.Args) < 1 {
|
if len(cmd.Args) < 1 {
|
||||||
feedUrl = "https://www.wagslane.dev/index.xml"
|
log.Fatal("usage: boot-dev-blog-aggregator agg time_between_reqs")
|
||||||
} else {
|
|
||||||
feedUrl = cmd.Args[0]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := context.Background()
|
t, err := time.ParseDuration(cmd.Args[0])
|
||||||
|
|
||||||
feed, err := rss.FetchFeed(ctx, feedUrl)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Failed to fetch feed")
|
return err
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("%v\n", feed)
|
ticker := time.NewTicker(t)
|
||||||
|
for ; ; <-ticker.C {
|
||||||
|
err := ScrapeFeedsHandler(s, cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
36
internal/handlers/scrapeFeedsHandler.go
Normal file
36
internal/handlers/scrapeFeedsHandler.go
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"github.com/rdarius/boot-dev-blog-aggregator/internal/config"
|
||||||
|
"github.com/rdarius/boot-dev-blog-aggregator/internal/rss"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ScrapeFeedsHandler(s *config.State, cmd config.Command) error {
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
nextFeed, err := s.DB.GetNextFeedToFetch(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = s.DB.MarkFeedFetched(ctx, nextFeed.ID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
feedData, err := rss.FetchFeed(ctx, nextFeed.Url)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Feed Title: " + feedData.Channel.Title)
|
||||||
|
|
||||||
|
for _, i := range feedData.Channel.Item {
|
||||||
|
fmt.Println("Item Title: " + i.Title)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
2
sql/queries/getNextFeedToFetch.sql
Normal file
2
sql/queries/getNextFeedToFetch.sql
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
-- name: GetNextFeedToFetch :one
|
||||||
|
SELECT * FROM feeds ORDER BY last_fetched_at NULLS FIRST LIMIT 1;
|
||||||
3
sql/queries/markFeeedFetched.sql
Normal file
3
sql/queries/markFeeedFetched.sql
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
-- name: MarkFeedFetched :one
|
||||||
|
UPDATE feeds SET last_fetched_at = CURRENT_TIMESTAMP, updated_at = CURRENT_TIMESTAMP WHERE id = $1
|
||||||
|
RETURNING *;
|
||||||
5
sql/schema/0004_last_fetched_field_in_feed.sql
Normal file
5
sql/schema/0004_last_fetched_field_in_feed.sql
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
-- +goose Up
|
||||||
|
ALTER TABLE feeds ADD COLUMN last_fetched_at timestamp DEFAULT null;
|
||||||
|
|
||||||
|
-- +goose Down
|
||||||
|
ALTER TABLE feeds DROP COLUMN last_fetched_at;
|
||||||
Reference in New Issue
Block a user