2019-06-23 04:21:30 +02:00
|
|
|
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2019-06-22 21:51:07 +02:00
|
|
|
import http
|
|
|
|
import json
|
|
|
|
import sync
|
|
|
|
import time
|
|
|
|
|
|
|
|
const (
|
|
|
|
NR_THREADS = 8
|
|
|
|
)
|
|
|
|
|
|
|
|
struct Story {
|
|
|
|
title string
|
2019-06-24 17:16:01 +02:00
|
|
|
url string
|
2019-06-22 21:51:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Fetcher {
|
|
|
|
mut:
|
2019-06-23 15:36:24 +02:00
|
|
|
mu sync.Mutex
|
|
|
|
ids []int
|
|
|
|
cursor int
|
|
|
|
list_id int
|
2019-06-22 21:51:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (f mut Fetcher) fetch() {
|
|
|
|
for {
|
|
|
|
f.mu.lock()
|
|
|
|
if f.cursor >= f.ids.len {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
id := f.ids[f.cursor]
|
|
|
|
f.cursor++
|
|
|
|
f.mu.unlock()
|
2019-07-29 19:18:26 +02:00
|
|
|
resp := http.get('https://hacker-news.firebaseio.com/v0/item/${id}.json') or {
|
|
|
|
println('failed to fetch data from /v0/item/${id}.json')
|
|
|
|
exit(1)
|
|
|
|
}
|
2019-06-22 21:51:07 +02:00
|
|
|
story := json.decode(Story, resp) or {
|
2019-06-23 10:41:42 +02:00
|
|
|
println('failed to decode a story')
|
|
|
|
exit(1)
|
2019-06-22 21:51:07 +02:00
|
|
|
}
|
2019-06-23 15:36:24 +02:00
|
|
|
f.mu.lock()
|
|
|
|
f.list_id++
|
|
|
|
cursor := f.list_id
|
|
|
|
f.mu.unlock()
|
|
|
|
println('#$cursor) $story.title | $story.url')
|
2019-06-22 21:51:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetches top HN stories in 8 coroutines
|
|
|
|
fn main() {
|
2019-07-29 19:18:26 +02:00
|
|
|
resp := http.get('https://hacker-news.firebaseio.com/v0/topstories.json') or {
|
|
|
|
println('failed to fetch data from /v0/topstories.json')
|
|
|
|
return
|
|
|
|
}
|
2019-06-22 21:51:07 +02:00
|
|
|
ids := json.decode( []int, resp) or {
|
2019-07-29 19:18:26 +02:00
|
|
|
println('failed to decode topstories.json')
|
2019-06-22 21:51:07 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
fetcher := &Fetcher{ids: ids}
|
|
|
|
for i := 0; i < NR_THREADS; i++ {
|
|
|
|
go fetcher.fetch()
|
|
|
|
}
|
2019-06-27 19:02:47 +02:00
|
|
|
time.sleep(5)
|
2019-06-22 21:51:07 +02:00
|
|
|
}
|
|
|
|
|