-
Notifications
You must be signed in to change notification settings - Fork 0
/
follow.sh
executable file
Β·186 lines (170 loc) Β· 5.16 KB
/
follow.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/bin/sh
# Default options
USER_LOGIN="zneix"
DISABLE_NOTIFICATIONS="true"
FOLLOW=1
# help
usage () {
echo "Usage: ./$(basename $0) [-fnh] [-u USER_LOGIN] [-o OAUTH] [-t TARGET] [-T TARGET_ID]" 2>&1
echo 'Follow a user on Twitch from command line through GQL, fuck webchat.'
echo ' -h shows help'
echo ' -f follows TARGET'
echo ' -F unfollows TARGET'
echo ' -n enable stream notifications on newly created follow'
echo ' -N do not enable stream notifications on newly created follow'
echo ' -u [USER_LOGIN] if pass(1) is present, USER_LOGIN can be specified to retrieve the OAuth token by invoking $(pass twilight/USER_LOGIN)'
echo ' -o [OAUTH] twilight OAuth token used while making GQL requests'
echo ' -t [TARGET] Twitch login of the user to follow. It will be first translated to corresponding user ID'
echo ' -T [TARGET_ID] Twitch user ID of the user to follow'
}
# Performs a gql query, needs 1 argument
make_query () {
QUERY=$(echo $1 | sed -E 's/\s+/ /g')
# use "https://httpbin.org/anything" for debugging
curl -s -X POST "https://gql.twitch.tv/gql" \
-H "Client-ID: ue6666qo983tsx6so1t0vnawi233wa" \
-H "Authorization: OAuth $TWILIGHT_OAUTH" \
-H "Content-Type: application/json" \
-d "$QUERY"
}
TEMP=$(getopt -o 'hfFnNu:o:t:T:' -l 'help,follow,unfollow,notifications,disable-notifications,user:,oauth:,target:,target-id:' -n 'follow.sh' -- "$@")
if [ $? -ne 0 ]; then
echo "Invalid arguments! Check ./$(basename $0) --help"
exit 1
fi
eval set -- "$TEMP"
unset TEMP
set -e #TODO: What does this do? 4Head
# Parsing arguments
while true; do
case "$1" in
'-h'|'--help')
usage
exit 0
;;
'-f'|'--follow')
FOLLOW=1
shift
continue
;;
'-F'|'--unfollow')
FOLLOW=0
shift
continue
;;
'-n'|'--notifications') # sets notifications to !DISABLE_NOTIFICATIONS
DISABLE_NOTIFICATIONS="false"
shift
continue
;;
'-N'|'--disable-notifications')
DISABLE_NOTIFICATIONS="true"
shift
continue
;;
'-u'|'--user')
USER_LOGIN="$2"
shift 2
continue
;;
'-o'|'--oauth')
TWILIGHT_OAUTH="$2"
shift 2
continue
;;
'-t'|'--target')
TARGET_LOGIN="$2"
shift 2
continue
;;
'-T'|'--target-id')
TARGET_ID="$2"
shift 2
continue
;;
--) shift; break ;;
*) break ;;
esac
done
if [ -z $TWILIGHT_OAUTH ] && command -v pass > /dev/null 2>&1 && [ $USER_LOGIN ]; then
#echo "--user [USER_LOGIN] has been specified, acquiring OAuth from pass"
PASS_OAUTH=$(pass twilight/tv/$USER_LOGIN)
if [ $PASS_OAUTH ]; then
TWILIGHT_OAUTH="$PASS_OAUTH"
fi
fi
if [ -z $TWILIGHT_OAUTH ]; then
echo "--oauth [OAUTH] (or --user [USER_LOGIN] with OAuth under \$(pass twilight/USER_LOGIN)) needs to be set!"
exit 2
fi
# Query translating $TARGET_LOGIN to their user id
USER_QUERY='{
"query": "query FetchUserByLogin($login: String) {
user(login: $login) {
id login
}
}",
"variables": {
"login": "'$TARGET_LOGIN'"
}
}'
# Translate TARGET_LOGIN to TARGET_ID
if [ $TARGET_LOGIN ]; then
#echo "TARGET has been set, attempting to translate it to corresponding user id"
#TARGET_ID=$(jq '.data[].id' -j <<< $(make_query "$USER_QUERY"))
TARGET_ID=$(make_query "$USER_QUERY" | jq '.data[].id' -j)
fi
if [ -z $TARGET_ID ]; then
echo "TARGET_ID needs to be set!"
exit 2
fi
# Actual script
echo "user: $USER_LOGIN"
echo "π $TARGET_ID ($TARGET_LOGIN) $([ $FOLLOW -eq 1 ] && echo "β€" || echo "π") $([ $DISABLE_NOTIFICATIONS == "true" ] && echo "π" || echo "π")"
# Mutation making $USER_LOGIN follow $TARGET_ID on Twitch
FOLLOW_QUERY='{
"query": "mutation FollowUser($xd: FollowUserInput!) {
followUser(input: $xd) {
follow {
disableNotifications
followedAt
user {
id
login
}
}
}
}",
"variables": {
"xd": {
"disableNotifications": '$DISABLE_NOTIFICATIONS',
"targetID": "'$TARGET_ID'"
}
}
}'
# Mutation making $USER_LOGIN unfollow $TARGET_ID on Twitch
UNFOLLOW_QUERY='{
"query": "mutation UnfollowUser($xd: UnfollowUserInput!) {
unfollowUser(input: $xd) {
follow {
disableNotifications
followedAt
user {
id
login
}
}
}
}",
"variables": {
"xd": {
"targetID": "'$TARGET_ID'"
}
}
}'
if [ $FOLLOW -eq 1 ]; then
#make_query "$FOLLOW_QUERY" | jq '.data.followUser.follow'
make_query "$FOLLOW_QUERY"
else
make_query "$UNFOLLOW_QUERY" | jq '.data.unfollowUser.follow'
fi