-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimelapse.go
85 lines (76 loc) · 1.99 KB
/
timelapse.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
package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"time"
)
func grabPicture(url, user, password, fname string) error {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
req.SetBasicAuth(user, password)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
val, exists := resp.Header["Content-Type"]
if exists && val[0] == "text/html" {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
log.Fatal(string(b))
}
log.Println("not a 200 response")
}
f, err := os.Create(fname)
if err != nil {
return err
}
written, err := io.Copy(f, resp.Body)
if err != nil {
return err
}
log.Printf("Wrote %s (%d bytes)\n", fname, written)
return nil
}
func watcher(host, user, password string, interval, duration time.Duration) error {
url := "http://" + host + "/snapshot.cgi"
log.Printf("watching(\"%s\", \"%s\", \"%s\")\n", url, user, password)
ticker := time.Tick(interval)
done := time.After(duration)
i := 0
for {
select{
case <-ticker:
fname := fmt.Sprintf("out_%05d.jpg", i)
err := grabPicture(url, user, password, fname)
if err != nil{
return err
}
i += 1
case <-done:
return nil
}
}
}
func main() {
host := flag.String("h", "ipcam_000000000000_0", "host")
user := flag.String("u", "admin", "user name")
password := flag.String("p", "admin", "password")
interval := flag.Duration("i", 24*time.Second, "interval")
duration := flag.Duration("d", 12*time.Hour , "duration")
flag.Parse()
err := watcher(*host, *user, *password, *interval, *duration)
if err != nil{
log.Fatal(err)
}
}