-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
89 lines (67 loc) · 1.67 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
package main
import (
"encoding/xml"
"fmt"
"io"
"net/http"
"os"
)
type RSS struct {
XMLName xml.Name `xml:"rss"`
Channel *Channel `xml:"channel"`
}
type Channel struct {
ChannelTitle string `xml:"title"`
ChannelLink string `xml:"link"`
ItemsList []Items `xml:"item"`
}
type Items struct {
ItemTitle string `xml:"title"`
ItemLink string `xml:"link"`
Categories []string `xml:"category"`
}
func main() {
var info RSS
data := readDevtoPosts()
err := xml.Unmarshal(data, &info)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
printInfo(info)
}
func getDevtoPosts() *http.Response {
resp, err := http.Get("https://dev.to//rss")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return resp
}
func readDevtoPosts() []byte {
resp := getDevtoPosts()
data, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return data
}
func printInfo(parsedXML RSS) {
fmt.Println("--------Posts of today at Dev.to--------")
fmt.Println("----------------------------------------")
for post := range parsedXML.Channel.ItemsList {
numOfPost := post + 1
fmt.Println("Post number", numOfPost)
fmt.Println("Post title:", parsedXML.Channel.ItemsList[post].ItemTitle)
fmt.Println("Post link:", parsedXML.Channel.ItemsList[post].ItemLink)
if len(parsedXML.Channel.ItemsList[post].Categories) <= 0 {
fmt.Println("Has no categories defined...")
}
for category := range parsedXML.Channel.ItemsList[post].Categories {
numOfCategory := category + 1
fmt.Println("Category number", numOfCategory, "is", parsedXML.Channel.ItemsList[post].Categories[category])
}
fmt.Println("-----------------------------------")
}
}