-
Notifications
You must be signed in to change notification settings - Fork 1
/
indexugrad.go
70 lines (63 loc) · 1.46 KB
/
indexugrad.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
package main
import (
"fmt"
"log"
"regexp"
"strings"
"sync"
archive "github.com/d4l3k/go-internetarchive"
"github.com/hypersleep/easyssh"
"github.com/urfave/cli"
)
const SSHTimeout = 60
func indexUGrad(c *cli.Context) error {
ssh := &easyssh.MakeConfig{
User: c.String("user"),
Server: c.String("server"),
// Optional key or Password without either we try to contact your agent SOCKET
Key: "/.ssh/id_rsa",
Port: "22",
}
// Call Run method with command you want to run on remote server.
response, _, _, err := ssh.Run(`find /home/c/cs*/public_html -name "*.html" -maxdepth 2`, SSHTimeout)
// Handle errors
if err != nil {
return err
}
urls := make(chan string)
var wg sync.WaitGroup
for i := 0; i < 8; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for url := range urls {
resp, err := archive.Snapshot(url)
if err != nil {
log.Println(err)
break
}
log.Println(resp)
}
}()
}
for _, line := range strings.Split(response, "\n") {
if url, ok := ugradPathToHTTP(line); ok {
urls <- url
}
}
close(urls)
wg.Wait()
return nil
}
var pathRegexp = regexp.MustCompile("^/home/c/(cs\\w+)/public_html/(.*)$")
func ugradPathToHTTP(path string) (string, bool) {
path = strings.TrimSpace(path)
if len(path) == 0 {
return "", false
}
matches := pathRegexp.FindStringSubmatch(path)
if len(matches) != 3 {
return "", false
}
return fmt.Sprintf("https://www.ugrad.cs.ubc.ca/~%s/%s", matches[1], matches[2]), true
}