hnbot/hn.go

108 lines
2.6 KiB
Go
Raw Permalink Normal View History

2023-04-16 15:14:44 +00:00
package main
import (
"encoding/json"
"errors"
2023-04-16 15:14:44 +00:00
"fmt"
"log"
"net/http"
"regexp"
"strconv"
2023-04-16 15:14:44 +00:00
)
type Story struct {
2023-04-25 00:25:59 +00:00
ID int
2023-04-16 15:14:44 +00:00
By string // username of author
Time int // UNIX timestamp
Descendants int // number of comments
2023-04-25 00:25:59 +00:00
Kids []int
2023-04-16 15:14:44 +00:00
Score int
Title string
Url string
}
2023-04-16 17:51:17 +00:00
var (
2023-04-25 00:30:04 +00:00
HackerNewsUrl = "https://news.ycombinator.com"
2023-04-16 17:51:17 +00:00
HackerNewsFirebaseUrl = "https://hacker-news.firebaseio.com/v0"
2023-04-25 00:30:04 +00:00
HackerNewsLinkRegexp = regexp.MustCompile(`(?:https?:\/\/)?news\.ycombinator\.com\/item\?id=([0-9]+)`)
)
2023-04-16 17:51:17 +00:00
2023-04-25 09:51:12 +00:00
func FetchHackerNewsTopStories() ([]Story, error) {
log.Println("Fetching HN top stories ...")
2023-04-16 15:14:44 +00:00
2023-04-25 09:51:12 +00:00
// API docs: https://github.com/HackerNews/API
2023-04-16 17:51:17 +00:00
url := fmt.Sprintf("%s/topstories.json", HackerNewsFirebaseUrl)
2023-04-16 15:14:44 +00:00
resp, err := http.Get(url)
if err != nil {
2023-04-25 09:51:12 +00:00
err = fmt.Errorf("error fetching HN top stories %w:", err)
return nil, err
2023-04-16 15:14:44 +00:00
}
defer resp.Body.Close()
var ids []int
err = json.NewDecoder(resp.Body).Decode(&ids)
if err != nil {
2023-04-25 09:51:12 +00:00
err = fmt.Errorf("error decoding HN top stories JSON: %w", err)
return nil, err
2023-04-16 15:14:44 +00:00
}
// we are only interested in the first page of top stories
const limit = 30
ids = ids[:limit]
var stories [limit]Story
for i, id := range ids {
2023-04-25 09:51:12 +00:00
story, err := FetchStoryById(id)
if err != nil {
return nil, err
}
2023-04-16 15:14:44 +00:00
stories[i] = story
}
2023-04-25 09:51:12 +00:00
log.Println("Fetching HN top stories ... OK")
2023-04-16 15:14:44 +00:00
// Can't return [30]Story as []Story so we copy the array
2023-04-25 09:51:12 +00:00
return stories[:], nil
2023-04-16 15:14:44 +00:00
}
2023-04-25 09:51:12 +00:00
func FetchStoryById(id int) (Story, error) {
log.Printf("Fetching HN story (id=%d) ...\n", id)
2023-04-16 15:14:44 +00:00
url := fmt.Sprintf("https://hacker-news.firebaseio.com/v0/item/%d.json", id)
resp, err := http.Get(url)
if err != nil {
2023-04-25 09:51:12 +00:00
err = fmt.Errorf("error fetching HN story (id=%d): %w", id, err)
return Story{}, err
2023-04-16 15:14:44 +00:00
}
defer resp.Body.Close()
var story Story
err = json.NewDecoder(resp.Body).Decode(&story)
if err != nil {
2023-04-25 09:51:12 +00:00
err := fmt.Errorf("error decoding HN story JSON (id=%d): %w", id, err)
return Story{}, err
2023-04-16 15:14:44 +00:00
}
2023-04-25 09:51:12 +00:00
log.Printf("Fetching HN story (id=%d) ... OK\n", id)
return story, nil
2023-04-16 15:14:44 +00:00
}
2023-04-16 19:05:10 +00:00
2023-04-25 00:25:59 +00:00
func ParseHackerNewsLink(link string) (int, error) {
match := HackerNewsLinkRegexp.FindStringSubmatch(link)
if len(match) == 0 {
return -1, errors.New("input is not a hacker news link")
}
id, err := strconv.Atoi(match[1])
if err != nil {
2023-04-25 09:51:12 +00:00
return -1, errors.New("integer conversion to string failed")
}
return id, nil
}
2023-04-16 19:05:10 +00:00
func HackerNewsUserLink(user string) string {
return fmt.Sprintf("%s/user?id=%s", HackerNewsUrl, user)
}
func HackerNewsItemLink(id int) string {
return fmt.Sprintf("%s/item?id=%d", HackerNewsUrl, id)
}