Import of top news from Hacker News api

This commit is contained in:
Riccardo
2022-07-15 12:52:46 +02:00
parent 573cd14683
commit 61f2fff12f
5 changed files with 68 additions and 505 deletions

44
hackernews/hackernews.go Normal file
View File

@@ -0,0 +1,44 @@
package hackernews
import (
"encoding/json"
"hackernewsletter/db"
"io/ioutil"
"log"
"net/http"
"strings"
)
func GetTopNewsIds() (response []string) {
res, err := http.Get("https://hacker-news.firebaseio.com/v0/topstories.json")
if err != nil {
log.Fatalln(err)
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatalln(err)
}
body_string := string(body)
response = strings.Split(body_string[1:len(body_string)-1], ",")
return response
}
func GetNewsById(id string) (response db.News) {
news_url := "https://hacker-news.firebaseio.com/v0/item/" + id + ".json"
res, err := http.Get(news_url)
if err != nil {
log.Fatalln(err)
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatalln(err)
}
json.Unmarshal(body, &response)
return response
}