-
Notifications
You must be signed in to change notification settings - Fork 0
/
slot_machine.sh
executable file
·146 lines (127 loc) · 5.56 KB
/
slot_machine.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
#!/bin/bash
# ================================================================================
# slot_machine.sh
# ================================================================================
# A mad science experiment
# by Squish
# ________________________________________________________________________________
# ⢀⣠⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⣠⣤⣶⣶
# ⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⢰⣿⣿⣿⣿
# ⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⣀⣀⣾⣿⣿⣿⣿
# ⣿⣿⣿⣿⣿⡏⠉⠛⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⣿
# ⣿⣿⣿⣿⣿⣿⠀⠀⠀⠈⠛⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⠉⠁⠀⣿
# ⣿⣿⣿⣿⣿⣿⣧⡀⠀⠀⠀⠀⠙⠿⠿⠿⠻⠿⠿⠟⠿⠛⠉⠀⠀⠀⠀⠀⣸⣿
# ⣿⣿⣿⣿⣿⣿⣿⣷⣄⠀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⣿⣿
# ⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠠⣴⣿⣿⣿⣿
# ⣿⣿⣿⣿⣿⣿⣿⣿⡟⠀⠀⢰⣹⡆⠀⠀⠀⠀⠀⠀⣭⣷⠀⠀⠀⠸⣿⣿⣿⣿
# ⣿⣿⣿⣿⣿⣿⣿⣿⠃⠀⠀⠈⠉⠀⠀⠤⠄⠀⠀⠀⠉⠁⠀⠀⠀⠀⢿⣿⣿⣿
# ⣿⣿⣿⣿⣿⣿⣿⣿⢾⣿⣷⠀⠀⠀⠀⡠⠤⢄⠀⠀⠀⠠⣿⣿⣷⠀⢸⣿⣿⣿
# ⣿⣿⣿⣿⣿⣿⣿⣿⡀⠉⠀⠀⠀⠀⠀⢄⠀⢀⠀⠀⠀⠀⠉⠉⠁⠀⠀⣿⣿⣿
# ⣿⣿⣿⣿⣿⣿⣿⣿⣧⠀⠀⠀⠀⠀⠀⠀⠈⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⣿⣿
# ⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿
# --------------------------------------------------------------------------------
# Monitor patreon for available limited tier slots, and send a notification with
# a subscription link if free slots are detected.
#
# The code is parametized so can be applied to any patreon tier for any creator.
#
# Requirements:
# - htmlq
# - jq
# - notify-send
# - curl
# - patience
#
# Updates:
# 27/03/2022 - Initial version, send linux desktop notification.
# 28/03/2022 - Added support for sending a text using textbelt.
#
# TODO:
# MacOS - Send notification using osascript.
# Windows - Send notification using powershell.
# --------------------------------------------------------------------------------
function check_and_notify_slots {
patreonUser="$1"
tierId="$2"
description="$3"
sendNotification="$4"
sendText="$5"
textNumber="$6"
textBeltKey="$7"
sendDiscord="$8"
discordWebhook="$9"
# scrape the membership page
curl -s "https://www.patreon.com/${patreonUser}/membership" | htmlq -t script > out.txt
# extract the data part necessary for checking tiers
lines=`wc -l out.txt | awk '{print $1}'`
start_line=`grep -nF "Object.assign(window.patreon.bootstrap" out.txt | awk -F":" '{print $1}'`
tail_lines=$(expr $lines - $start_line)
end_line=$( expr `tail -n ${tail_lines} out.txt | grep -nF "});" | head -n 1 | awk -F":" '{print $1}'` - 1)
# Build the output for jq to look at...
echo "{" > out2.txt
cat out.txt | tail -n ${tail_lines} | head -n ${end_line} >> out2.txt
echo "}" >> out2.txt
# extract the number of open brain board slots
bb_slots=$(cat out2.txt | jq ".campaign.included[] | select( .id | contains(\"${tierId}\") ) | .attributes.remaining" | head -1)
# simple shell output
d=`date +%s`
echo "$d, $description, $bb_slots"
if [ $bb_slots -gt 0 ]; then
if [ "$sendNotification" = "true" ]; then
notify-send -u critical -t 30000 -i $PWD/icon.png -a "Discord" -c "alert" "$description Poller" "There are $bb_slots slot(s) open on the $description tier: \nhttps://patreon.com/join/$patreonUser/checkout?rid=$tierId"
fi
if [ "$sendText" = "true" ]; then
send_text "$textNumber" "$textBeltKey" "$description tier open ($bb_slots free)."
fi
if [ "$sendDiscord" = "true" ]; then
send_discord "$discordWebhook" "$description tier open ($bb_slots free)."
fi
fi
}
function send_discord {
webhookUrl="$1"
body="$2"
curl -v -X POST "$webhookUrl" -H "Content-type: application/json" -d "{\"content\": \"${body}\"}"
}
function send_text {
number="$1"
key="$2"
body="$3"
curl -X POST https://textbelt.com/text \
--data-urlencode phone="$number" \
--data-urlencode message="$body" \
-d key="$key"
}
function read_config {
cfg="./config.json"
if [ ! -f "${cfg}" ]; then
echo "No ${cfg}. Please create this file here using config.json.example as a base."
exit 0
fi
export CREATOR=$(cat ${cfg} | jq -r '.creator')
export TIER_ID=$(cat ${cfg} | jq -r '.tierId')
export DESCRIPTION=$(cat ${cfg} | jq -r '.description')
export INTERVAL=$(cat ${cfg} | jq -r '.interval')
export SEND_TEXT=$(cat ${cfg} | jq -r '.sendText')
export SEND_NOTIFY=$(cat ${cfg} | jq -r '.sendNotification')
export TEXT_NUMBER=$(cat ${cfg} | jq -r '.textNumber')
export TEXT_KEY=$(cat ${cfg} | jq -r '.textBeltKey')
export SEND_DISCORD=$(cat ${cfg} | jq -r '.sendDiscord')
export DISCORD_WEBHOOK=$(cat ${cfg} | jq -r '.discordWebhook')
echo "Creator : $CREATOR"
echo "Tier : $TIER_ID"
echo "Desc : $DESCRIPTION"
echo "Interval : $INTERVAL"
echo "Text? : $SEND_TEXT"
echo "Notify? : $SEND_NOTIFY"
echo "Number : $TEXT_NUMBER"
echo "Key : $TEXT_KEY"
echo "Discord? : $SEND_DISCORD"
echo "Discord URL: $DISCORD_WEBHOOK"
}
read_config
while true
do
check_and_notify_slots "$CREATOR" "$TIER_ID" "$DESCRIPTION" "$SEND_NOTIFY" "$SEND_TEXT" "$TEXT_NUMBER" "$TEXT_KEY" "$SEND_DISCORD" "$DISCORD_WEBHOOK"
sleep $INTERVAL
done