Same changes
This commit is contained in:
61
internal/api/fetch.go
Normal file
61
internal/api/fetch.go
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/mmcdole/gofeed"
|
||||||
|
)
|
||||||
|
|
||||||
|
func FetchPlain(url string) string {
|
||||||
|
response, err := http.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer response.Body.Close()
|
||||||
|
|
||||||
|
body, err := io.ReadAll(response.Body)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
return string(body)
|
||||||
|
}
|
||||||
|
|
||||||
|
func FetchJSON[T any](url string) (T, error) {
|
||||||
|
var result T
|
||||||
|
|
||||||
|
resp, err := http.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
return result, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return result, fmt.Errorf("unexpected status: %s", resp.Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return result, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(body, &result)
|
||||||
|
if err != nil {
|
||||||
|
return result, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func FetchRSS(url string) (ret []gofeed.Item, err error) {
|
||||||
|
fp := gofeed.NewParser()
|
||||||
|
feed, err := fp.ParseURL(url)
|
||||||
|
|
||||||
|
for _, v := range feed.Items {
|
||||||
|
ret = append(ret, *v)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
4
internal/config/conf.sh
Normal file
4
internal/config/conf.sh
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
news=true
|
||||||
|
quote=true
|
||||||
70
internal/config/config.go
Normal file
70
internal/config/config.go
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "embed"
|
||||||
|
"encoding/json"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
//go:embed conf.sh
|
||||||
|
var defaultConfig []byte
|
||||||
|
|
||||||
|
//go:embed wrapper.sh
|
||||||
|
var wrapper string
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
News bool `json:"news"`
|
||||||
|
Quotes bool `json:"quote"`
|
||||||
|
Ramblings bool `json:"ramblings"`
|
||||||
|
Ramblings_Path string `json:"ramblings_path"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewConfig() (c Config) {
|
||||||
|
c.News = true
|
||||||
|
c.Quotes = true
|
||||||
|
c.Ramblings = true
|
||||||
|
ramblingsDir := os.Getenv("XDG_DOCUMENTS_DIR")
|
||||||
|
if ramblingsDir == "" {
|
||||||
|
ramblingsDir = filepath.Join(os.Getenv("HOME"), "Documents")
|
||||||
|
}
|
||||||
|
c.Ramblings_Path = filepath.Join(ramblingsDir, "wire")
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Config) ReadConfig() {
|
||||||
|
configDir := os.Getenv("XDG_CONFIG_HOME")
|
||||||
|
if configDir == "" {
|
||||||
|
configDir = filepath.Join(os.Getenv("HOME"), ".config")
|
||||||
|
}
|
||||||
|
|
||||||
|
fp := filepath.Join(configDir, "wire", "conf.sh")
|
||||||
|
|
||||||
|
dir := filepath.Dir(fp)
|
||||||
|
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := os.ReadFile(fp)
|
||||||
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
if err := os.WriteFile(fp, defaultConfig, 0644); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
out, err := exec.Command("sh", "-c", wrapper, "--", fp).Output()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.Unmarshal(out, c); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
3
internal/config/wrapper.sh
Normal file
3
internal/config/wrapper.sh
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
. $1
|
||||||
|
printf '{"news":%s,"quote":%s,"ramblings":%s,"ramblings_path":"%s"}\n' "$news" "$quote" "$ramblings" "$ramblings_path"
|
||||||
29
main.go
29
main.go
@@ -6,8 +6,8 @@ import (
|
|||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"wire/internal/api"
|
||||||
"github.com/mmcdole/gofeed"
|
"wire/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed version
|
//go:embed version
|
||||||
@@ -15,7 +15,10 @@ var version string
|
|||||||
|
|
||||||
type Data struct {
|
type Data struct {
|
||||||
Version string
|
Version string
|
||||||
|
Quote template.HTML
|
||||||
|
Ramblings string
|
||||||
Articles []Article
|
Articles []Article
|
||||||
|
Config config.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
type Article struct {
|
type Article struct {
|
||||||
@@ -23,6 +26,12 @@ type Article struct {
|
|||||||
Link string
|
Link string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Quote struct {
|
||||||
|
Q string `json:"q"`
|
||||||
|
A string `json:"a"`
|
||||||
|
H string `json:"h"`
|
||||||
|
}
|
||||||
|
|
||||||
var data Data = Data{
|
var data Data = Data{
|
||||||
Version: version,
|
Version: version,
|
||||||
}
|
}
|
||||||
@@ -30,19 +39,23 @@ var data Data = Data{
|
|||||||
func main() {
|
func main() {
|
||||||
http.HandleFunc("/", newsHandler)
|
http.HandleFunc("/", newsHandler)
|
||||||
|
|
||||||
fp := gofeed.NewParser()
|
cfg := config.NewConfig()
|
||||||
feed, _ := fp.ParseURL("https://rss.nytimes.com/services/xml/rss/nyt/US.xml")
|
cfg.ReadConfig()
|
||||||
|
data.Config = cfg
|
||||||
|
|
||||||
for i, v := range feed.Items {
|
items, err := api.FetchRSS("https://rss.nytimes.com/services/xml/rss/nyt/US.xml")
|
||||||
|
for _, v := range items {
|
||||||
data.Articles = append(data.Articles, Article{
|
data.Articles = append(data.Articles, Article{
|
||||||
Title: v.Title,
|
Title: v.Title,
|
||||||
Link: v.Link,
|
Link: v.Link,
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
if i == 9 {
|
quote, err := api.FetchJSON[[]Quote]("https://zenquotes.io/api/today")
|
||||||
break
|
if err != nil {
|
||||||
}
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
data.Quote = template.HTML(quote[0].H)
|
||||||
|
|
||||||
fmt.Println("wire: starting server at http://127.0.0.1:1337")
|
fmt.Println("wire: starting server at http://127.0.0.1:1337")
|
||||||
log.Fatal(http.ListenAndServe("127.0.0.1:1337", nil))
|
log.Fatal(http.ListenAndServe("127.0.0.1:1337", nil))
|
||||||
|
|||||||
@@ -6,12 +6,26 @@
|
|||||||
<title>Wire News</title>
|
<title>Wire News</title>
|
||||||
</head>
|
</head>
|
||||||
<h1>Wire {{.Version}}</h1>
|
<h1>Wire {{.Version}}</h1>
|
||||||
|
|
||||||
|
{{if .Config.News}}
|
||||||
<h2>Latest News</h2>
|
<h2>Latest News</h2>
|
||||||
<ul>
|
<ul>
|
||||||
{{range .Articles}}
|
{{range .Articles}}
|
||||||
<li><a href="{{.Link}}">{{.Title}}</a></li>
|
<li><a href="{{.Link}}">{{.Title}}</a></li>
|
||||||
{{end}}
|
{{end}}
|
||||||
</ul>
|
</ul>
|
||||||
|
{{end}}
|
||||||
|
|
||||||
|
{{if .Config.Quotes}}
|
||||||
|
<h2>Fortification of the Mind</h2>
|
||||||
|
{{.Quote}}
|
||||||
|
{{end}}
|
||||||
|
|
||||||
|
{{if .Config.Ramble}}
|
||||||
|
<h2>Schizo Ramblings</h2>
|
||||||
|
{{.Ramblings}}
|
||||||
|
{{end}}
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user