Skip to content

Commit e1b91dc

Browse files
committed
add news bot
0 parents  commit e1b91dc

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed

.gitignore

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# If you prefer the allow list template instead of the deny list, see community template:
2+
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
3+
#
4+
# Binaries for programs and plugins
5+
*.exe
6+
*.exe~
7+
*.dll
8+
*.so
9+
*.dylib
10+
11+
# Test binary, built with `go test -c`
12+
*.test
13+
14+
# Output of the go coverage tool, specifically when used with LiteIDE
15+
*.out
16+
17+
# Dependency directories (remove the comment below to include it)
18+
# vendor/
19+
20+
# Go workspace file
21+
go.work
22+
go.work.sum
23+
24+
# env file
25+
.env
26+
27+
bin/*

go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module github.com/stym06/hackernews-bot
2+
3+
go 1.23
4+
5+
require (
6+
github.com/fatih/color v1.17.0 // indirect
7+
github.com/mattn/go-colorable v0.1.13 // indirect
8+
github.com/mattn/go-isatty v0.0.20 // indirect
9+
golang.org/x/sys v0.18.0 // indirect
10+
)

go.sum

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
2+
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
3+
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
4+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
5+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
6+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
7+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
8+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
9+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
10+
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
11+
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

main.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io"
7+
"log"
8+
"math/rand"
9+
"net/http"
10+
"os/exec"
11+
"runtime"
12+
"time"
13+
14+
"github.com/fatih/color"
15+
)
16+
17+
type HackerStory struct {
18+
Title string `json: "title"`
19+
Url string `json: "url"`
20+
}
21+
22+
func openbrowser(url string) {
23+
var err error
24+
25+
switch runtime.GOOS {
26+
case "linux":
27+
err = exec.Command("xdg-open", url).Start()
28+
case "windows":
29+
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
30+
case "darwin":
31+
err = exec.Command("open", url).Start()
32+
default:
33+
err = fmt.Errorf("unsupported platform")
34+
}
35+
if err != nil {
36+
log.Fatal(err)
37+
}
38+
39+
}
40+
41+
func main() {
42+
rand.Seed(time.Now().UnixNano())
43+
topStoriesUrl := "https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty"
44+
45+
resp, err := http.Get(topStoriesUrl)
46+
47+
if err != nil {
48+
panic(err)
49+
}
50+
51+
defer resp.Body.Close()
52+
53+
body, err := io.ReadAll(resp.Body)
54+
if err != nil {
55+
panic(err)
56+
}
57+
var topStories []int64
58+
err = json.Unmarshal(body, &topStories)
59+
if err != nil {
60+
panic(err)
61+
}
62+
63+
randomStoryId := topStories[rand.Intn(len(topStories))]
64+
65+
randomStoryUrl := fmt.Sprintf("https://hacker-news.firebaseio.com/v0/item/%d.json?print=pretty", randomStoryId)
66+
67+
resp, err = http.Get(randomStoryUrl)
68+
if err != nil {
69+
panic(err)
70+
}
71+
72+
body, err = io.ReadAll(resp.Body)
73+
if err != nil {
74+
panic(err)
75+
}
76+
77+
var hackerStory HackerStory
78+
err = json.Unmarshal(body, &hackerStory)
79+
if err != nil {
80+
panic(err)
81+
}
82+
83+
color.Green(hackerStory.Title)
84+
color.White(hackerStory.Url)
85+
86+
openbrowser(hackerStory.Url)
87+
88+
}

0 commit comments

Comments
 (0)