-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
135 lines (108 loc) · 2.92 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
)
type curiouscatResponseT struct {
UserData struct {
Id int `json:"id"`
Username string `json:"username"`
Answers int `json:"answers"`
} `json:"userData"`
Posts []struct {
Type string `json:"type"`
Post struct {
Id int `json:"id"`
Timestamp int `json:"timestamp"`
SecondsElapsed int `json:"seconds_elapsed"`
Comment string `json:"comment"`
Reply string `json:"reply"`
} `json:"post"`
}
}
var (
url string
client *http.Client
)
func main() {
username := flag.String("username", "", "Nome do usuário no CuriousCat")
limit := flag.Int("limit", 30, "Limite máximo de publicações para exibir (0 = ilimitado)")
flag.Parse()
if flag.NFlag() == 0 {
flag.Usage()
os.Exit(0)
}
fmt.Println("Usuário selecionado:", *username)
fmt.Printf("- -\n")
url = fmt.Sprintf("https://curiouscat.live/api/v2.1/profile?username=%s", *username)
client = http.DefaultClient
client.Timeout = time.Second * 5
defer client.CloseIdleConnections()
readPosts := 0
done := false
maxTimestamp := 0
var err error
for {
if done {
break
}
posts := getPosts(maxTimestamp)
var curiouscatResponse *curiouscatResponseT
err = json.Unmarshal(posts, &curiouscatResponse)
if err != nil {
fmt.Println("Erro fazendo parse da resposta da API:", err)
done = true
continue
}
if len(curiouscatResponse.Posts) == 0 {
if readPosts == 0 {
fmt.Printf("Puxa, o usuário %s não possui nenhuma pergunta publicada!\n", *username)
} else {
fmt.Printf("Nenhum post a mais para carregar!\n")
}
done = true
continue
}
// A partir da primeira chamada para API, o max_timestamp da próxima requisição
// precisa ser baseado no primeiro timestamp lido
maxTimestamp = curiouscatResponse.Posts[0].Post.Timestamp - 1
for _, post := range curiouscatResponse.Posts {
if readPosts == *limit && *limit > 0 {
fmt.Printf("Número máximo de posts a exibir foi atingido: %d\n", *limit)
done = true
break
}
readPosts++
fmt.Printf("Data: %s\n", time.Unix(int64(post.Post.Timestamp), 0))
fmt.Printf("Há: %s\n", time.Since(time.Now().Add(time.Second*time.Duration(post.Post.SecondsElapsed)*-1)).Truncate(time.Hour))
fmt.Printf("Pergunta: %s\n", post.Post.Comment)
fmt.Printf("Resposta: %s\n", post.Post.Reply)
fmt.Printf("- -\n")
}
}
fmt.Println("Encerrei.")
os.Exit(0)
}
func getPosts(maxTimestamp int) []byte {
currentUrl := url
if maxTimestamp > 0 {
currentUrl = fmt.Sprintf("%s&max_timestamp=%d", url, maxTimestamp)
}
response, err := client.Get(currentUrl)
if err != nil {
fmt.Println("Erro ao requisitar o serviço:", err)
os.Exit(0)
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println("Erro lendo a resposta da API:", err)
os.Exit(0)
}
return body
}