forked from dotabuff/manta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
73 lines (59 loc) · 1.38 KB
/
util_test.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
package manta
import (
"compress/bzip2"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
)
func mustGetReplayData(name string, url string) []byte {
buf, err := getReplayData(name, url)
if err != nil {
panic(err)
}
return buf
}
func mustGetReplayReader(name string, url string) io.ReadCloser {
for {
path := fmt.Sprintf("replays/%s.dem", name)
r, err := os.Open(path)
if err == nil {
return r
}
fmt.Printf("unable to find replay %s at %s, attemping repair...", name, path)
if _, err := getReplayData(name, url); err != nil {
panic(err)
}
}
}
func getReplayData(name string, url string) ([]byte, error) {
path := fmt.Sprintf("replays/%s.dem", name)
if data, err := ioutil.ReadFile(path); err == nil {
return data, nil
}
fmt.Printf("downloading replay %s from %s...\n", name, url)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Return an error if we don't get a 200
if resp.StatusCode != 200 {
return nil, fmt.Errorf("invalid status %d", resp.StatusCode)
}
var data []byte
if url[len(url)-3:] == "bz2" {
data, err = ioutil.ReadAll(bzip2.NewReader(resp.Body))
} else {
data, err = ioutil.ReadAll(resp.Body)
}
if err != nil {
return nil, err
}
if err := ioutil.WriteFile(path, data, 0644); err != nil {
return nil, err
}
fmt.Printf("downloaded replay %s from %s to %s\n", name, url, path)
return data, nil
}