-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
90 lines (71 loc) · 1.82 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
package main
import (
"flag"
"log"
"net/http"
"net/url"
"os"
"strings"
gofeed "github.com/mmcdole/gofeed"
)
func prepareDestination(path string) {
if _, err := os.Stat(path); os.IsNotExist(err) {
os.Mkdir(path, 0755)
}
}
func savePage(url string, path string) {
resp, err := http.Get(url)
if err != nil {
log.Panic(err)
}
f, err := os.Create(path)
defer f.Close()
if err != nil {
log.Panic(err)
}
err = resp.Write(f)
if err != nil {
log.Panic(err)
}
}
func main() {
feedURLPtr := flag.String("url", "", "RSS or Atom feed URL")
destDirectoryPtr := flag.String("destination", "", "Local directory path where items should")
flag.Parse()
if *feedURLPtr == "" {
log.Fatal("Provide -url parameter to identify feed to download, to show more options use $ xmlfeed_dump -h")
}
if *destDirectoryPtr == "" {
path, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
*destDirectoryPtr = path
}
feedURL, err := url.Parse(*feedURLPtr)
if err != nil {
log.Fatal("Use Feed URL as the firts argument")
panic(err)
}
log.Printf("Starting RSS/Atom feed website dumper for %s", feedURL.String())
log.Println("Preparing destination directory..")
destDirectory := *destDirectoryPtr + "/" + feedURL.Hostname()
prepareDestination(destDirectory)
log.Println("Fetching the feed..")
fp := gofeed.NewParser()
feed, err := fp.ParseURL(feedURL.String())
if err != nil {
log.Println("Invalid Feed")
panic(err)
}
log.Println("Have valid feed with name: " + feed.Title)
log.Println("Fetching items..")
for i := 0; i < len(feed.Items); i++ {
item := feed.Items[i]
log.Println("Storing Item from URL:" + item.Link)
targetPath := destDirectory + "/" + strings.ReplaceAll(item.Link, "/", "-")
savePage(item.Link, targetPath)
log.Println("Stored to: " + targetPath)
}
log.Println("Dump completed.")
}