forked from BarryCarlyon/streamdonations-alerter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdonations.py
297 lines (257 loc) · 12.6 KB
/
donations.py
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
from decimal import *
from socketIO_client import SocketIO
import ConfigParser
import json
import time
import sys, os
import urllib2
import winsound
import logging
logging.basicConfig(level=logging.DEBUG)
topDonorAmount = float(0)
recentDonations = []
donationsPolled = False
version = "3.0"
internal_working_dir = os.getcwd()
#os.environ['REQUESTS_CA_BUNDLE'] = os.path.join(os.path.dirname(__file__), "cacert.pem")
cacert_location = False
def str2bool(v):
return v.lower() in ("yes", "true", "t", "1")
def getConnected():
global topDonorAmount
global recentDonations
global donationsPolled
def login():
emitData = json.dumps({'channel': channel, 'key': key})
socketIO.emit('auth', emitData)
def authenticated():
global topDonorAmount
global recentDonations
global donationsPolled
print ' '
print 'Logged into the Donation Tracker as %s' % channel
print ' '
if donationsPolled == False:
print 'Polling donation server..'
u = urllib2.urlopen('https://www.streamdonations.net/api/poll?channel=%s&key=%s' % (channel, key))
currentDonations = u.read()
currentDonations = json.loads(currentDonations)
if currentDonations["status"] == "success":
topDonor = currentDonations["top"]
mostRecentDonor = currentDonations["mostRecent"]
if dailyTopDonation == False:
try:
topDonor = topDonor[0]
if float(topDonor["amount"]) >= fileMinimum:
topDonorAmount = float(topDonor["amount"])
if topDonor["note"] == None:
topDonor["note"] = "No Message"
if topDonor["twitchUsername"] == None:
topDonor["twitchUsername"] = "Anonymous"
topDonationFormatted = topDonationFormatting.replace("{amount}",topDonor["amount"]).replace("{note}",topDonor["note"]).replace("{username}",topDonor["twitchUsername"]).replace("{processor}",topDonor["processor"]).replace("{currencySymbol}",topDonor["currencySymbol"])
localFile = open(resourcePath('topDonator.txt', user=True), 'w')
localFile.write(" %s " % topDonationFormatted.encode("utf-8", errors='ignore'))
localFile.close()
print "Updated top donation to: %s" % topDonationFormatted.encode("utf-8", errors='ignore')
else:
print "Top donation lower than minimum. Setting top donation to nobody."
localFile = open(resourcePath('topDonator.txt', user=True), 'w')
localFile.write(" ")
localFile.close()
except IndexError:
print "No top donation. Setting top donation to nobody."
localFile = open(resourcePath('topDonator.txt', user=True), 'w')
localFile.write(" ")
localFile.close()
else:
print "Setting top donation to nobody."
localFile = open(resourcePath('topDonator.txt', user=True), 'w')
localFile.write(" ")
localFile.close()
if dailyDonationList == False:
try:
donationList = ""
i = 0
for donor in mostRecentDonor:
i += 1
if i <= donationListAmount:
if float(donor["amount"]) >= fileMinimum:
if donor["note"] == None:
donor["note"] = "No Message"
if donor["twitchUsername"] == None:
donor["twitchUsername"] = "Anonymous"
if i == 1:
donationList += donationListFormatting.replace("{amount}",donor["amount"]).replace("{note}",donor["note"]).replace("{username}",donor["twitchUsername"]).replace("{processor}",donor["processor"]).replace("{currencySymbol}",donor["currencySymbol"])
else:
donationList += ", "+donationListFormatting.replace("{amount}",donor["amount"]).replace("{note}",donor["note"]).replace("{username}",donor["twitchUsername"]).replace("{processor}",donor["processor"]).replace("{currencySymbol}",donor["currencySymbol"])
recentDonations.append(donationListFormatting.replace("{amount}",donor["amount"]).replace("{note}",donor["note"]).replace("{username}",donor["twitchUsername"]).replace("{processor}",donor["processor"]).replace("{currencySymbol}",donor["currencySymbol"]))
localFile = open(resourcePath('donatorList.txt', user=True), 'w')
localFile.write(" %s " % donationList.encode("utf-8", errors='ignore'))
localFile.close()
print "Updated donation list to: %s" % donationList.encode("utf-8", errors='ignore')
except:
print "No recent donations. Donation list is empty."
localFile = open(resourcePath('donatorList.txt', user=True), 'w')
localFile.write(" ")
localFile.close()
else:
print "Setting donation list to empty."
localFile = open(resourcePath('donatorList.txt', user=True), 'w')
localFile.write(" ")
localFile.close()
if dailyRecentDonation == False:
try:
mostRecentDonor = mostRecentDonor[0]
if float(mostRecentDonor["amount"]) >= fileMinimum:
if mostRecentDonor["note"] == None:
mostRecentDonor["note"] = "No Message"
if mostRecentDonor["twitchUsername"] == None:
mostRecentDonor["twitchUsername"] = "Anonymous"
donationFormatted = donationFormatting.replace("{amount}",mostRecentDonor["amount"]).replace("{note}",mostRecentDonor["note"]).replace("{username}",mostRecentDonor["twitchUsername"]).replace("{processor}",mostRecentDonor["processor"]).replace("{currencySymbol}",mostRecentDonor["currencySymbol"])
localFile = open(resourcePath('mostRecentDonator.txt', user=True), 'w')
localFile.write(" %s " % donationFormatted.encode("utf-8", errors='ignore'))
localFile.close()
print "Updated most recent donator to: %s" % donationFormatted.encode("utf-8", errors='ignore')
else:
print "Most recent donation lower than minimum. Setting most recent donation to nobody."
localFile = open(resourcePath('mostRecentDonator.txt', user=True), 'w')
localFile.write(" ")
localFile.close()
except:
print "No most recent donation. Setting most recent donation to nobody."
localFile = open(resourcePath('mostRecentDonator.txt', user=True), 'w')
localFile.write(" ")
localFile.close()
else:
print "Setting most recent donation to nobody."
localFile = open(resourcePath('mostRecentDonator.txt', user=True), 'w')
localFile.write(" ")
localFile.close()
print ' '
print 'Current Top Donation set to: %s' % topDonorAmount
donationsPolled = True
print 'Incoming donations will now be processed'
def authenticationFailed():
print 'Authentication Failed for %s' % channel
def newDonation(donation):
global topDonorAmount
global recentDonations
donationFormatted = donationFormatting.replace("{amount}",donation["amount"]).replace("{note}",donation["note"]).replace("{username}",donation["twitchUsername"]).replace("{processor}",donation["processor"]).replace("{currencySymbol}",donation["currencySymbol"])
donationListFormatted = donationListFormatting.replace("{amount}",donation["amount"]).replace("{note}",donation["note"]).replace("{username}",donation["twitchUsername"]).replace("{processor}",donation["processor"]).replace("{currencySymbol}",donation["currencySymbol"])
topDonationFormatted = topDonationFormatting.replace("{amount}",donation["amount"]).replace("{note}",donation["note"]).replace("{username}",donation["twitchUsername"]).replace("{processor}",donation["processor"]).replace("{currencySymbol}",donation["currencySymbol"])
consoleDonationFormatted = consoleDonationFormatting.replace("{amount}",donation["amount"]).replace("{note}",donation["note"]).replace("{username}",donation["twitchUsername"]).replace("{processor}",donation["processor"]).replace("{currencySymbol}",donation["currencySymbol"])
print ' '
if float(donation["amount"]) > topDonorAmount:
isTopDonation = True
else:
isTopDonation = False
if isTopDonation == True:
topDonorAmount = float(donation["amount"])
print 'We have a new TOP donation!!'
if float(donation["amount"]) >= fileMinimum:
localFile = open(resourcePath('topDonator.txt', user=True), 'w')
localFile.write(" %s " % topDonationFormatted.encode("utf-8", errors='ignore'))
localFile.close()
else:
print 'We have a new donation!'
print("---> %s <---" % consoleDonationFormatted.encode("utf-8", errors='ignore'))
if float(donation["amount"]) >= fileMinimum:
localFile = open(resourcePath('mostRecentDonator.txt', user=True), 'w')
localFile.write(" %s " % donationFormatted.encode("utf-8", errors='ignore'))
localFile.close()
if len(recentDonations) == donationListAmount:
recentDonations.pop()
recentDonations.insert(0, donationListFormatted)
donationList = ""
i = 0
for donor in recentDonations:
i += 1
if i == 1:
donationList += donor
else:
donationList += ", "+donor
localFile = open(resourcePath('donatorList.txt', user=True), 'w')
localFile.write(" %s " % donationList.encode("utf-8", errors='ignore'))
localFile.close()
if float(donation["amount"]) >= soundMinimum:
if playWAVonDonation == True:
if isTopDonation == True:
playWAV("top")
else:
playWAV("regular")
def playWAV(donationType):
if donationType == "top":
winsound.PlaySound(resourcePath(topDonationWAV, user=True), winsound.SND_FILENAME)
else:
winsound.PlaySound(resourcePath(donationWAV, user=True), winsound.SND_FILENAME)
Config = ConfigParser.ConfigParser()
Config.read(resourcePath('settings.ini', user=True))
channel = Config.get("Donation Tracker Config", 'Channel').lower()
key = Config.get("Donation Tracker Config", 'API-Key')
dailyTopDonation = str2bool(Config.get("Donation Tracker Config", 'DailyTopDonation'))
dailyRecentDonation = str2bool(Config.get("Donation Tracker Config", 'DailyRecentDonation'))
dailyDonationList = str2bool(Config.get("Donation Tracker Config", 'DailyDonationList'))
playWAVonDonation = str2bool(Config.get("Donation Tracker Config", 'PlayWAV'))
if playWAVonDonation == True:
donationWAV = Config.get("Donation Tracker Config", 'DonationWAV')
topDonationWAV = Config.get("Donation Tracker Config", 'TopDonationWAV')
donationFormatting = Config.get("Donation Tracker Config", 'DonationFormatting').encode("utf-8", errors='ignore')
donationListFormatting = Config.get("Donation Tracker Config", 'DonationListFormatting').encode("utf-8", errors='ignore')
topDonationFormatting = Config.get("Donation Tracker Config", 'TopDonationFormatting').encode("utf-8", errors='ignore')
consoleDonationFormatting = Config.get("Donation Tracker Config", 'ConsoleFormatting').encode("utf-8", errors='ignore')
donationListAmount = int(Config.get("Donation Tracker Config", 'DonationListAmount'))
soundMinimum = float(Config.get("Donation Tracker Config", 'SoundMinimum'))
fileMinimum = float(Config.get("Donation Tracker Config", 'FileMinimum'))
if not channel:
raise Exception('Channel is blank in config')
if not key:
raise Exception('API-Key is blank in config')
socketIO = SocketIO('http://www.streamdonations.net', verify=False) # , 443, verify=cacert_location
socketIO.on('login', login)
socketIO.on('authenticated', authenticated)
socketIO.on('authentication failed', authenticationFailed)
socketIO.on('new donation', newDonation)
socketIO.wait()
def start_tracking(internal_resource_path):
global internal_working_dir
global cacert_location
internal_working_dir = internal_resource_path
#os.environ['REQUESTS_CA_BUNDLE'] = resourcePath('cacert.pem')
#cacert_location = os.environ['REQUESTS_CA_BUNDLE']
print "Donation Tracker v%s" % version
print ''
while True:
try:
getConnected()
except Exception, e:
if ("No section" in str(e)):
print "Your settings.ini file is missing or corrupted. Please exit and update the configuration."
time.sleep(30)
elif ("No option" in str(e)):
print e
print "In other words, the option is missing/malformed in your configuration. Please exit and update the configuration."
time.sleep(30)
elif ("HTTP Error" in str(e)):
print e
print "It looks like my server is having some issues. Please exit and update the configuration."
time.sleep(30)
else:
print e
print "Interesting.. Please exit and update the configuration."
time.sleep(30)
def resourcePath(relative_path = '', user = False):
base_dir = ''
if getattr(sys, 'frozen', False):
# running in a PyInstaller bundle
if (user == False):
# bundle dir
base_dir = internal_working_dir
else:
# user access dir
base_dir = os.path.dirname(sys.executable)
else:
# running in normal Python environment
base_dir = os.path.dirname(__file__)
return os.path.join(base_dir, relative_path)
if __name__ == "__main__":
start_tracking(os.path.dirname(__file__))