-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfeed_scraper.go
54 lines (39 loc) · 916 Bytes
/
feed_scraper.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
package main
import (
"strings"
"github.com/mmcdole/gofeed"
"gorm.io/gorm"
)
func FeedScraper(key string, src *Source) ([]Post, error) {
fp := gofeed.NewParser()
ary := []Post{}
feedBody, err := Scrape(src.ScrapeUrl)
if err != nil {
return ary, err
}
feed, err := fp.ParseString(feedBody)
if err != nil {
return ary, err
}
for _, item := range feed.Items {
authors := []string{}
for _, author := range item.Authors {
authors = append(authors, author.Name)
}
previousPost, _ := FindPostByUrl(item.Link)
if !previousPost.DeletedAt.Time.IsZero() {
continue
}
post := Post{
Model: gorm.Model{ID: previousPost.ID},
Title: item.Title,
Summary: item.Description,
Url: item.Link,
Author: strings.Join(authors, ", "),
Source: key,
PublishedAt: *item.PublishedParsed,
}
ary = append(ary, post)
}
return ary, nil
}