-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
95 lines (80 loc) · 2.08 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// go run main.go -subreddit="golang" -limit="10"
// "https://www.reddit.com/r/golang/new/.json?limit=10"
package main
import (
"bufio"
"errors"
"os"
"strings"
"fmt"
"log"
"github.com/go-resty/resty/v2"
"github.com/logrusorgru/aurora"
// https://golang.org/doc/code.html
// $go build first in models/
// Refer to go.mod file also.
Models "steadylearner.com/models"
)
var (
au aurora.Aurora
)
func init() {
au = aurora.NewAurora(true)
}
func handleReadToString(err error) {
if err != nil {
log.Fatalf("reader.ReadString failed with '%s'\n", err)
}
}
func removeNewLine(input string) string {
return strings.TrimSuffix(input, "\n")
}
func main() {
for {
subreddit_reader := bufio.NewReader(os.Stdin)
fmt.Println("Which subreddit you want to scrape?(golang)")
subreddit, err := subreddit_reader.ReadString('\n')
handleReadToString(err)
if subreddit == "\n" {
subreddit = "golang"
}
limit_reader := bufio.NewReader(os.Stdin)
fmt.Println("How many you want?(50)") // [1, 100]
limit, err := limit_reader.ReadString('\n')
handleReadToString(err)
if limit == "\n" {
limit = "50"
}
target := fmt.Sprintf(
"https://www.reddit.com/r/%s/new/.json?limit=%s",
removeNewLine(subreddit),
removeNewLine(limit),
)
// https://godoc.org/github.com/go-resty/resty#Request.SetResult
// https://github.com/go-resty/resty/blob/master/response.go#L59
resp, err := resty.New().R().
SetResult(&Models.Response{}).
Get(target)
if err != nil {
log.Fatal(err)
return
}
// Type conversion to Models.Response so verify ok value here?
if response, ok := resp.Result().(*Models.Response); !ok {
log.Fatal(errors.New("invalid response format"))
} else {
for index, post := range response.Data.Children {
num := index + 1
stdoutLink := fmt.Sprintf("%d. %s (%s)", num, post.Data.Title, au.Blue(post.Data.URL))
fmt.Println(stdoutLink)
}
}
end_reader := bufio.NewReader(os.Stdin)
fmt.Println("\nEnd?([n]/y])")
end, err := end_reader.ReadString('\n')
handleReadToString(err)
if strings.HasPrefix(end, "y") {
break
}
}
}