Add News widget and integrate NewsAPI for live updates

This commit is contained in:
2025-12-29 21:19:05 +01:00
parent d638a8ae97
commit 9b5afc3d7c
9 changed files with 260 additions and 38 deletions

77
apis/newsapi/main.go Normal file
View File

@@ -0,0 +1,77 @@
package news
import (
"ArinDash/config"
"encoding/json"
"io"
"log"
"net/http"
"strings"
)
const apiBaseURL = "https://newsapi.org/v2/top-headlines"
type configFile struct {
News newsConfig
}
type newsConfig struct {
ApiKey string
Sources string
}
type News struct {
Status string `json:"status"`
TotalResults int `json:"totalResults"`
Articles []Article `json:"articles"`
}
type Article struct {
Source Source `json:"source"`
Author string `json:"author"`
Title string `json:"title"`
Description string `json:"description"`
URL string `json:"url"`
URLToImage string `json:"urlToImage"`
PublishedAt string `json:"publishedAt"`
Content string `json:"content"`
}
type Source struct {
ID string `json:"id"`
Name string `json:"name"`
}
func FetchNews() News {
cfg := &configFile{}
config.LoadConfig(cfg)
client := &http.Client{}
req, err := http.NewRequest("GET", apiBaseURL+"?sources="+cfg.News.Sources+"&pageSize=100&apiKey="+cfg.News.ApiKey, nil)
if err != nil {
panic(err)
}
resp, err := client.Do(req)
if err != nil {
return News{
Status: err.Error(),
}
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return News{
Status: err.Error(),
}
}
news := News{}
err = json.Unmarshal([]byte(strings.ReplaceAll(strings.ReplaceAll(string(respBody), "\r", ""), "\u00A0", "")), &news)
if err != nil {
log.Fatal(err)
}
return news
}

View File

@@ -4,7 +4,6 @@ import (
"ArinDash/config"
"encoding/json"
"io"
"log"
"net/http"
)
@@ -29,6 +28,7 @@ type Warning struct {
Code []string `json:"code"`
Reference string `json:"reference"`
Info []Info `json:"info"`
Errormsg string `json:"errormsg"`
}
type Info struct {
@@ -53,33 +53,45 @@ func FetchWarnings() []Warning {
}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
return append(make([]Warning, 0), Warning{
Errormsg: err.Error(),
})
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
return append(make([]Warning, 0), Warning{
Errormsg: err.Error(),
})
}
warnings := make([]Warning, 0)
err = json.Unmarshal(respBody, &warnings)
if err != nil {
log.Fatal(err)
return append(make([]Warning, 0), Warning{
Errormsg: err.Error(),
})
}
result := make([]Warning, 0)
for _, warning := range warnings {
req, err := http.NewRequest("GET", apiBaseURL+"/warnings/"+warning.Id+".json", nil)
if err != nil {
panic(err)
return append(make([]Warning, 0), Warning{
Errormsg: err.Error(),
})
}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
return append(make([]Warning, 0), Warning{
Errormsg: err.Error(),
})
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
return append(make([]Warning, 0), Warning{
Errormsg: err.Error(),
})
}
warning := Warning{}
err = json.Unmarshal(respBody, &warning)

View File

@@ -3,21 +3,22 @@ package openWeatherMap
import "time"
type CurrentWeather struct {
Base string `json:"base"`
Visibility int `json:"visibility"`
Dt int `json:"dt"`
Timezone int `json:"timezone"`
Id int `json:"id"`
Name string `json:"name"`
Cod int `json:"cod"`
Coordinates Coordinates `json:"coord"`
Weather []Weather `json:"weather"`
Wind Wind `json:"wind"`
Clouds Clouds `json:"clouds"`
Rain Rain `json:"rain"`
Snow Snow `json:"snow"`
Sys Sys `json:"sys"`
Main Main `json:"main"`
Base string `json:"base"`
Visibility int `json:"visibility"`
Dt int `json:"dt"`
Timezone int `json:"timezone"`
Id int `json:"id"`
Name string `json:"name"`
Cod int `json:"cod"`
Coordinates Coordinates `json:"coord"`
Weather []Weather `json:"weather"`
Wind Wind `json:"wind"`
Clouds Clouds `json:"clouds"`
Rain Rain `json:"rain"`
Snow Snow `json:"snow"`
Sys Sys `json:"sys"`
Main Main `json:"main"`
ErrorMessage string
}
type Coordinates struct {

View File

@@ -4,7 +4,6 @@ import (
"ArinDash/config"
"encoding/json"
"io"
"log"
"net/http"
)
@@ -27,16 +26,22 @@ func FetchCurrentWeather() CurrentWeather {
req, err := http.NewRequest("GET", apiBaseURL+"?id="+cfg.OpenWeatherMap.LocationId+"&units=metric&lang=en&APPID="+cfg.OpenWeatherMap.ApiKey, nil)
if err != nil {
panic(err)
return CurrentWeather{
ErrorMessage: err.Error(),
}
}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
return CurrentWeather{
ErrorMessage: err.Error(),
}
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
return CurrentWeather{
ErrorMessage: err.Error(),
}
}
err = json.Unmarshal(respBody, currentWeather)

View File

@@ -4,7 +4,6 @@ import (
"ArinDash/config"
"encoding/json"
"io"
"log"
"net/http"
"strings"
)
@@ -50,7 +49,7 @@ func (ph *PiHConnector) do(method string, endpoint string, body io.Reader) []byt
req, err := http.NewRequest(method, requestString, body)
if err != nil {
log.Fatal(err)
return make([]byte, 0)
}
req.Header.Add("X-FTL-SID", ph.Session.SID)
@@ -58,13 +57,13 @@ func (ph *PiHConnector) do(method string, endpoint string, body io.Reader) []byt
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
return make([]byte, 0)
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
return make([]byte, 0)
}
return respBody
@@ -82,24 +81,24 @@ func Connect() PiHConnector {
req, err := http.NewRequest("POST", "http://"+cfg.Pihole.Host+"/api/auth", strings.NewReader("{\"password\": \""+cfg.Pihole.Password+"\"}"))
if err != nil {
log.Fatal(err)
panic(err)
}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
panic(err)
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
panic(err)
}
s := &PiHAuth{}
err = json.Unmarshal(respBody, s)
if err != nil {
log.Fatal(err)
panic(err)
}
connector = &PiHConnector{
Host: cfg.Pihole.Host,