forked from Sioro-Neoku/go-peerflix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
96 lines (84 loc) · 1.76 KB
/
main.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
package main
import (
"flag"
"log"
"net/http"
"os"
"os/exec"
"os/signal"
"runtime"
"strconv"
"syscall"
"time"
)
// Exit statuses.
const (
_ = iota
exitNoTorrentProvided
exitErrorInClient
)
func main() {
// Parse flags.
var port int
var vlc *bool
var seed *bool
vlc = flag.Bool("vlc", false, "Open vlc to play the file")
flag.IntVar(&port, "port", 8080, "Port to stream the video on")
seed = flag.Bool("seed", false, "Seed after finished downloading")
flag.Parse()
if len(flag.Args()) == 0 {
flag.Usage()
os.Exit(exitNoTorrentProvided)
}
// Start up the torrent client.
client, err := NewClient(flag.Arg(0), port, *seed)
if err != nil {
log.Fatalf(err.Error())
os.Exit(exitErrorInClient)
}
// Http handler.
go func() {
http.HandleFunc("/", client.GetFile)
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(port), nil))
}()
// Open vlc to play.
if *vlc {
go func() {
for !client.ReadyForPlayback() {
time.Sleep(time.Second)
}
playInVlc(port)
}()
}
// Handle exit signals.
interruptChannel := make(chan os.Signal, 1)
signal.Notify(interruptChannel,
os.Interrupt,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
go func(interruptChannel chan os.Signal) {
for range interruptChannel {
log.Println("Exiting...")
client.Close()
os.Exit(0)
}
}(interruptChannel)
// Cli render loop.
for {
client.Render()
time.Sleep(time.Second)
}
}
func playInVlc(port int) {
log.Printf("Playing in vlc")
command := []string{"vlc"}
if runtime.GOOS == "darwin" {
command = []string{"open", "-a", "vlc"}
}
command = append(command, "http://localhost:"+strconv.Itoa(port))
if err := exec.Command(command[0], command[1:]...).Start(); err != nil {
log.Printf("Error opening vlc: %s\n", err)
}
}