-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
318 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io/ioutil" | ||
|
||
"github.com/PuerkitoBio/goquery" | ||
"github.com/azimut/cli-view/internal/hackernews" | ||
) | ||
|
||
func main() { | ||
file, err := ioutil.ReadFile("/home/sendai/testfield/hn.html") | ||
if err != nil { | ||
panic(err) | ||
} | ||
r := bytes.NewReader(file) | ||
doc, err := goquery.NewDocumentFromReader(r) | ||
if err != nil { | ||
panic(err) | ||
} | ||
comments := hackernews.MakeComments(doc) | ||
fmt.Println(comments[0].Childs[0]) | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package hackernews | ||
|
||
import ( | ||
"strconv" | ||
"time" | ||
|
||
"github.com/PuerkitoBio/goquery" | ||
) | ||
|
||
func MakeComments(doc *goquery.Document) []*Comment { | ||
prev := &Comment{} | ||
var parents []*Comment | ||
var comments []*Comment | ||
doc.Find("table.comment-tree tr.comtr").Each(func(i int, sel *goquery.Selection) { | ||
current := NewComment(sel) | ||
if current.isChildOf(prev) { | ||
prev.Childs = append(prev.Childs, current) | ||
parents = append(parents, prev) | ||
} | ||
if current.isSiblingOf(prev) { | ||
if len(parents) > 0 { | ||
parents[len(parents)-1].Childs = append(parents[len(parents)-1].Childs, current) | ||
} else { | ||
comments = append(comments, current) | ||
} | ||
} | ||
if current.isAncestorOf(prev) { | ||
diff := prev.indent - current.indent | ||
parents = parents[:len(parents)-diff] | ||
if len(parents) > 0 { | ||
parents[len(parents)-1].Childs = append(parents[len(parents)-1].Childs, current) | ||
} else { | ||
comments = append(comments, current) | ||
} | ||
} | ||
prev = current | ||
}) | ||
return comments | ||
} | ||
|
||
func NewComment(sel *goquery.Selection) *Comment { | ||
return &Comment{ | ||
id: commentId(sel), | ||
msg: commentMsg(sel), | ||
//togg: commentTogg(sel), | ||
user: commentUser(sel), | ||
//date: commentDate(sel), | ||
indent: commentIndent(sel), | ||
} | ||
} | ||
|
||
func (current *Comment) isChildOf(other *Comment) bool { | ||
if current.indent > other.indent { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func (current *Comment) isSiblingOf(other *Comment) bool { | ||
if current.indent == other.indent { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func (current *Comment) isAncestorOf(other *Comment) bool { | ||
if current.indent < other.indent { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func commentTogg(sel *goquery.Selection) int { | ||
rawTogg, exists := sel.Find("a.togg").Attr("n") | ||
if !exists { | ||
panic("no toggle n on comment") | ||
} | ||
togg, err := strconv.Atoi(rawTogg) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return togg | ||
} | ||
|
||
func commentIndent(sel *goquery.Selection) int { | ||
rawindent, exists := sel.Find("td.ind").Attr("indent") | ||
if !exists { | ||
panic("no indent for comment") | ||
} | ||
indent, err := strconv.Atoi(rawindent) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return indent | ||
} | ||
|
||
func commentMsg(sel *goquery.Selection) string { | ||
return sel.Find("span.commtext").Text() | ||
} | ||
|
||
func commentId(sel *goquery.Selection) int { | ||
rawid, exists := sel.Attr("id") | ||
if !exists { | ||
panic("comment id not found") | ||
} | ||
id, err := strconv.Atoi(rawid) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return id | ||
} | ||
|
||
func commentUser(sel *goquery.Selection) string { | ||
return sel.Find("a.hnuser").Text() | ||
} | ||
|
||
func commentDate(sel *goquery.Selection) time.Time { | ||
rawdate, exists := sel.Find("span.age").Attr("title") | ||
if !exists { | ||
panic("could not find span.age title") | ||
} | ||
date, err := time.Parse("%-%M-%DT%h:%m:%s", rawdate) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return date | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package hackernews | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/PuerkitoBio/goquery" | ||
) | ||
|
||
func TestMakeComments(t *testing.T) { | ||
file, err := ioutil.ReadFile("/home/sendai/testfield/hn.html") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
r := bytes.NewReader(file) | ||
doc, err := goquery.NewDocumentFromReader(r) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
comments := MakeComments(doc) | ||
if len(comments) != 19 { | ||
t.Errorf("expected 19 root comments got %d", len(comments)) | ||
} | ||
if len(comments[0].Childs) != 4 { | ||
t.Errorf("expected 4 child comments for [0] got %d", len(comments[0].Childs)) | ||
} | ||
if len(comments[0].Childs[0].Childs) != 1 { | ||
t.Errorf("expected 1 child comment for .Childs[0].Childs got %d", len(comments[0].Childs[0].Childs)) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package hackernews | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/PuerkitoBio/goquery" | ||
) | ||
|
||
func NewHeader(doc *goquery.Document) *Op { | ||
return &Op{ | ||
ncomments: getNComments(doc), | ||
title: getTitle(doc), | ||
score: getScore(doc), | ||
user: getUser(doc), | ||
url: getUrl(doc), | ||
} | ||
} | ||
|
||
func getUser(doc *goquery.Document) string { | ||
return doc.Find("td.subtext a.hnuser").Text() | ||
} | ||
|
||
func getDate(doc *goquery.Document) time.Time { | ||
rawdate, exists := doc.Find("td.subtext span.age").Attr("title") | ||
if !exists { | ||
panic("could not find span.age title") | ||
} | ||
date, err := time.Parse("%-%M-%DT%h:%m:%s", rawdate) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return date | ||
} | ||
|
||
func getNComments(doc *goquery.Document) int { | ||
rawn := doc.Find("td.subtext a").Last().Text() | ||
n, err := strconv.Atoi(rawn) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return n | ||
} | ||
|
||
func getTitle(doc *goquery.Document) string { | ||
anchor, exists := doc.Find("tr#pagespace").Attr("title") | ||
if !exists { | ||
panic("no title") | ||
} | ||
return anchor | ||
} | ||
|
||
func getUrl(doc *goquery.Document) string { | ||
url, exists := doc.Find("a.storylink").Attr("href") | ||
if !exists { | ||
panic("no story href") | ||
} | ||
return url | ||
} | ||
|
||
func getScore(doc *goquery.Document) int { | ||
rawscore := doc.Find("span.score").Text() | ||
rawscore = strings.TrimRight(rawscore, " points") | ||
score, err := strconv.Atoi(rawscore) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return score | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package hackernews | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/PuerkitoBio/goquery" | ||
) | ||
|
||
func TestNewHeader(t *testing.T) { | ||
file, err := ioutil.ReadFile("/home/sendai/testfield/hn.html") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
r := bytes.NewReader(file) | ||
doc, err := goquery.NewDocumentFromReader(r) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
header := NewHeader(doc) | ||
if header.url != "https://www.newyorker.com/news/news-desk/the-red-warning-light-on-richard-bransons-space-flight" { | ||
t.Errorf("invalid URL") | ||
} | ||
if header.score != 312 { | ||
t.Errorf("invalid score, expected 312") | ||
} | ||
if header.user != "zlsa" { | ||
t.Errorf("invalid user, expected \"zlsa\"") | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package hackernews | ||
|
||
import "time" | ||
|
||
type Op struct { | ||
url string | ||
title string | ||
score int | ||
ncomments int | ||
user string | ||
date time.Time | ||
} | ||
|
||
type Comment struct { | ||
id int | ||
msg string | ||
Childs []*Comment | ||
indent int | ||
user string | ||
date time.Time | ||
} |