-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcf-sound-monitor.rb
executable file
·104 lines (75 loc) · 2.71 KB
/
cf-sound-monitor.rb
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
#!/usr/bin/ruby
require 'net/http'
# This script reads an Invesdor campaign banner and plays
# sounds if the sum is bigger than last time this was run.
# Config
address = "https://www.invesdor.com/en/pitches/903/banner"
sum_per_coin = 500
filepath = "latest_sum.txt"
soundpath = "coin.wav"
lifeupinterval = 30000 # How often should a 1up sound be played (set to 0 to disable)
lifeupsoundpath = "1up.wav"
levelupinterval = 500000 # How often should a level up sound be played (set to 0 to disable)
levelupsoundpath = "levelup.wav"
announcement_voice = "Alex" #set to nil to skip Mac say command announcement
repeat_after = 300 #number of seconds to wait between polls. Set 0 to turn of repeating.
# NOTE: if this is above zero, the program never stops automatically
while repeat_after > 0 do
print "\nChecking campaign status at #{Time.now}: "
# Check latest sum online
uri = URI.parse(address)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
sum = response.body.match(/<span>([\d\.]+)..EUR<\/span>/)[1].delete(".").to_i
# Read the earlier sum from file
latest_sum = 0
if File.file?(filepath)
File.open(filepath, 'r') do |f|
latest_sum = f.gets.to_i
end
else
# if the file was not found, this is probably the first time running
# and then it doesn't make sense to play all the sounds from 0 to sum
latest_sum = sum
end
print "#{latest_sum} -> #{sum}"
# Play the sounds
if sum > latest_sum
extra_coins = 0
diff = sum-latest_sum
if diff > sum_per_coin
extra_coins = (diff-501)/sum_per_coin + 1
end
print " x #{extra_coins + 1}"
while extra_coins > 0 do
Thread.new do
`afplay #{soundpath}`
end
extra_coins -= 1
sleep 0.35
end
`afplay #{soundpath}`
# check if level up reached
if sum/levelupinterval > latest_sum/levelupinterval
`afplay #{levelupsoundpath}`
end
# check if 1up reached
if sum/lifeupinterval > latest_sum/lifeupinterval
`afplay #{lifeupsoundpath}`
end
sleep 1
sum_to_say = sum
if sum > 1000000
sum_to_say = "1 million #{sum-1000000}"
end
`say -v "#{announcement_voice}" "The current sum of investments is #{sum_to_say}."` if announcement_voice
end
# Write this sum to file
File.open(filepath, 'w') do |f|
# use "\n" for two lines of text
f.puts sum
end
sleep repeat_after
end