Exemple #1
0
// showComments print comment list.
func showComments(auth string, id string) {
	req, err := http.NewRequest("GET", "https://code.google.com/feeds/issues/p/"+project+"/issues/"+id+"/comments/full", nil)
	if err != nil {
		log.Fatal("failed to get comments:", err)
	}
	req.Header.Set("Authorization", "GoogleLogin "+auth)
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		log.Fatal("failed to get comments:", err)
	}
	defer res.Body.Close()
	if res.StatusCode != 200 {
		log.Fatal("failed to authenticate:", res.Status)
	}
	var feed Feed
	err = xml.Unmarshal(res.Body, &feed)
	if err != nil {
		log.Fatal("failed to get comments:", err)
	}
	for _, entry := range feed.Entry {
		doc, err := html.Parse(strings.NewReader(entry.Content))
		if err != nil {
			log.Fatal("failed to parse xml:", err)
		}
		text, err := dump(doc)
		if err != nil {
			log.Fatal("failed to parse xml:", err)
		}
		println(entry.Title, "\n", text)
	}
}
Exemple #2
0
func parseHTML(r io.Reader, np int) {

	doc, err := html.Parse(r)

	if err != nil {
		fmt.Println("Error occurred in parsing HTML!")
		return
	}

	var f func(*html.Node)

	i := 0
	max_stories := np

	f = func(n *html.Node) {
		if i == max_stories {
			return
		}

		if n.Type == html.ElementNode && n.Data == "td" {
			if len(n.Attr) != 0 {
				if n.Attr[0].Key == "class" && n.Attr[0].Val == "title" {
					printBox(n.Child[0].Data)
					i++
				}
			}
		}

		for _, c := range n.Child {
			f(c)
		}
	}

	f(doc)
}
Exemple #3
0
func GetTrendingRepositories(client *http.Client) ([]Repository, os.Error) {
	resp, e := client.Get("https://www.github.com/explore")
	if e != nil {
		return nil, e
	}
	rootnode, e := html.Parse(resp.Body)
	if e != nil {
		return nil, e
	}

	list := make([]Repository, 0, 5)
	listnode := findNextNodeWithClass(rootnode, "ranked-repositories")
	for _, item := range listnode.Child {
		repo := findNextNodeWithTag(item, "h3")
		if repo != nil && !hasClass("last", item) {
			owner := repo.Child[1].Child[0].Data
			name := repo.Child[3].Child[0].Data
			list = append(list, Repository{
				User: owner,
				Name: name,
			})
		}
	}
	return list, nil

}
Exemple #4
0
func TestSomething(t *testing.T) {
	f, err := os.Open("test-data/html5.html", os.O_RDONLY, 0)
	if err != nil {
		t.Errorf("Unable to open file: %v", err)
	}
	defer f.Close()

	doc, err := html.Parse(f)
	if err != nil {
		t.Errorf("Error parsing html:- %v", err)
	}
	var fn func(*html.Node, int)
	fn = func(n *html.Node, i int) {
		switch n.Type {
		default:
			fmt.Println(i, "Unexpected type")
		case html.TextNode:
			fmt.Println(i, "Text Node", n.Data, n.Parent)
		case html.DocumentNode:
			fmt.Println(i, "Document Node")
		case html.ElementNode:
			fmt.Println(i, "Element Node", n.Data, n.Parent)
		case html.CommentNode:
			fmt.Println(i, "Element Node")
		}
		for _, c := range n.Child {
			fn(c, i+1)
		}
	}
	fn(doc, 0)
}
Exemple #5
0
func NewHTMLScrubbedReader(reader io.Reader) (*HTMLScrubbedReader, os.Error) {
	node, err := html.Parse(reader)
	if err != nil {
		return nil, err
	}
	buf := bytes.NewBufferString("")
	this := &HTMLScrubbedReader{node, buf}
	this.parse(node, buf)
	return this, nil
}
Exemple #6
0
func main() {
	doc, err = html.Parse(os.Stdin)
	if err != nil {
		fmt.Fprintf(os.Stderr, "findlinks1: %v\n", err)
		os.Exit(1)
	}
	for _, link := range visit(nil, doc) {
		fmt.Println(link)
	}
}
Exemple #7
0
func main() {
	res, err := http.Get("http://ponychan.net/chan/chat")
	if err != nil {
		fmt.Printf("error @ http.Get")
		os.Exit(1)
	}

	parse, err := html.Parse(res.Body)
	if err != nil {
		fmt.Printf("error @ html.parse")
		os.Exit(1)
	}
	threads := buildThreads(parse)
	for ix := 0; ix < len(threads); ix++ {
		fmt.Printf("id: %s Author: %s / %s \n Subj: %s \n-----\n", threads[ix].ThreadNo, threads[ix].Author.Name, threads[ix].Author.Trip, threads[ix].Subject)
	}
}
Exemple #8
0
func main() {
	res, _, err := http.Get("http://www.mspaintadventures.com/?s=6&p=005366")
	if err != nil {
		fmt.Printf("Error detected as %s", err)
		return
	}

	z, err := html.Parse(res.Body)
	if err != nil {
		fmt.Printf("Error while parsing: %v", err)
		return
	}

	foundSpoiler := false

	var f func(*html.Node)
	f = func(n *html.Node) {
		if n.Type == html.ElementNode && n.Data == "div" && len(n.Attr) != 0 {
			//Check to see if it's the spoiler div
			at0 := n.Attr[0]
			if at0.Key == "class" && at0.Val == "spoiler" {
				//It's the spoiler div, store the stuff.
				fmt.Println("Found a spoiler div!")
				//storeLog(n)
				saveLogT(n)
				foundSpoiler = true
			}
		} else if n.Type == html.ElementNode && n.Data == "table" && len(n.Attr) != 0 {
			if n.Attr[0].Key == "width" && n.Attr[1].Val == "90%" {
				fmt.Println("I found what looks like the table")
			}
		}
		for _, c := range n.Child {
			f(c)
		}
	}
	f(z)
}