-
Notifications
You must be signed in to change notification settings - Fork 0
/
louvre.go
146 lines (126 loc) · 3.06 KB
/
louvre.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
// Copyright 2018 Serge 'q3k' Bazanski
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted.
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
)
var (
upstream = "http://oodviewer.q3k.me/"
termsUrl = upstream + "terms.json"
termUrl = upstream + "term.json/%s"
flagOutput string
flagParallel int
)
func terms() ([]string, error) {
resp, err := http.Get(termsUrl)
if err != nil {
return nil, fmt.Errorf("could not GET %q: %v", termsUrl, err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("could not read %q: %v", termsUrl, err)
}
tdata := [][]interface{}{}
err = json.Unmarshal(body, &tdata)
if err != nil {
return nil, fmt.Errorf("could not decode: %v", err)
}
res := make([]string, len(tdata))
j := 0
for i, t := range tdata {
s, ok := t[0].(string)
if !ok {
log.Printf("Could not decode %dth element...", i)
continue
}
res[j] = s
j += 1
}
res = res[:j]
return res, nil
}
type term struct {
Entry string `json:"entry"`
Added int `json:"added"`
Author string `json:"author"`
}
func oneTerm(termName string) ([]*term, error) {
termName = url.QueryEscape(termName)
termUrl := fmt.Sprintf(termUrl, termName)
resp, err := http.Get(termUrl)
if err != nil {
return nil, fmt.Errorf("could not GET %q: %v", termUrl, err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("could not read %q: %v", termUrl, err)
}
t := []*term{}
err = json.Unmarshal(body, &t)
if err != nil {
return nil, fmt.Errorf("could not decode: %v", err)
}
return t, nil
}
type termOrError struct {
termName string
err error
terms []*term
}
func worker(jobs chan string, results chan termOrError) {
for j := range jobs {
terms, err := oneTerm(j)
res := termOrError{
termName: j,
err: err,
terms: terms,
}
results <- res
}
}
func init() {
flag.StringVar(&flagOutput, "output", "terms.json", "Where to download all terms.")
flag.IntVar(&flagParallel, "parallel", 32, "How many concurrent connections to use.")
flag.Parse()
}
func main() {
trms, err := terms()
if err != nil {
log.Fatalf("Could not download terms: %v", err)
}
log.Printf("Server has %d terms.\n", len(trms))
jobs := make(chan string, len(trms))
for _, t := range trms {
jobs <- t
}
results := make(chan termOrError, len(trms))
allTerms := make(map[string][]*term)
for w := 0; w < flagParallel; w++ {
go worker(jobs, results)
}
for i := 0; i < len(trms); i++ {
res := <-results
if res.err != nil {
log.Printf("Could not get %q: %v", res.termName, res.err)
continue
}
allTerms[res.termName] = res.terms
}
log.Printf("Got %d terms.\n", len(allTerms))
outData, err := json.Marshal(allTerms)
if err != nil {
log.Fatalf("Could not marshal JSON: %v\n", err)
}
err = ioutil.WriteFile(flagOutput, outData, 0644)
if err != nil {
log.Fatalf("Could not write to %q: %v\n", flagOutput, err)
}
log.Printf("Wrote %d terms to %q.\n", len(allTerms), flagOutput)
}