-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkstathandler.go
74 lines (68 loc) · 2.22 KB
/
linkstathandler.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
package main
import (
"crypto/md5"
"fmt"
"github.com/httpreserve/httpreserve"
"github.com/httpreserve/wayback"
)
var structids []string
// makeHash will create a MD5 hash for us to use to index our data without
// duplication...
func makeHash(js string) string {
md5 := md5.New()
md5.Write([]byte(js))
return fmt.Sprintf("%x", md5.Sum(nil))
}
// convertInterface will help us pipe generic values from
// the deconstruction of httpreserve.LinkStats to a string for
// storage in BoltDB.
func convertInterface(v interface{}) string {
var val string
switch v.(type) {
case string:
val = fmt.Sprintf("%s", v)
case int:
val = fmt.Sprintf("%d", v)
case bool:
switch v {
case true:
val = "true"
case false:
val = "false"
}
}
if val == "" {
return "\"\""
}
return val
}
// storeStruct allows us to get a different representation of the LinkStats structure
// e.g. as a map we have good flexibility over looping and passing around without
// reglection to iterate through the struct for us.
func storeStruct(ls httpreserve.LinkStats, js string) map[string]interface{} {
var lmap = make(map[string]interface{})
// make an id to help filtering in reports,
// id should be unique to the JSON output
id := makeHash(js)
lmap["id"] = id
lmap["filename"] = ls.FileName
lmap["content-type"] = ls.ContentType
lmap["title"] = ls.Title
lmap["analysis version number"] = ls.AnalysisVersionNumber
lmap["analysis version text"] = ls.AnalysisVersionText
lmap["link"] = ls.Link
lmap["response code"] = ls.ResponseCode
lmap["response text"] = ls.ResponseText
lmap["internet archive latest"] = ls.InternetArchiveLinkLatest
lmap["internet archive earliest"] = ls.InternetArchiveLinkEarliest
lmap["wayback latest date"] = wayback.GetHumanDate(ls.InternetArchiveLinkLatest)
lmap["wayback earliest date"] = wayback.GetHumanDate(ls.InternetArchiveLinkEarliest)
lmap["internet archive save link"] = ls.InternetArchiveSaveLink
lmap["internet archive response code"] = ls.InternetArchiveResponseCode
lmap["internet archive response text"] = ls.InternetArchiveResponseText
lmap["archived"] = ls.Archived
lmap["protocol error"] = ls.Error
lmap["protocol error"] = ls.ErrorMessage
lmap["stats creation time"] = ls.StatsCreationTime
return lmap
}