From e628c534de451c6a6d3479aa0a0913b5f831c720 Mon Sep 17 00:00:00 2001 From: Logen Kain Date: Thu, 21 Sep 2017 20:20:15 -0700 Subject: [PATCH] golang:generic_net_fetch:first_commit --- go/generic_net_fetch/TODO | 1 + go/generic_net_fetch/main.go | 44 ++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 go/generic_net_fetch/TODO create mode 100644 go/generic_net_fetch/main.go diff --git a/go/generic_net_fetch/TODO b/go/generic_net_fetch/TODO new file mode 100644 index 0000000..09caee3 --- /dev/null +++ b/go/generic_net_fetch/TODO @@ -0,0 +1 @@ +Get a real name diff --git a/go/generic_net_fetch/main.go b/go/generic_net_fetch/main.go new file mode 100644 index 0000000..213a888 --- /dev/null +++ b/go/generic_net_fetch/main.go @@ -0,0 +1,44 @@ +package main + +import ( + "fmt" + "io" + "net/http" + "os" + + "github.com/mmcdole/gofeed" +) + +func fileGet(filepath, url string) (err error) { + out, err := os.Create(filepath) + if err != nil { + return err + } + defer out.Close() + + resp, err := http.Get(url) + if err != nil { + return err + } + defer resp.Body.Close() + + _, err = io.Copy(out, resp.Body) + if err != nil { + return err + } + + return nil +} + +func main() { + feedParser := gofeed.NewParser() + feed, _ := feedParser.ParseURL("http://feeds.twit.tv/twit.xml") + + fileUrl := feed.Items[1].Enclosures[0].URL + fileTitle := feed.Items[1].Title + + fmt.Println(fileUrl) + fmt.Println(fileTitle) + + fileGet(fileTitle, fileUrl) +}