-
Notifications
You must be signed in to change notification settings - Fork 7
/
random_article.go
150 lines (132 loc) · 2.9 KB
/
random_article.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"fmt"
"io"
"log"
"math/rand"
"net/http"
"regexp"
"sort"
"strings"
)
const (
SITE = "https://github.com"
SITE_RAW = "https://raw.githubusercontent.com"
SITE_PATH = "/helioloureiro/canalunixloadon/tree/master/pautas"
)
func curl(url string) string {
resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
body, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
log.Fatal(err)
}
fmt.Println("status_code:", resp.StatusCode)
if resp.StatusCode != 200 {
return ""
}
return string(body)
}
func print(i ...interface{}) {
fmt.Println(i)
}
func grep(pattern, text string) bool {
matched, err := regexp.MatchString(pattern, text)
if err != nil {
return false
}
if matched {
return true
}
return false
}
func sed(old, new, text string) string {
re := regexp.MustCompile(old)
return re.ReplaceAllString(text, new)
}
func revsort(palavras []string) {
sort.Sort(sort.Reverse(sort.StringSlice(palavras)))
}
func main() {
fmt.Println("random articles")
content := curl(SITE + SITE_PATH)
lines := strings.Split(content, "\n")
var listaPautas []string
for _, line := range lines {
// print(line)
if grep("repo-content-pjax-container", line) {
// print(line)
line = sed(".*href=\"", "", line)
line = sed("\".*", "", line)
if grep("[0-9]", line) {
// print(line)
listaPautas = append(listaPautas, line)
}
}
}
getnew := make(chan string, 1)
requestnew := make(chan int, 1)
revsort(listaPautas)
ultimaPauta := listaPautas[0]
ultimaPauta = sed("/blob", "", ultimaPauta)
print("Ultima:" + ultimaPauta)
go func() {
// print("link1: " + SITE_RAW + ultimaPauta)
// print("link2: " + "https://raw.githubusercontent.com/helioloureiro/canalunixloadon/master/pautas/20210911.md")
conteudoPauta := curl(SITE_RAW + ultimaPauta)
var assuntos []string
for _, line := range strings.Split(conteudoPauta, "\n") {
if grep(`^\*`, line) {
// print(line)
assuntos = append(assuntos, line)
}
}
var nr int
var lidos []int
for {
select {
case <-requestnew:
for {
nr = rand.Intn(len(assuntos))
// print("Número randômico:", nr)
temValor := false
for _, v := range lidos {
if nr == v {
// print("Valor já existe:", nr)
temValor = true
}
}
if !temValor {
break
}
}
lidos = append(lidos, nr)
getnew <- assuntos[nr]
}
}
}()
for {
var entradaDados string
fmt.Print("Pressione <Enter> pra novo assunto ou digite \"q\" pra sair:")
size, err := fmt.Scanf("%s", &entradaDados)
if err != nil {
// size == 0: apertou <Enter>
if size != 0 {
// print("error no Scan()")
log.Fatal(err)
}
}
// fmt.Println("Entrada de dados:", entradaDados)
// fmt.Println("tamanho dos dados:", size)
if entradaDados == "q" {
// print("saindo do loop")
break
}
requestnew <- 1
article := <-getnew
print(article)
}
}