-
Notifications
You must be signed in to change notification settings - Fork 0
/
mementoqa.go
263 lines (217 loc) · 5.74 KB
/
mementoqa.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package main
import (
"encoding/json"
ps "github.com/httpreserve/phantomjsscreenshot"
sr "github.com/httpreserve/simplerequest"
"log"
"net/url"
"strconv"
"strings"
"sync"
)
var screenshot = false
var port = "8080"
func init() {
if ps.Hello() {
screenshot = true
}
}
var useragent = "memento-qa/0.0.1"
type timemap struct {
id string
uri string
resolved bool
compliant bool
original string
timegate string
screenshot string
captured bool
}
var memes []timemap
const datelayout = "20060102150405"
func getPotentialURL(archiveurl string, uri string, date string) string {
base := strings.Split(archiveurl, "timemap")[0]
return base + date + "/" + uri
}
const wedoitbase = "http://labs.mementoweb.org/timemap/json/"
const base = "http://timetravel.mementoweb.org/timemap/json/"
func makeTimemap(uri string) string {
return base + uri
}
func recurseInterface(f interface{}) {
var tm timemap
m := f.(map[string]interface{})
for k, v := range m {
switch value := v.(type) {
case string:
v := v.(string)
switch k {
case "uri":
u, err := url.Parse(v)
tm.uri = u.String()
if err != nil {
log.Println("Problem parsing URI sent by time travel. URI will be blank.", v)
tm.uri = ""
}
case "archive_id":
tm.id = v
case "memento_compliant":
if v == "no" {
tm.compliant = false
} else if v == "yes" {
tm.compliant = true
} else {
log.Println("unknown compliance status")
}
case "original_uri":
tm.original = v
case "timegate_uri":
tm.timegate = v
default:
log.Println("unknown API function, check memento version", k, v)
}
case int:
log.Println("int, in json unmarshal interface")
case []interface{}:
//we're expecting this, recurse and decode...
for _, u := range value {
recurseInterface(u)
}
//default:
//we've information we haven't worked with before...
}
}
if tm.uri != "" {
memes = append(memes, tm)
}
}
func manageURIExceptions(oldURI, newURI string) string {
if strings.Contains(oldURI, "http://wayback.archive-it.org/all/") {
s := strings.Split(oldURI, "/all/")[0]
return s + newURI
}
return newURI
}
func addDateURIs(mementos []timemap, url string, date string) {
for i := range mementos {
if mementos[i].uri != "" {
if mementos[i].compliant {
uri := getPotentialURL(mementos[i].uri, url, date)
resp, err := makeSimpleRequest(uri)
if err != nil {
log.Println("error making simplerequest for archival uri")
} else {
tmpURI := resp.GetHeader("Location")
if tmpURI != "" {
mementos[i].uri = manageURIExceptions(uri, tmpURI)
mementos[i].resolved = true
}
}
}
}
}
log.Println("Results for:", len(mementos), "potential mementos")
}
func getTimemap(uri string) ([]timemap, error) {
resp, _ := makeSimpleRequest(uri)
var m interface{}
err := json.Unmarshal([]byte(resp.Data), &m)
if err != nil {
return nil, err
}
recurseInterface(m)
return memes, nil
}
func makeSimpleRequest(uri string) (sr.SimpleResponse, error) {
req, err := sr.Create(sr.GET, uri)
if err != nil {
return sr.SimpleResponse{}, err
}
req.NoRedirect(true)
req.Accept("*/*")
req.Agent(useragent)
resp, err := req.Do()
if err != nil {
return sr.SimpleResponse{}, err
}
return resp, nil
}
func makeScreenshots(m []timemap) {
log.Println("making screenshots")
var count int
//make archived screenshots...
var err error
for x := range m {
if m[x].uri != "" && m[x].compliant && m[x].resolved != false && screenshot {
m[x].screenshot, err = ps.GrabScreenshot(m[x].uri)
if err != nil {
log.Println("error creating screenshot for ", m[x].uri, err)
} else {
count++
m[x].captured = true
}
}
}
log.Println(strconv.Itoa(count), "screenshots made")
}
func maketable(mementos []timemap) string {
var tab string
var noncompliant []string
var others []string
tab = tab + "<table><tr><th>uri</th><th>snapshot</th></tr>"
for i := range mementos {
/*
id string
uri string
compliant bool
original string
timegate string
screenshot string
*/
if mementos[i].compliant && mementos[i].resolved && mementos[i].captured != false {
tab = tab + "<tr><td><a href='" + mementos[i].uri + "' target='_blank'>" + mementos[i].uri + "</a></td><td><img src='" + mementos[i].screenshot + "'/></td></tr>"
} else if mementos[i].compliant && mementos[i].resolved && mementos[i].captured == false {
tab = tab + "<tr><td><a href='" + mementos[i].uri + "' target='_blank'>" + mementos[i].uri + "</a></td><td><b>not captured</b></td></tr>"
} else {
noncompliant = append(noncompliant, mementos[i].id)
}
if mementos[i].compliant && mementos[i].resolved == false {
others = append(others, mementos[i].id)
}
}
tab = tab + "</table>"
var noncomp = "<b>Noncompliant Timegates</b><pre>"
for i := range noncompliant {
noncomp = noncomp + noncompliant[i] + "<br/>"
}
noncomp = noncomp + "</pre>"
var oth = "<b>Compliant but no resource</b><pre>"
for i := range others {
oth = oth + others[i] + "<br/>"
}
oth = oth + "</pre>"
return noncomp + "<br/>" + oth + "<br/><b>Good Timegates</b><br/>" + tab
}
func timegate(url string, date string) string {
//1. maketimemap for baseuri
//2. extract from timemap
//3, for compliant urls get next date for form date
//4. with a new uri take screenshot
//5. display, in table...
//get timemap data...
m, _ := getTimemap(makeTimemap(url))
//get the date of the snapshot in the archive closest to that we requested...
addDateURIs(m, url, date)
//make screenshots
makeScreenshots(m)
return maketable(m)
}
func main() {
var wg sync.WaitGroup
wg.Add(1)
go DefaultServer(port, &wg)
log.Println("Server started on:", port)
log.Println("Using:", sr.Version())
wg.Wait()
return
}