Skip to content

Instantly share code, notes, and snippets.

@gkawamoto
Created November 14, 2018 17:33
Show Gist options
  • Save gkawamoto/8590b48639ee81f4ff536316a9bed171 to your computer and use it in GitHub Desktop.
Save gkawamoto/8590b48639ee81f4ff536316a9bed171 to your computer and use it in GitHub Desktop.
Exportando casts e episódios do 99Vidas para CSV
package main
import (
"bytes"
"encoding/csv"
"encoding/xml"
"io"
"net/http"
"os"
)
// RSS ?
type RSS struct {
Channel []Channel `xml:"channel"`
}
// Channel ?
type Channel struct {
Title string `xml:"title"`
Item []Item `xml:"item"`
}
// Item ?
type Item struct {
Title string `xml:"title"`
PubDate string `xml:"pubDate"`
}
func main() {
var data, err = downloadFile("http://99vidas.com.br/99vidas.xml")
if err != nil {
panic(err)
}
var result = RSS{}
err = xml.Unmarshal(data, &result)
if err != nil {
panic(err)
}
var item Item
var writer = csv.NewWriter(os.Stdout)
writer.Write([]string{"Título", "Data de Publicação", "Responsável"})
for _, item = range result.Channel[0].Item {
writer.Write([]string{item.Title, item.PubDate})
}
writer.Flush()
}
func downloadFile(url string) ([]byte, error) {
var buffer bytes.Buffer
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
_, err = io.Copy(&buffer, resp.Body)
if err != nil {
return nil, err
}
return buffer.Bytes(), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment