LoginSignup
6
6

More than 5 years have passed since last update.

Goでニコニコ生放送のRSSから開場日時や放送開始日時を抽出してみる

Last updated at Posted at 2014-12-21

GAE (Go/Python) もくもく勉強会 in 横浜タネマキ vol.12 で調べた結果のメモ。標準ライブラリだけで行けた。

parsenicoliverss.go
package main

import (
    "encoding/xml"
    "fmt"
    "io/ioutil"
    "net/http"
)

type Item struct {
    XMLName     xml.Name `xml:"item"`
    Title       string   `xml:"title"`
    Description string   `xml:"description"`
    OpenTime    string   `xml:"http://live.nicovideo.jp/ open_time"`
    StartTime   string   `xml:"http://live.nicovideo.jp/ start_time"`
}

type Channel struct {
    XMLName xml.Name `xml:"channel"`
    Title   string   `xml:"title"`
    Items   []Item   `xml:"item"`
}

type Rss struct {
    XMLName xml.Name `xml:"rss"`
    Channel Channel  `xml:"channel"`
}

func main() {
    res, err := http.Get("http://live.nicovideo.jp/rss")
    if err != nil {
        panic(err)
    }
    defer res.Body.Close()
    body, err := ioutil.ReadAll(res.Body)
    if err != nil {
        panic(err)
    }

    r := Rss{}
    if err := xml.Unmarshal(body, &r); err != nil {
        panic(err)
    }
    fmt.Printf("RSSタイトル: %s\n", r.Channel.Title)
    fmt.Println("----------")
    for _, v := range r.Channel.Items {
        fmt.Printf("番組タイトル: %s\n", v.Title)
        fmt.Printf("番組説明文: %s\n", v.Description)
        fmt.Printf("開場日時: %s\n", v.OpenTime)
        fmt.Printf("放送開始日時: %s\n", v.StartTime)
        fmt.Println("----------")
    }
}
6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6