Moved parameters to standalone fle

This commit is contained in:
Riccardo
2022-07-15 16:18:06 +02:00
parent 61f2fff12f
commit 54b2dda79a
8 changed files with 547 additions and 13 deletions

View File

@@ -2,6 +2,7 @@ package hackernews
import (
"encoding/json"
"fmt"
"hackernewsletter/db"
"io/ioutil"
"log"
@@ -9,8 +10,10 @@ import (
"strings"
)
func GetTopNewsIds() (response []string) {
res, err := http.Get("https://hacker-news.firebaseio.com/v0/topstories.json")
func GetTopNewsIds(url string) (response []string) {
fmt.Println("Retrieving news...")
res, err := http.Get(url)
if err != nil {
log.Fatalln(err)
}
@@ -23,11 +26,15 @@ func GetTopNewsIds() (response []string) {
body_string := string(body)
response = strings.Split(body_string[1:len(body_string)-1], ",")
fmt.Println("Done.")
return response
}
func GetNewsById(id string) (response db.News) {
news_url := "https://hacker-news.firebaseio.com/v0/item/" + id + ".json"
func GetNewsById(id string, url string) (response db.News) {
fmt.Printf("Retrieving data for news with id %v...\n", id)
news_url := strings.ReplaceAll(url, "{ID}", id)
res, err := http.Get(news_url)
if err != nil {
log.Fatalln(err)
@@ -40,5 +47,7 @@ func GetNewsById(id string) (response db.News) {
json.Unmarshal(body, &response)
fmt.Printf("News with id %v retrieved.\n", id)
return response
}