-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblackvue.go
216 lines (184 loc) · 4.08 KB
/
blackvue.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
package blackvue
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
// New initializes a fresh blackvue client
func New(ip string) *Client {
return &Client{
ip: ip,
}
}
type Client struct {
ip string
}
type Videos struct {
Front, Rear, Unknown []Video
}
// Video represents the base path to a video or gps file
// ie. {Video}.thm
type Video string
func (v Video) MP4() string {
return fmt.Sprintf("%s.mp4", v)
}
func (v Video) THM() string {
return fmt.Sprintf("%s.thm", v)
}
func (c *Client) list() (Videos, error) {
var vids Videos
path := fmt.Sprintf("http://%s/blackvue_vod.cgi", c.ip)
req, err := http.NewRequest("GET", path, nil)
if err != nil {
return vids, err
}
httpCli := &http.Client{
Timeout: time.Duration(5 * time.Second),
}
resp, err := httpCli.Do(req)
if err != nil {
return vids, err
}
defer resp.Body.Close()
bs, err := ioutil.ReadAll(resp.Body)
if err != nil {
return vids, err
}
lines := strings.Split(string(bs), "\r\n")
for _, l := range lines {
base := strings.TrimPrefix(strings.TrimSuffix(l, ".mp4,s:1000000"), "n:/Record/")
if strings.HasSuffix(base, "R") {
vids.Rear = append(vids.Rear, Video(base))
} else if strings.HasSuffix(base, "F") {
vids.Front = append(vids.Front, Video(base))
} else if base == "v:1.00" {
// ignore this
} else {
vids.Unknown = append(vids.Unknown, Video(base))
}
}
return vids, nil
}
// List enumerates all the video files on Dashcam
func (c *Client) List() (Videos, error) {
return c.list()
}
type Summary struct {
FrontCount, RearCount int
FrontTotal, RearTotal int
}
func (c *Client) Status(dir string) (*Summary, error) {
vids, err := c.list()
if err != nil {
return nil, err
}
var (
rearCount, frontCount int
)
for _, vid := range vids.Rear {
path := filepath.Join(dir, vid.MP4())
_, err := os.Stat(path)
if err != nil {
rearCount++
}
}
for _, vid := range vids.Front {
path := filepath.Join(dir, vid.MP4())
_, err := os.Stat(path)
if err != nil {
frontCount++
}
}
return &Summary{
FrontCount: frontCount,
FrontTotal: len(vids.Front),
RearCount: rearCount,
RearTotal: len(vids.Rear),
}, nil
}
// Sync pulls all the video files not found in path
func (c *Client) Sync(dir string) error {
if err := os.MkdirAll(dir, 0755); err != nil {
log.Fatal(err)
}
vids, err := c.list()
if err != nil {
return err
}
return c.sync(dir, vids)
}
func (c *Client) sync(dir string, vids Videos) error {
// It's unlikely this is more efficient, but at least
// we don't have to wait for rear to finish first
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
for _, vid := range vids.Rear {
path := filepath.Join(dir, vid.MP4())
_, err := os.Stat(path)
// If file not found, download it
if err != nil {
if err := c.fetchVideo(dir, vid); err != nil {
log.Printf("failed to fetch video %s: %s\n", vid, err)
}
}
}
}()
wg.Add(1)
go func() {
defer wg.Done()
for _, vid := range vids.Front {
path := filepath.Join(dir, vid.MP4())
_, err := os.Stat(path)
// If file not found, download it
if err != nil {
if err := c.fetchVideo(dir, vid); err != nil {
log.Printf("failed to fetch video %s: %s\n", vid, err)
}
}
}
}()
wg.Wait()
return nil
}
func (c *Client) fetchVideo(path string, vid Video) error {
outMP4, err := os.Create(filepath.Join(path, vid.MP4()))
if err != nil {
return err
}
outTHM, err := os.Create(filepath.Join(path, vid.THM()))
if err != nil {
return err
}
// mp4
uri := fmt.Sprintf("http://%s/Record/%s", c.ip, vid.MP4())
log.Printf("saving %s to %s\n", uri, outMP4.Name())
resp, err := http.Get(uri)
if err != nil {
return err
}
defer resp.Body.Close()
if _, err := io.Copy(outMP4, resp.Body); err != nil {
return err
}
// thm
uri = fmt.Sprintf("http://%s/Record/%s", c.ip, vid.THM())
log.Printf("saving %s to %s\n", uri, outTHM.Name())
resp, err = http.Get(uri)
if err != nil {
return err
}
defer resp.Body.Close()
if _, err := io.Copy(outTHM, resp.Body); err != nil {
return err
}
return nil
}