2022-04-26 09:55:59 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-05-02 12:40:38 -04:00
|
|
|
"bytes"
|
2022-04-26 09:55:59 -04:00
|
|
|
"encoding/csv"
|
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
2022-05-02 12:40:38 -04:00
|
|
|
"io"
|
2022-04-26 09:55:59 -04:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
XMLName xml.Name `xml:"person"`
|
|
|
|
FirstName string `xml:"firstname"`
|
|
|
|
Parents interface{} `xml:"parent"`
|
|
|
|
*/
|
|
|
|
|
|
|
|
type Item struct {
|
|
|
|
Title string `xml:"title"`
|
|
|
|
Link string `xml:"link"`
|
|
|
|
Author string `xml:"author"`
|
|
|
|
Guid string `xml:"guid"`
|
|
|
|
Description string `xml:"description"`
|
|
|
|
Content string `xml:",innerxml"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
fmt.Println("hello go")
|
2022-05-02 12:40:38 -04:00
|
|
|
data, err := os.ReadFile("/home/balki/projects/tss/w.tmp/ounapuu.xml")
|
2022-04-26 09:55:59 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
v := struct {
|
|
|
|
Items []Item `xml:"channel>item"`
|
|
|
|
}{}
|
|
|
|
|
|
|
|
err = xml.Unmarshal(data, &v)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.OpenFile("./data.csv", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2022-05-02 12:40:38 -04:00
|
|
|
var b bytes.Buffer
|
|
|
|
b.WriteString("hello world")
|
|
|
|
csvw := csv.NewWriter(&b)
|
2022-04-26 09:55:59 -04:00
|
|
|
csvw.Write([]string{
|
|
|
|
"date",
|
|
|
|
"link",
|
|
|
|
"info",
|
|
|
|
})
|
|
|
|
|
2022-05-02 12:40:38 -04:00
|
|
|
for index, item := range v.Items[:3] {
|
2022-04-26 09:55:59 -04:00
|
|
|
c := item.Content
|
2022-05-02 12:40:38 -04:00
|
|
|
fmt.Println(index, len(c), item.Title)
|
|
|
|
//fmt.Println(item.Content)
|
|
|
|
err = csvw.Write([]string{
|
2022-04-26 09:55:59 -04:00
|
|
|
time.Now().String(),
|
|
|
|
item.Link,
|
|
|
|
strings.Replace(c, "\n", " ", -1),
|
|
|
|
})
|
2022-05-02 12:40:38 -04:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
err = csvw.Write([]string{
|
|
|
|
time.Now().String(),
|
|
|
|
"dummy.link",
|
|
|
|
"dummy content, lskdjf lksjdf lkjk",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
*/
|
2022-04-26 09:55:59 -04:00
|
|
|
}
|
2022-05-02 12:40:38 -04:00
|
|
|
csvw.Flush()
|
|
|
|
err = f.Sync()
|
|
|
|
fmt.Println(err)
|
|
|
|
data2, err := io.ReadAll(&b)
|
|
|
|
fmt.Printf("data2: %s\n", data2)
|
|
|
|
fmt.Println(err)
|
|
|
|
// fmt.Println(v.Items[len(v.Items)-1].Content)
|
2022-04-26 09:55:59 -04:00
|
|
|
}
|
|
|
|
func foo() {
|
|
|
|
x := `
|
|
|
|
<foo>
|
|
|
|
<bar><blue>slkdfjdslk</blue></bar>
|
|
|
|
</foo>
|
|
|
|
`
|
|
|
|
s := struct {
|
|
|
|
Bar struct {
|
|
|
|
Content string `xml:",innerxml"`
|
|
|
|
} `xml:"bar"`
|
|
|
|
}{}
|
|
|
|
|
|
|
|
err := xml.Unmarshal([]byte(x), &s)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
fmt.Println(s.Bar.Content)
|
|
|
|
}
|