Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 2d47d48c65
Fetching contributors…

Cannot retrieve contributors at this time

file 108 lines (77 sloc) 1.473 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
package main

import (
"fmt"
"http"
"html"
"io"
"flag"
)

func printLenLine(str string) {
l_s := len(str)

for i := 0; i < l_s+2; i++ {
fmt.Print("_")
}

}

func printBox(str string) {
printLenLine(str)

fmt.Println("")
fmt.Println("")

fmt.Print("| ")
fmt.Print(str)
fmt.Print(" |")

fmt.Println("")

printLenLine(str)

fmt.Println("")
}

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)
}

func catHN(URL string, np int) {
res, url, error := http.Get(URL)

if error == nil {
parseHTML(res.Body, np)

} else {
fmt.Printf("%s\n", error)

}
}

func parseArgs() (string, int) {
URL := "http://news.ycombinator.com/"

newposts := flag.Bool("new", false, "Show new posts")
numposts := flag.Int("posts", 5, "How many posts to show")

  flag.Parse()

if *newposts {
fmt.Println("Newest Posts\n")
URL = "http://news.ycombinator.com/newest"

} else {
fmt.Println("Top Posts\n")
}

return URL, *numposts
}

func main() {
URL, numposts := parseArgs()

catHN(URL, numposts)
}
Something went wrong with that request. Please try again.