-
Notifications
You must be signed in to change notification settings - Fork 0
/
Currency_StreamlabsSystem.py
322 lines (263 loc) · 9.36 KB
/
Currency_StreamlabsSystem.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# -*- coding: utf-8 -*-
import sys
sys.platform = "win32"
import os, threading, json, codecs, traceback, time
from time import localtime, strftime
from datetime import timedelta
#NOTE:
#Conflicts with the Visual Studio Python installation.
#Use the normal Python installation for streamlabs chatbot!
#---------------------------
# [Required] Script Information
#---------------------------
ScriptName = "[Currency]"
Website = "https://github.com/erickraemer/currency"
Description = "An enhancement script for the streamlabs loyality system which rewards your viewers with points for watching your stream."
Creator = "Eric Krämer"
Version = "1.0.0.4"
#returns script folder path + filename
def getPath(filename):
return os.path.join(os.path.dirname(__file__), filename)
def openWebsite():
os.system("start \"\" {}".format(Website))
return
#set variables
def Init():
global settings
global stopEvent
global scoreSummary
global logActive
global logData
global logPath
global decayLog
global thread
logActive = False
logData = list()
logPath = getPath("currency.log")
thread = None
stopEvent = threading.Event()
settings = ScriptSettings()
scoreSummary = dict()
if settings.Valid and settings.DecayActive:
decayLog = DecayTracker("decaylog.json")
return
#stop thread and calculate point decay
def Unload():
if thread and thread.isAlive():
stopEvent.set()
thread.join()
sendDiscordInfo()
return
def Tick():
return
def Execute(Data):
return
#stop or start thread
def ScriptToggled(state):
global thread
if not settings.Valid:
Error("Settings could not be loaded, did you hit the \"Save Settings\" button?")
return
if state and not (thread and thread.isAlive()):
thread = threading.Thread(target=exceptionCatcher)
stopEvent.clear()
thread.start()
elif not state and thread and thread.isAlive():
stopEvent.set()
thread.join()
return
def Reloadsettings(jsonData):
settings.Reload(jsonData)
return
#contains user settings
class ScriptSettings():
def __init__(self):
self.__load()
def reload(self, jsonData):
self.__load()
def __load(self):
self.Valid = False
try:
#try loading settings
file = getPath("settings.json")
with codecs.open(file, encoding="utf-8-sig", mode="r") as f:
self.__dict__ = json.load(f, encoding="utf-8")
except:
return
#set time format
self.PayoutAmount = int(self.PayoutAmount)
self.PayoutInterval = timedelta(minutes=self.PayoutInterval)
self.DecayCooldown = timedelta(hours=self.DecayCooldown)
self.DecayInterval = timedelta(hours=self.DecayInterval)
self.DecayViewerAmount = int(filter(str.isdigit, self.DecayViewerAmount))
self.DecayFixed = self.DecayFixed == "Fixed"
self.Valid = True
self.PollRate = 30
return
class DecayTracker():
def __init__(self, filename):
self.data = dict()
self.__path = getPath(filename)
self.load()
return
def __getitem__(self, key):
return self.data[key]
def __setitem__(self, key, value):
self.data[key] = value
return
def update(self):
sotv = set(Parent.GetTopCurrency(settings.DecayViewerAmount).keys())
dlKeys = set(self.data.keys())
difA = dlKeys.difference(sotv)
difB = sotv.difference(dlKeys)
if difA or difB:
self.data.update(dict.fromkeys(difB, timedelta()))
for e in difA:
del self.data[e]
self.save()
return
def load(self):
if os.path.isfile(self.__path):
with open(self.__path, "r") as f:
temp = json.loads(f.read())
for k,v in temp.iteritems():
self.data[k] = timedelta(minutes=v)
self.update()
return
dotv = Parent.GetTopCurrency(settings.DecayViewerAmount)
for k in dotv.keys():
dotv[k] = timedelta()
self.data = dotv
self.save()
return
def save(self):
temp = dict()
with open(self.__path, "w") as f:
for k,v in self.data.iteritems():
temp[k] = int(v.total_seconds() / 60)
f.write(json.dumps(temp))
return
#catches exceptions from the thread and prints them to the log console
def exceptionCatcher():
try:
run()
except BaseException as ex:
Log(str(traceback.format_exc()))
return
#check if stream is live and add points after PayoutInterval
def run():
#wait until stream is live or script is stopped
while not Parent.IsLive():
if stopEvent.wait(settings.PollRate):
return
setupLogging()
nextPayout = time.time() + settings.PayoutInterval.total_seconds() - (time.time() % settings.PayoutInterval.total_seconds())
while Parent.IsLive():
if stopEvent.wait(nextPayout - time.time()):
break
nextPayout += settings.PayoutInterval.total_seconds()
payoutPoints()
return
#write saved log info to file if the stream goes live
def setupLogging():
global logActive
global logData
logActive = True
with open(logPath, "w") as f:
f.write("Date: {}\n".format(strftime("%d %B %Y", localtime())))
for s in logData:
f.write("{}\n".format(s))
logData[:] = []
return
#add points for every active viewer and check session presence
def payoutPoints():
#add points for viewers
viewer = getActiveUsers()
if not viewer:
Log("No viewers to give points to!")
return
for v in viewer:
addPoints(v, settings.PayoutAmount)
updateDecayLog(viewer)
payoutNotification(viewer)
return
def payoutNotification(viewer):
msg = "+{} {} for: {}".format(settings.PayoutAmount,
Parent.GetCurrencyName(),
", ".join(Parent.GetDisplayName(v) for v in viewer))
Log(msg)
if settings.AnnouncePayout:
Parent.SendStreamMessage("/me {}".format(msg.replace(", ", ", ~")))
return
def getActiveUsers():
viewer = set()
for user in Parent.GetActiveUsers():
if Parent.GetDisplayName(user) == Parent.GetChannelName():
continue
if Parent.GetPoints(user) <= 0:
link = "https://api.crunchprank.net/twitch/followed/{}/{}".format(Parent.GetChannelName(), user)
result = Parent.GetRequest(link, {})
if (json.loads(result)["status"] != 200 or
"-" not in json.loads(result)["response"]):
continue
viewer.add(user)
return viewer
def updateDecayLog(viewer):
if not settings.DecayActive:
return
decayLog.update()
#reset or increase decay for viewers
for k in decayLog.data.keys():
if k in viewer:
decayLog[k] = timedelta()
else:
checkDecay(k)
decayLog.save()
return
def checkDecay(user):
cooldownReached = decayLog[user] + settings.PayoutInterval >= settings.DecayCooldown
decayIntervalReached = (decayLog[user] < settings.DecayCooldown or timedelta(seconds=(decayLog[user] - settings.DecayCooldown).total_seconds() % settings.DecayInterval.total_seconds()) + settings.PayoutInterval >= settings.DecayInterval)
decayLog[user] += settings.PayoutInterval
if not (cooldownReached and decayIntervalReached):
return
percent = getDecayAmount(user)
decay = int((percent / 100.0) * Parent.GetPoints(user))
removePoints(user, decay)
Log("{}% decay (-{} {}) for: {}, last seen {} hours ago".format(percent, decay, Parent.GetCurrencyName(), Parent.GetDisplayName(user), int(decayLog[user].total_seconds() / 3600)))
return
def getDecayAmount(user):
if settings.DecayFixed:
return settings.DecayAmount
return int((decayLog[user] - settings.DecayCooldown + settings.DecayInterval).total_seconds() / settings.DecayInterval.total_seconds())
#sends a discord information at the end of the stream
def sendDiscordInfo():
if not scoreSummary:
return
msg1 = ", ".join(": ".join((Parent.GetDisplayName(k),"+{}".format(v) if v > 0 else str(v))) for k,v in sorted(scoreSummary.iteritems(), key=lambda (k,v): (-v,k)))
msg = "Today's Score:\n{}".format(msg1);
Log(msg)
if settings.AnnounceDiscord:
Parent.SendDiscordMessage(msg)
return
def addPoints(user, amount):
Parent.AddPoints(user, settings.PayoutAmount)
scoreSummary[user] = scoreSummary.get(user, 0) + settings.PayoutAmount
return
def removePoints(user, amount):
Parent.RemovePoints(user, amount)
scoreSummary[user] = scoreSummary.get(user, 0) - amount
return
#write log to file after stream goes live
#else save info for later
def Log(msg):
msg = "{} {}".format(strftime("%H:%M", localtime()), msg)
Parent.Log(ScriptName, msg)
if not logActive:
logData.append(msg)
return
with open(logPath, "a") as f:
f.write("{}\n".format(msg))
return
def Error(msg):
Parent.SendStreamWhisper(Parent.GetChannelName(), "{}: {}".format(ScriptName, msg))
return