Skip to content

Commit

Permalink
Add Create Post Scraper
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyyy09 committed Aug 3, 2023
1 parent c3c3058 commit 1ccdf33
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ package main

import (
"context"
"database/sql"
"encoding/xml"
"io"
"log"
"net/http"
"strings"
"sync"
"time"

"github.com/bootdotdev/projects/posts/internal/database"
"github.com/google/uuid"
)

func startScraping(db *database.Queries, concurrency int, timeBetweenRequest time.Duration) {
Expand Down Expand Up @@ -45,7 +50,34 @@ func scrapeFeed(db *database.Queries, wg *sync.WaitGroup, feed database.Feed) {
return
}
for _, item := range feedData.Channel.Item {
log.Println("Found post", item.Title)
publishedAt := sql.NullTime{}
if t, err := time.Parse(time.RFC1123Z, item.PubDate); err == nil {
publishedAt = sql.NullTime{
Time: t,
Valid: true,
}
}

_, err = db.CreatePost(context.Background(), database.CreatePostParams{
ID: uuid.New(),
CreatedAt: time.Now().UTC(),
UpdatedAt: time.Now().UTC(),
FeedID: feed.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("Feed %s collected, %v posts found", feed.Name, len(feedData.Channel.Item))
}
Expand Down

0 comments on commit 1ccdf33

Please sign in to comment.