-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
59 lines (52 loc) · 1.5 KB
/
main.go
File metadata and controls
59 lines (52 loc) · 1.5 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
package main
import (
"strings"
"bufio"
"flag"
"fmt"
"log"
"os"
"path/filepath"
)
var (
fullTargetFilePath string
targetDirectory = flag.String("t", "", "Target directory for your images.")
targetFeed = flag.String("f", DefaultFeed, "RSS Feed from which to pull images.")
maxImages = flag.Int("i", 10000, "Image download count limit.")
)
// DefaultFeed is the nasa image of the day rss.
const DefaultFeed = "https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss"
func main() {
if parseArgs() {
fullTargetFilePath, _ = filepath.Abs(*targetDirectory)
if confirmFullFilePathAndFeed(fullTargetFilePath, *targetFeed) {
feed := RetrieveResource(*targetFeed, "feed")
enclosures := EnclosureUrlsFromRssBytes(feed.Data)
for _, enc := range enclosures[0:*maxImages] {
resource := RetrieveResource(enc.URL, enc.Type)
if err := resource.SaveResource(fullTargetFilePath); err != nil {
log.Fatalf("Error saving resource: %s", err)
}
}
}
}
}
func parseArgs() bool {
flag.Parse()
if args := flag.Args(); len(args) == 1 {
*targetDirectory = args[0]
}
if *targetDirectory == "" {
log.Fatal("Error: TargetDirectory is required.")
return false
}
return true
}
func confirmFullFilePathAndFeed(path, feed string) bool {
fmt.Printf("This will save images from %q to %q do you wish to continue? (y/N): ", feed, path)
char, _, err := bufio.NewReader(os.Stdin).ReadRune()
if err != nil {
log.Fatal("Error: reading input")
}
return strings.ToUpper(string(char)) == "Y"
}