This repository has been archived by the owner on Nov 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpan.sh
executable file
·89 lines (70 loc) · 2.87 KB
/
rpan.sh
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
for C in jq xdg-open nc curl xclip; do
command -v "$C" >/dev/null 2>&1 || { echo "$C is required but not installed, aborting" >&2; exit 1; }
done
usage() {
echo "Usage: $0 SubredditName \"stream title\" [access_token]"
exit 1
}
sub="$1"
test -z "$sub" && usage
title="$2"
test -z "$title" && usage
# let user know that r/pan is only available during specific hours
if [[ $sub == "pan" ]]; then
echo "NOTICE: You are only able to stream to r/pan during specific hours. Please visit reddit.com/r/pan to learn more."
fi
REDIR=$(cat <<EOF
HTTP/1.1 200 OK
<script>
if( location.href.indexOf("#") > 0) {
window.setTimeout(function(){
location.href=location.href.replace("#","?") // redirect with hash string parameters as query string (so it is sent to the HTTP server)
}, 100);
} else {
document.write("Thanks. You can close this window now");
}
</script>
EOF
)
get_access_token() {
# open browser
xdg-open "https://www.reddit.com/api/v1/authorize?client_id=ohXpoqrZYub1kg&response_type=token&redirect_uri=http://localhost:65010/callback&scope=*&state=$RANDOM"
# start a "web" server and wait for the user to click on accept which would lead to requests to this server
# the server is queried for favicon too and in random order - in that case the token is in the referrer header - proper web server would be better
while true; do
access_token=$(echo "$REDIR" | nc -lvp 65010 -q0 | grep -E 'access_token=' | sed -ne 's/.*access_token=\([^&]*\).*/\1/p')
test -z "$access_token" || break
done
}
access_token="$3"
test -z "$access_token" && get_access_token
echo "Access Token: $access_token" # can be reused for 1 hour as an argument to this script
# Get valid RPAN subreddits
validate_subs=$(curl --location --request GET "https://strapi.reddit.com/recommended_broadcast_subreddits" \
-H "User-Agent: Key/0.1" \
-H "Authorization: Bearer $access_token");
# Turn result into array
valid_subreddits=($(echo "$validate_subs" | jq -r '.data[]'))
# Append r/pan
valid_subreddits=("pan" "${valid_subreddits[@]}")
# validate user's subreddit input
if [[ ! " ${valid_subreddits[@]} " =~ " ${sub} " ]]; then
echo "ERROR: $sub is not a valid RPAN subreddit!"
echo "RPAN SUBREDDITS:"
for s in ${valid_subreddits[@]}; do
echo -e "\\t[*] $s"
done
exit 1
fi
stream=$(curl -v -XPOST "https://strapi.reddit.com/r/$sub/broadcasts" \
-G --data-urlencode "title=$title" \
-H "User-Agent: Key/0.1" \
-H "Authorization: Bearer $access_token");
post=$(echo "$stream" | jq -r '.data.post.url')
key=$(echo "$stream" | jq -r '.data.streamer_key')
test -z "$key" && echo "$stream" && exit 1
echo -n "$key" | xclip -sel p -f | xclip -i -sel c # copy to both clipboards
echo "Post URL: $post"
echo "RTMP URL: rtmp://ingest.redd.it/inbound/"
echo "Streamer Key: $key (copied to your clipboard)"
# TODO query watchers / remaining time / comments?