-
Notifications
You must be signed in to change notification settings - Fork 4
/
misc.go
131 lines (121 loc) · 3.39 KB
/
misc.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
package feed
import (
"fmt"
"html"
"io"
"io/ioutil"
"math/rand"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"time"
"unicode"
"github.com/kennygrant/sanitize"
)
// copyFileContents copies the contents of the file named src to the file named
// by dst. The file will be created if it does not already exist. If the
// destination file exists, all it's contents will be replaced by the contents
// of the source file.
func copyFileContents(src, dst string) (err error) {
in, err := os.Open(src)
if err != nil {
return
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return
}
defer func() {
cerr := out.Close()
if err == nil {
err = cerr
}
}()
if _, err = io.Copy(out, in); err != nil {
return
}
err = out.Sync()
return
}
func fileExists(f string) bool {
_, err := os.Stat(f)
return !os.IsNotExist(err)
}
func makeURLDebouncer(wait time.Duration) func(URL string) string {
lastAccessed := make(map[string]time.Time)
return func(URL string) string {
u, err := url.Parse(URL)
if err != nil {
log.Errorf("Could not parse URL %s, will try to fetch it anyway: %s", URL, err)
return URL
}
domain := u.Host
timePassed := time.Since(lastAccessed[domain])
if timePassed < wait {
log.Debugf("Waiting %.1f seconds to request from %s", (wait - timePassed).Seconds(), URL)
time.Sleep(wait - timePassed)
}
lastAccessed[domain] = time.Now()
return URL
}
}
func cleanXML(XML []byte) []byte {
// http://blog.zikes.me/post/cleaning-xml-files-before-unmarshaling-in-go/
// Remove utf8 special shenanigans
printOnly := func(r rune) rune {
if unicode.IsPrint(r) {
return r
}
return -1
}
return []byte(strings.Map(printOnly, string(XML)))
}
// Fixes "could not parse HTML: XML syntax error on line 1: invalid character entity —" in codes like this:
// <summary type="html">Every site I've used Codeship on—until today...</summary>
// See: https://stackoverflow.com/questions/3805050/xml-parser-error-entity-not-defined
func unescapeXML(XML string) string {
return html.UnescapeString(XML)
}
// makeCachedURLFetcher is retired. It was used during initial phases of development to prevent spamming feed sources.
func makeCachedURLFetcher(client *http.Client, cacheDir string) func(URL string) (content []byte, err error) {
fetcher := MakeURLFetcher(client)
return func(URL string) (content []byte, err error) {
cacheDir = filepath.Clean(cacheDir)
fileName := cacheDir + "/" + sanitize.BaseName(URL) + ".html"
if fileExists(fileName) {
log.Debugf("Cached %s", fileName)
b, err := ioutil.ReadFile(fileName)
if err != nil {
return []byte(""), fmt.Errorf("error opening cache %s : %s", URL, err)
}
return b, nil
}
log.Debugf("Web %s", URL)
content, err = fetcher(URL)
if err != nil {
return content, err
}
f, err := os.Create(fileName)
if err != nil {
return nil, fmt.Errorf("could not open cache file file %s for writing: %s", fileName, err)
}
defer f.Close()
if _, err = f.Write(content); err != nil {
return []byte(""), fmt.Errorf("could not write to cache file %s: %s", fileName, err)
}
return content, nil
}
}
func shuffleMapKeys(srcMap map[string]string) (mapKeys []string) {
mapKeys = make([]string, 0, len(srcMap))
for k := range srcMap {
mapKeys = append(mapKeys, k)
}
rand.Shuffle(len(mapKeys), func(i, j int) {
mapKeys[i], mapKeys[j] = mapKeys[j], mapKeys[i]
})
return mapKeys
}