summarylogtreecommitdiffstats
path: root/aceget.exp
blob: bc19bd4b9640e5db11bad1b80f4558842a6acb47 (plain)
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
#!/usr/bin/expect -f

#
# Aceget: a command-line Ace Stream client
#
# params: hostname/ip port-number TORRENT/INFOHASH/PID/RAW/URL url/pid/whatever
# - for media usage, see http://wiki.acestream.org/wiki/index.php/Engine_API/en
# - incoming video data is pushed into stdout, so redirect to your media software of choice
#


#
# Check dependencies, suppress all output
#
package require sha1
log_user 0


#
# Configuration
#
if {[expr $argc < 4]} {
	puts stderr "params: hostname/ip port-number TORRENT/INFOHASH/PID/RAW/URL url/pid/whatever"
	exit -1
} 
set address [lindex $argv 0]
set port [lindex $argv 1]
set type [lindex $argv 2]
set pid [lindex $argv 3]

set apiversion 3
set apikey n51LvQoTlJzNGaFxseRK-uvnvX-sD4Vm5Axwmc4UcoD-jruxmKsuJaH0eVgE


#
# Script start
#
# Connect via telnet, set timeout at 10 sec
#
spawn telnet $address $port
set timeout 10
expect {
	"Unable to connect to remote host" {puts stderr "Telnet connection failed, wrong address/port?"; exit 1}
	timeout {puts stderr "Telnet connection timed out, network error?"; exit 2}
	"Connected to"
}

#
# Shake hands and receive a challenge, then authenticate as per:
# http://wiki.acestream.org/wiki/index.php/Product_key/en
#
send [format "HELLOBG version=%d\r" $apiversion]
expect {
	-re "HELLOTS.*key=(\[^ \]*).*" {set challenge $expect_out(1,string)}
	timeout {puts stderr "Handshake timed out, incompatible ace version?"; exit 3}
}
send [format "READY key=%s-%s\r" [exec sed s/-.*// <<$apikey] [::sha1::sha1 "$challenge$apikey"]]
expect {
	NOTREADY {puts stderr "Authentication failed, wrong API key?"; exit 4}
	timeout {puts stderr "Authentication timed out, network error?"; exit 5}
	AUTH
}

#
# Send user data
#
send "USERDATA \[\{\"gender\": \"1\"\}, \{\"age\": \"3\"\}\]\r"

#
# Request the given Acestream PID from the engine, timeout after 30 sec
#
set timeout 30
send "START $type $pid 0\r"
expect {
	-re "START.*(http://\[^ \]*).*" {set videolink $expect_out(1,string)}
	timeout {puts stderr "Video request timed out, broadcast unavailable?"; exit 6}
}

#
# Push incoming .TS data into stdout, but only if redirected off the console
#
if {[dict exists [fconfigure stdout] -mode]} {
	expect_user *
	puts stderr $videolink
	puts stderr "Hit return when finished..."
	expect_user -timeout -1 \n
} else {
	catch {exec >@stdout 2>@stderr curl -s $videolink}
}

#
# Cleanup
#
send "STOP\r"
send "SHUTDOWN\r"