-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrss.go
More file actions
62 lines (52 loc) · 1.16 KB
/
rss.go
File metadata and controls
62 lines (52 loc) · 1.16 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
package main
import (
"encoding/xml"
"io"
"net/http"
"time"
)
// RSS feed XML structure
type RSSFeed struct {
Channel struct {
Title string `xml:"title"`
Link string `xml:"link"`
Description string `xml:"description"`
Language string `xml:"language"`
Item []RSSItem `xml:"item"`
} `xml:"channel"`
}
type RSSItem struct {
Title string `xml:"title"`
Link string `xml:"link"`
Description string `xml:"description"`
PubDate string `xml:"pubDate"`
}
func urlToFeed(url string) (RSSFeed, error) {
var rssFeed RSSFeed
// Make an HTTP GET request
resp, err := http.Get(url)
if err != nil {
return rssFeed, err
}
defer resp.Body.Close()
// Check status code
if resp.StatusCode != http.StatusOK {
return rssFeed, err
}
// Read the response body
dat, err := io.ReadAll(resp.Body)
if err != nil {
return rssFeed, err
}
// Parse XML
err = xml.Unmarshal(dat, &rssFeed)
if err != nil {
return rssFeed, err
}
return rssFeed, nil
}
func parseRSSDate(dateStr string) (time.Time, error) {
// Common RSS date format
layout := "Mon, 02 Jan 2006 15:04:05 -0700"
return time.Parse(layout, dateStr)
}