-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
153 lines (135 loc) · 3.63 KB
/
main.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"bytes"
"fmt"
"github.com/gorilla/feeds"
"github.com/mmcdole/gofeed"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
)
var now = time.Now()
var client = http.Client{
Timeout: 15 * time.Second,
}
const USER_AGENT = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/117.0"
func main() {
parser := gofeed.NewParser()
parser.Client = &client
parser.UserAgent = USER_AGENT
origFeed, err := parser.ParseURL("https://feeds.a.dj.com/rss/RSSWorldNews.xml")
if err != nil {
log.Fatal("failed to fetch feed: ", err, "\n")
}
myFeed := &feeds.Feed{
Title: origFeed.Title + " - bigTop stories",
Link: &feeds.Link{Href: "https://std-move.github.io/rss-fetcher/wsj-world-bigtop.xml"},
Description: origFeed.Description,
Created: now,
}
for _, itm := range origFeed.Items {
if err := processArticle(itm, &myFeed.Items); err != nil {
log.Println(err)
}
}
if err = serializeFeed(myFeed, "public/wsj-world-bigtop.xml"); err != nil {
log.Fatal("failed to serialize my feed: ", err, "\n")
}
log.Print("Successfully updated feed, article count [",
len(myFeed.Items), "] out of [", len(origFeed.Items), "]", "\n")
}
func processArticle(item *gofeed.Item, myItems *[]*feeds.Item) error {
ext, ok := item.Extensions["dj"]
if !ok {
return fmt.Errorf("no dj extensions in article")
}
atype, ok := ext["articletype"]
if !ok {
return fmt.Errorf("no articletype in dj extension")
}
found := false
for _, itm := range atype {
if strings.Contains(itm.Value, "News") || strings.Contains(itm.Value, "news") {
found = true
break
}
}
if !found {
log.Print("not a news article [", item.Link, "]", "\n")
return nil
}
ampLink := func() string {
if !strings.Contains(item.Link, "wsj.com/amp/") {
return strings.Replace(item.Link, "wsj.com/", "wsj.com/amp/", -1)
} else {
return item.Link
}
}()
req, err := http.NewRequest("GET", ampLink, nil)
if err != nil {
return fmt.Errorf("%v [%v]: %w", "error creating req", ampLink, err)
}
req.Header.Set("User-Agent", USER_AGENT)
rsp, err := client.Do(req)
if err != nil {
return fmt.Errorf("%v [%v]: %w", "error fetching", ampLink, err)
}
defer rsp.Body.Close()
body, err := io.ReadAll(rsp.Body)
if err != nil {
return fmt.Errorf("%v [%v]: %w", "error reading", ampLink, err)
}
if !bytes.Contains(body, []byte("class=\"bigTop-hero")) &&
!bytes.Contains(body, []byte("\"bigtopheroid\"")) &&
!bytes.Contains(body, []byte("\"inset_type\":\"bigtophero\"")) {
log.Print("not a bigTop article [", ampLink, "]", "\n")
return nil
}
log.Print("bigTop article [", ampLink, "]", "\n")
created := func() time.Time {
if item.PublishedParsed != nil {
return *item.PublishedParsed
} else {
return now
}
}()
updated := func() time.Time {
if item.UpdatedParsed != nil {
return *item.UpdatedParsed
} else {
return created
}
}()
*myItems = append(*myItems, &feeds.Item{
Title: item.Title,
Link: &feeds.Link{Href: item.Link},
Content: item.Description,
Created: created,
Updated: updated,
})
return nil
}
func serializeFeed(myFeed *feeds.Feed, pathToFile string) error {
ser, err := myFeed.ToAtom()
if err != nil {
return err
}
err = os.MkdirAll(filepath.Dir(pathToFile), os.ModePerm)
if err != nil {
return fmt.Errorf("%v [%v]: %w", "failed to mkdir", filepath.Dir(pathToFile), err)
}
f, err := os.Create(pathToFile)
if err != nil {
return fmt.Errorf("%v [%v]: %w", "failed to create file", pathToFile, err)
}
defer f.Close()
_, err = f.WriteString(ser)
if err != nil {
return fmt.Errorf("%v [%v]: %w", "failed to write my feed", pathToFile, err)
}
return nil
}