forked from jasonparekh/slingdvr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asyncrecorder.go
84 lines (69 loc) · 1.77 KB
/
asyncrecorder.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
package main
import (
"errors"
"fmt"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"time"
)
func AsyncRecorder(recs <-chan RecRequest) error {
for {
if err := handleRec(<-recs); err != nil {
return err
}
}
}
func handleRec(r RecRequest) error {
defer close(r.Finished)
if err := powerReceiver(); err != nil {
return err
}
if err := startProgram(r.Showing); err != nil {
return err
}
record(r.Showing, nil, timeNow().Add(r.Showing.End.Sub(r.Showing.Start)))
return nil
}
func powerReceiver() error {
bin := fmt.Sprintf("%s/rec2a.pl", filepath.Dir(os.Args[0]))
if _, err := os.Stat(bin); os.IsNotExist(err) {
bin, err = filepath.Abs("rec2a.pl")
if err != nil {
return err
}
}
args := getSlingArgs()
args = append(args, "-selectonly=1")
// TODO IR blaster is acting up, try a few times
for i := 0; i < 5; i++ {
cmd := exec.Command(bin, args...)
if err := cmd.Start(); err != nil {
return errors.New("Could not start power-on command: " + err.Error())
}
if err := cmd.Wait(); err != nil {
return errors.New(fmt.Sprintf("Could not wait on power-on command[%s %v]: %s", bin, args, err.Error()))
}
time.Sleep(1 * time.Second)
}
return nil
}
func startProgram(s Showing) error {
u := fmt.Sprintf("http://newdish.sling.com/dvr/rest/v1/receiverid/%s?correlationid=%s", config.ReceiverId, config.CorrelationId)
// 'User-Agent:da-android-phone/3.6.6' actiontype=playbackStart programid=8052 pvrattribute=16789504 creator=0
resp, err := http.PostForm(u, url.Values{
"actiontype": {"playbackStart"},
"programid": {s.Id},
"pvrattribute": {s.PVRAttrib},
"creator": {"0"},
})
if err != nil {
return err
}
if resp.StatusCode != 200 {
return errors.New("Did not get OK response for starting program")
}
return nil
}