-
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
7 changed files
with
140 additions
and
21 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
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
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 |
---|---|---|
@@ -1,25 +1,102 @@ | ||
package hackernews | ||
|
||
import ( | ||
"strings" | ||
"errors" | ||
"github.com/caser/gophernews" | ||
"time" | ||
|
||
"github.com/PuerkitoBio/goquery" | ||
"github.com/azimut/cli-view/internal/fetch" | ||
) | ||
|
||
func Fetch(url, ua string, timeout time.Duration) (doc *goquery.Document, err error) { | ||
url, err = effectiveUrl(url) | ||
const NUM_OF_WORKERS = 3 | ||
|
||
func unix2time(t int) time.Time { | ||
return time.Unix(int64(t), 0) | ||
} | ||
|
||
func Fetch(rawUrl, ua string, timeout time.Duration) (doc *Op, err error) { | ||
url, storyId, err := effectiveUrl(rawUrl) | ||
if err != nil { | ||
return nil, err | ||
} | ||
res, err := fetch.Fetch(url, ua, timeout) | ||
client := gophernews.NewClient() | ||
story, err := fetchStory(client, storyId) | ||
if err != nil { | ||
return nil, err | ||
} | ||
doc, err = goquery.NewDocumentFromReader(strings.NewReader(res)) | ||
if story.Type != "story" { | ||
return nil, errors.New("is not a story") | ||
} | ||
// TODO: ncomments | ||
op := Op{ | ||
url: url, | ||
title: story.Title, | ||
score: story.Score, | ||
user: story.By, | ||
date: unix2time(story.Time), | ||
} | ||
// comments := fetchComments(story.Kids) // TODO: error | ||
return &op, nil | ||
} | ||
|
||
func fetchStory(client *gophernews.Client, id int) (*gophernews.Story, error) { | ||
story, err := client.GetStory(id) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return | ||
return &story, nil | ||
} | ||
|
||
func fetchComments(comments []int) (state map[int]Comment) { | ||
storiesCh := make(chan int, 5) | ||
commentsCh := make(chan result, 5) | ||
go sendWork(comments, storiesCh) | ||
spawnWorkers(storiesCh, commentsCh) | ||
collector(storiesCh, commentsCh, state) | ||
return state | ||
} | ||
|
||
func sendWork(ids []int, input chan<- int) { | ||
for _, id := range ids { | ||
input <- id | ||
} | ||
} | ||
|
||
func spawnWorkers(in <-chan int, out chan<- result) { | ||
for i := 0; i < NUM_OF_WORKERS; i++ { | ||
go worker(in, out) | ||
} | ||
} | ||
|
||
type result struct { | ||
comment gophernews.Comment | ||
err error | ||
} | ||
|
||
func worker(stories <-chan int, output chan<- result) { | ||
client := gophernews.NewClient() | ||
for storyId := range stories { | ||
comment, err := client.GetComment(storyId) | ||
if err != nil { | ||
output <- result{err: err} | ||
} | ||
output <- result{comment: comment} | ||
} | ||
} | ||
|
||
func collector(commentsCh chan<- int, responseCh <-chan result, state map[int]Comment) { | ||
for response := range responseCh { | ||
if response.err != nil { | ||
continue | ||
} | ||
c := response.comment | ||
state[c.ID] = Comment{ | ||
id: c.ID, | ||
msg: c.Text, | ||
user: c.By, | ||
kids: c.Kids, | ||
} | ||
for _, id := range c.Kids { | ||
commentsCh <- id | ||
} | ||
} | ||
close(commentsCh) | ||
} |
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 |
---|---|---|
@@ -1,21 +1,31 @@ | ||
package hackernews | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/url" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
func effectiveUrl(rawUrl string) (string, error) { | ||
func effectiveUrl(rawUrl string) (string, int, error) { | ||
uri, err := url.Parse(rawUrl) | ||
if err != nil { | ||
return "", err | ||
return "", 0, err | ||
} | ||
if uri.Host != "news.ycombinator.com" { | ||
return "", errors.New("invalid hostname") | ||
return "", 0, fmt.Errorf("invalid Host: %s", uri.Host) | ||
} | ||
if uri.Path != "/item" { | ||
return "", errors.New("invalid path") | ||
return "", 0, fmt.Errorf("invalid Path: %s", uri.Path) | ||
} | ||
params := strings.Split(uri.RawQuery, "=") | ||
if params[0] != "id" || len(params) != 2 { | ||
return "", 0, fmt.Errorf("invalid RawQuery: %s", params) | ||
} | ||
id, err := strconv.Atoi(params[1]) | ||
if err != nil { | ||
return "", 0, err | ||
} | ||
uri.Scheme = "https" | ||
return uri.String(), nil | ||
return uri.String(), id, nil | ||
} |