Last active
December 9, 2015 03:08
-
-
Save clee/c8f36ed935ea8882ee02 to your computer and use it in GitHub Desktop.
parsing newznab results in go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"encoding/json" | |
) | |
type NewznabAttr struct { | |
Name string | |
Value string | |
} | |
type Attr struct { | |
NewznabAttr `json:"@attributes"` | |
} | |
type Item struct { | |
Title string | |
Author string | |
Link string | |
Attr []Attr | |
} | |
type ItemList struct { | |
Item []Item | |
} | |
type Feed struct { | |
Channel ItemList | |
} | |
func main() { | |
var c Feed | |
f, err := ioutil.ReadFile("oznzb-api.json") | |
if err != nil { | |
panic(err) | |
} | |
err = json.Unmarshal(f, &c) | |
if err != nil { | |
panic(err) | |
} | |
il := c.Channel.Item | |
if len(il) != 100 { | |
panic("ItemList is bullshit!") | |
} | |
for i := 0; i < 5; i++ { | |
fmt.Printf("result: %+v\n", il[i]) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"encoding/xml" | |
) | |
type NewznabAttr struct { | |
Name string `xml:"name,attr"` | |
Value string `xml:"value,attr"` | |
} | |
type Item struct { | |
Title string `xml:"title"` | |
Author string `xml:"author"` | |
Link string `xml:"link"` | |
Attrs []NewznabAttr `xml:"http://www.newznab.com/DTD/2010/feeds/attributes/ attr"` | |
} | |
type Feed struct { | |
XMLName xml.Name `xml:"rss"` | |
Items []Item `xml:"channel>item"` | |
} | |
func main() { | |
var c Feed | |
f, err := ioutil.ReadFile("api.xml") | |
if err != nil { | |
panic(err) | |
} | |
err = xml.Unmarshal(f, &c) | |
if err != nil { | |
panic(err) | |
} | |
il := c.Items | |
if len(il) != 100 { | |
panic("ItemList is bullshit!") | |
} | |
for i := 0; i < 5; i++ { | |
fmt.Printf("result: %+v\n", il[i]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment