package main

import (
    "bytes"
    "encoding/xml"
    "fmt"
    "io/ioutil"
    "os"
)

func main() {

    srcName := "i.rss"
    if len(os.Args) >= 2 {
        srcName = os.Args[1]
    }
    xml_up := getXmlFile("i.rss")
    xml := getXmlFile(srcName)
    rss_up := ParseRSS(xml_up)
    rss := ParseRSS(xml)

    displayRssChannel(rss.Channel)
    ComparedRSS(rss_up, rss)
}

//对比两张rss
func ComparedRSS(rss_up *RssFeed, rss *RssFeed) string {
    var items_up []*RssItem = rss_up.Channel.Item
    var items []*RssItem = rss.Channel.Item
    var maxLen = 10
    var buffer bytes.Buffer
    for i := 0; i < len(items); i++ {
        var check = false
        //判断在数组中是否存在
        for j := 0; j < len(items_up); j++ {
            if items[i].Title == items_up[j].Title {
                check = true
                break
            }
        }
        if !check {
            fmt.Println(items[i].Title)
            fmt.Println("bbs.yzjlb.net" + items[i].Comments)
            buffer.WriteString(fmt.Sprintf("%s\n%s%s", items[i].Title, "bbs.yzjlb.net", items[i].Comments))
            buffer.WriteString("\n\n")
        }
        if i >= maxLen {
            break
        }

        // var datetime, err = time.Parse(" 21 January 2021 13:01:42 +0800", rss.Item[i].PubDate[strings.Index(rss.Item[i].PubDate, ",")+1:])
        // // var datetime, err = time.ParseDuration(rss.Item[i].PubDate)
        // if err != nil {
        //  fmt.Println("err: ", err)
        // }
        // fmt.Println(datetime.Format("2006-01-02 15:04:05 010"))
    }
    if buffer.Len() > 2 {
        return buffer.String()[0 : buffer.Len()-2]
    }
    return ""
}

//显示rss信息
func displayRssChannel(rss *RssChannel) {
    fmt.Println("Title:", rss.Title)
    fmt.Println("Link:", rss.Link)
    fmt.Println("Last build date:", rss.LastBuildDate)
    fmt.Println("Pub date:", rss.PubDate)
    fmt.Println("Language:", rss.Language)
    fmt.Println("Description:", rss.Description)
    for i := 0; i < len(rss.Item); i++ {
        fmt.Println(rss.Item[i].Title)
        // var datetime, err = time.Parse(" 21 January 2021 13:01:42 +0800", rss.Item[i].PubDate[strings.Index(rss.Item[i].PubDate, ",")+1:])
        // // var datetime, err = time.ParseDuration(rss.Item[i].PubDate)
        // if err != nil {
        //  fmt.Println("err: ", err)
        // }
        // fmt.Println(datetime.Format("2006-01-02 15:04:05 010"))
    }
}

//读取文件
func getXmlFile(filename string) []byte {
    f, err := ioutil.ReadFile(filename)
    if err != nil {
        fmt.Println("Unable to read src xml file.", err)
        os.Exit(0)
    }
    return f
}

//将rss数据解析为RssFeed结构体
func ParseRSS(x []byte) *RssFeed {
    feed := RssFeed{}
    d := xml.NewDecoder(bytes.NewReader(x))
    err := d.Decode(&feed)
    if err != nil {
        fmt.Println("Failed decoding xml")
        os.Exit(0)
    }
    return &feed
}

type RssFeed struct {
    XMLName xml.Name    `xml:"rss"`
    Channel *RssChannel `xml:"channel"`
}
type RssChannel struct {
    XMLName       xml.Name   `xml:"channel"`
    Title         string     `xml:"title"`
    Description   string     `xml:"description"`
    Link          string     `xml:"link"`
    Language      string     `xml:"language"`
    PubDate       string     `xml:"pubDate"`
    LastBuildDate string     `xml:"lastBuildDate"`
    Item          []*RssItem `xml:"item"`
}

type RssItem struct {
    XMLName     xml.Name `xml:"item"`
    Title       string   `xml:"title"`
    Link        string   `xml:"link"`
    PubDate     string   `xml:"pubDate"`
    Creator     string   `xml:"dc:creator"`
    Description string   `xml:"description"`
    Comments    string   `xml:"comments"`
}

发表评论

邮箱地址不会被公开。 必填项已用*标注