-
Notifications
You must be signed in to change notification settings - Fork 0
/
listprocessor.go
125 lines (110 loc) · 3.13 KB
/
listprocessor.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
package main
import (
"bufio"
"fmt"
"os"
"strings"
"sync"
)
var pscomplete = false
// list handler to help us kick off some go channels
// we pass a first class function to help route our output
func listHandler(outputHandler func(js string)) {
link := make(chan map[string]string)
results := make(chan string)
wg := new(sync.WaitGroup)
// batches of two... helps us to batch out work, e.g. to throttle
// server requests... two requests per second, IN THEORY!
for w := 0; w <= 2; w++ {
wg.Add(1)
go getJSON(link, results, wg)
}
// Create a link map for output to the output handlers
go func() {
if list == "" {
// Use demo list...
for k, v := range linkmap {
l := make(map[string]string)
l[k] = v
link <- l
}
} else {
// Read all the lines in a file...
file, err := os.Open(list)
if err != nil {
fmt.Fprintf(os.Stderr, "Error with scanner: %s\n", err.Error())
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
l := make(map[string]string)
row := scanner.Text()
var split []string
if strings.Contains(row, "\",\"") {
split = strings.Split(scanner.Text(), "\",\"")
} else if strings.Contains(row, "\", \"") {
split = strings.Split(scanner.Text(), "\", \"")
} else {
split = strings.Split(scanner.Text(), ",")
}
if len(split) != 2 {
fmt.Fprintf(os.Stderr, "ignoring: issue reading string from file: %s\n", scanner.Text())
} else {
l[strings.Trim(split[1], " ")] = strings.Trim(split[0], " ")
link <- l
}
}
if err := scanner.Err(); err != nil {
fmt.Fprintf(os.Stderr, "Error with scanner: %s\n", scanner.Text())
}
}
close(link)
}()
go func() {
wg.Wait()
pscomplete = true
linklen++
close(results)
}()
for js := range results {
outputHandler(js)
}
}
var linklen int
func getJSON(link <-chan map[string]string, results chan<- string, wg *sync.WaitGroup) {
defer wg.Done()
for m := range link {
// k filename, v link...
for k, v := range m {
k = strings.Trim(k, "\"")
v = strings.Trim(v, "\"")
results <- getJSONFromLocal(k, v)
}
}
}
// retrieve a JSON output from HTTPreserve without talking to the server
func httpreserveJSONOutput(link string, filename string) string {
js := getJSONFromLocal(link, filename)
return js
}
// demo linkmap...
var linkmap = map[string]string{
"http://www.taupofest.co.nz/": "nz govt",
"http://www.siac.govt.nz": "nz govt",
"http://www.bbc.co.uk/news": "bbc news",
"http://www.bbc.co.uk/": "bbc home",
"http://www.bbc.co.uk/radio": "bbc radio",
"https://www.moma.org/": "moma",
"http://www.google.com": "",
"http://google.com": "",
"http://www.exponentialdecay.co.uk": "",
"http://www.archive.org": "",
"http://perma.cc": "",
"http://wikipedia.org": "",
"http://www.ers.dol.govt.nz": "",
"http://www.edcouncil.govt.nz": "",
"http://www.med.govt.nz": "",
"http://www.taxpolicy.ird.govt.nz": "",
"http://unitedfuture.org.nz/": "",
"http://unitednz.co.nz/": "",
}