-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
179 lines (151 loc) · 3.44 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"bufio"
"crypto/tls"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/mattn/go-xmpp"
"github.com/yhat/scrape"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
var baseURL = "https://www.livecoding.tv/"
var client *xmpp.Client
var jidSuffix = "@livecoding.tv"
var roomSuffix = "@chat.livecoding.tv"
var usernameToFind string
func main() {
// Check for arguments
if len(os.Args) < 3 {
fmt.Print("Did you provide enough arguments? -> " + os.Args[0] + " <username-to-find> <your-username> <your-password> \n")
os.Exit(1)
}
usernameToFind = os.Args[1]
username := os.Args[2]
password := os.Args[3]
liveChannels := getLiveChannels()
startXMPP(username, password, liveChannels)
for {
}
}
func getLiveChannels() []string {
// request and parse the front page
resp, err := http.Get(baseURL + "livestreams/")
if err != nil {
panic(err)
}
root, err := html.Parse(resp.Body)
if err != nil {
panic(err)
}
// define a matcher
matcher := func(n *html.Node) bool {
// must check for nil values
if n.DataAtom == atom.A && n.Parent != nil && n.Parent.Parent != nil {
return scrape.Attr(n.Parent.Parent, "class") == "browse-main-videos--item"
}
return false
}
// grab all channels
var liveChannels []string
channels := scrape.FindAll(root, matcher)
for _, channel := range channels {
link := scrape.Attr(channel, "href")
// links containing "videoes" are not live channels
if !strings.Contains(link, "videos") {
liveChannels = append(liveChannels, link[1:len(link)-1])
}
}
return liveChannels
}
type SavedUser struct {
Room string
Username string
}
var savedUsers []*SavedUser
func saveUser(from string) {
splittedFrom := strings.Split(from, "@chat.livecoding.tv/")
if len(splittedFrom) != 2 {
return
}
s := &SavedUser{
Room: splittedFrom[0],
Username: splittedFrom[1],
}
savedUsers = append(savedUsers, s)
}
func findUsername() {
var rooms []string
for _, u := range savedUsers {
if u.Username == usernameToFind {
rooms = append(rooms, u.Room)
}
}
if len(rooms) > 0 {
fmt.Print(usernameToFind + " was found in following channels: \n")
for _, r := range rooms {
fmt.Print(baseURL + r + "\n")
}
} else {
fmt.Print(usernameToFind + " was not found anywhere :( \n")
}
os.Exit(0)
}
func startXMPP(username, password string, liveChannels []string) {
timeout := time.After(5 * time.Second)
stopFetching := false
options := xmpp.Options{
Host: "xmpp.livecoding.tv:5222",
User: username + jidSuffix,
Password: password,
NoTLS: true,
Debug: false,
Session: false,
TLSConfig: &tls.Config{InsecureSkipVerify: true},
}
client, err := options.NewClient()
if err != nil {
log.Fatal(err)
}
go func() {
for {
if stopFetching {
findUsername()
return
}
chat, err := client.Recv()
if err != nil {
log.Fatal(err)
}
switch v := chat.(type) {
case xmpp.Presence:
saveUser(v.From)
}
}
}()
// Join all live channels to get users
for _, channel := range liveChannels {
client.JoinMUCNoHistory(channel+roomSuffix, username)
}
for {
select {
case <-timeout:
stopFetching = true
return
}
in := bufio.NewReader(os.Stdin)
line, err := in.ReadString('\n')
if err != nil {
continue
}
line = strings.TrimRight(line, "\n")
tokens := strings.SplitN(line, " ", 2)
if len(tokens) == 2 {
client.Send(xmpp.Chat{Remote: tokens[0], Type: "chat", Text: tokens[1]})
}
}
}