|
| 1 | +#--------------------------------------- |
| 2 | +# Import Libraries |
| 3 | +#--------------------------------------- |
| 4 | +import sys |
| 5 | +import clr |
| 6 | +import time |
| 7 | +import os |
| 8 | +import json |
| 9 | +import threading |
| 10 | +from datetime import datetime, time as datetime_time, timedelta |
| 11 | +import codecs |
| 12 | + |
| 13 | +clr.AddReference("IronPython.SQLite.dll") |
| 14 | +clr.AddReference("IronPython.Modules.dll") |
| 15 | + |
| 16 | +debuggingMode = True |
| 17 | +ScriptName = "End Of Stream Timer" |
| 18 | +Website = "http://www.edog0049a.com" |
| 19 | +Description = "When Will It end" |
| 20 | +Creator = "Edog" |
| 21 | +Version = "9.0" |
| 22 | + |
| 23 | +SettingsFile = "Settings.json" |
| 24 | +Path = os.path.dirname(__file__) |
| 25 | +Settings = {} |
| 26 | +eventDATA={} |
| 27 | +TimeFile = "end_time.txt" |
| 28 | +TimeDir = "overlay\\" |
| 29 | +TimerActive = False |
| 30 | +timestampOfflineSince = 0 |
| 31 | +TmpTime="" |
| 32 | +OldTime="" |
| 33 | + |
| 34 | +def Init(): |
| 35 | + global TimeFile, Settings, SettingsFile, Path, TmpTime, OldTime, waiting |
| 36 | + try:# create subfolder if it doesnt exist |
| 37 | + if not os.path.exists(os.path.dirname(os.path.join(Path, TimeDir))): |
| 38 | + os.makedirs(os.path.dirname(os.path.join(Path,TimeDir))) |
| 39 | + # create overlay files if they dont exist |
| 40 | + if not os.path.exists(os.path.join(Path,TimeDir + TimeFile)): |
| 41 | + with open(os.path.join(Path, TimeDir + TimeFile), "w+") as f: |
| 42 | + f.write(" ") |
| 43 | + except: |
| 44 | + Parent.SendTwitchMessage(("Failed to make timer file!!") [:490]) |
| 45 | + |
| 46 | + try: |
| 47 | + with codecs.open(os.path.join(Path, SettingsFile), encoding='utf-8-sig', mode='r') as file: |
| 48 | + Settings = json.load(file, encoding='utf-8-sig') |
| 49 | + TmpTime=Settings["EndTime"] |
| 50 | + OldTime=Settings["EndTime"] |
| 51 | + except: |
| 52 | + Parent.SendTwitchMessage(("Settings failed to load") [:490]) |
| 53 | + return |
| 54 | + |
| 55 | +def Execute(data): |
| 56 | + global Settings,TimerActive |
| 57 | + try: |
| 58 | + if data.IsChatMessage(): |
| 59 | + if data.GetParamCount() >= 2 and data.GetParam(0).lower() == "!timer" and data.IsWhisper(): |
| 60 | + #check commands and permissions P_[command]=command permission |
| 61 | + if data.GetParam(1).lower() == "start" and Parent.HasPermission(data.User, Settings["P_start"], ""): |
| 62 | + if TimerActive: #check if timer is running |
| 63 | + Parent.SendTwitchWhisper(data.User,"Timer is active!") |
| 64 | + else: #if timer off start timer |
| 65 | + Start() |
| 66 | + Parent.SendTwitchWhisper(data.User,"Timer is started!") |
| 67 | + |
| 68 | + elif data.GetParam(1).lower() == "stop" and Parent.HasPermission(data.User, Settings["P_stop"], ""): |
| 69 | + Stop() |
| 70 | + Parent.SendTwitchWhisper(data.User, "Timer is stopped!") |
| 71 | + elif data.GetParam(1).lower() == "addmin" and Parent.HasPermission(data.User, Settings["P_addmin"], ""): |
| 72 | + if data.GetParam(2): |
| 73 | + AddMin(int(data.GetParam(2))) |
| 74 | + Parent.SendTwitchWhisper(data.User, "Added " + data.GetParam(2) + " Minutes!") |
| 75 | + else: |
| 76 | + AddMin() |
| 77 | + Parent.SendTwitchWhisper(data.User, "Added 15 Minutes!") |
| 78 | + |
| 79 | + elif data.GetParam(1).lower() == "addhr" and Parent.HasPermission(data.User, Settings["P_addhr"], ""): |
| 80 | + if data.GetParam(2): |
| 81 | + AddHour(int(data.GetParam(2))) |
| 82 | + Parent.SendTwitchWhisper(data.User, "Added " + data.GetParam(2) + " Hour!") |
| 83 | + else: |
| 84 | + AddHour() |
| 85 | + Parent.SendTwitchWhisper(data.User, "Added 1 Hour!") |
| 86 | + |
| 87 | + elif data.GetParam(1).lower() == "gettime" and Parent.HasPermission(data.User, Settings["P_gettime"], ""): |
| 88 | + Parent.SendTwitchWhisper(data.User,"Timer is set to: " + Settings["EndTime"]) |
| 89 | + |
| 90 | + elif data.GetParam(1).lower() == "timeleft" and Parent.HasPermission(data.User, Settings["P_timeleft"], ""): |
| 91 | + Parent.SendTwitchWhisper(data.User,"Time to end: " + str(TimeDiff())) |
| 92 | + |
| 93 | + elif data.GetParam(1).lower() == "settime" and Parent.HasPermission(data.User, Settings["P_settime"], ""): |
| 94 | + setTime(data.GetParam(2)) |
| 95 | + Parent.SendTwitchWhisper(data.User,"Time set to: " + Settings["EndTime"]) |
| 96 | + |
| 97 | + elif data.GetParam(1).lower() == "setendmsg" and Parent.HasPermission(data.User, Settings["P_setendmsg"], ""): |
| 98 | + setEndMsg(data.Message.replace(data.GetParam(0) + " " + data.GetParam(1),"")) |
| 99 | + Parent.SendTwitchWhisper(data.User,"New end message set to: " + Settings["EndMsg"]) |
| 100 | + |
| 101 | + elif data.GetParam(1).lower() == "setmsg" and Parent.HasPermission(data.User, Settings["P_setmsg"], ""): |
| 102 | + setDisplayMsg(data.Message.replace(data.GetParam(0) + " " + data.GetParam(1),"")) |
| 103 | + Parent.SendTwitchWhisper(data.User,"New message set to: " + Settings["DisplayMsg"]) |
| 104 | + |
| 105 | + elif data.GetParam(1).lower() == "help" and Parent.HasPermission(data.User, Settings["P_setmsg"], ""): |
| 106 | + Parent.SendTwitchWhisper(data.User,"\"!timer start\" - Starts timer ******* \"!timer stop\" - Stops timer ******* \"!timer gettime\" - Gets time to end timer ******* \"!timer settime\" - sets the end time format(\'00:00:00\' 24hr) ******* ") |
| 107 | + except ValueError,e: |
| 108 | + Parent.SendTwitchWhisper(data.User,("Command Failed: Please check formats! ERR: \" " + str(e)) + "\"" [:490]) |
| 109 | + return |
| 110 | + |
| 111 | +def WriteTimerFile(msg): |
| 112 | + global TimeFile, TimeDir |
| 113 | + try: |
| 114 | + with codecs.open(os.path.join(Path, TimeDir + TimeFile), encoding='utf-8-sig', mode="w+") as file: |
| 115 | + file.write(msg) |
| 116 | + except : |
| 117 | + Debug("err") |
| 118 | + return |
| 119 | + |
| 120 | +def StartTimer(): |
| 121 | + global TimerActive, Settings, TmpTime, Path |
| 122 | + TimerActive=True |
| 123 | + while TimerActive == True: |
| 124 | + WriteTimerFile(Settings["DisplayMsg"] + " " + str(TimeDiff(TmpTime))) |
| 125 | + if TimeDiff(TmpTime).total_seconds() == int(Settings["ChatSoonSlider"]) * 60 and Settings["ChatSoonEnabled"]: |
| 126 | + Parent.SendTwitchMessage(str(Settings["ChatSoonMsg"]).replace("$min", str(int(Settings["ChatSoonSlider"]))) [:490]) |
| 127 | + if TimeDiff(TmpTime).total_seconds() == 0: |
| 128 | + WriteTimerFile(Settings["EndMsg"]) |
| 129 | + if Settings["ChatEndEnabled"]: |
| 130 | + Parent.SendTwitchMessage((Settings["ChatEndMsg"]) [:490]) |
| 131 | + TimerActive=False |
| 132 | + if Settings["SoundEnabled"]: |
| 133 | + PlaySound() |
| 134 | + TmpTime=Settings["EndTime"] |
| 135 | + break |
| 136 | + time.sleep(1) |
| 137 | + |
| 138 | +def Start(): |
| 139 | + global TimerActive, Settings , TmpTime |
| 140 | + EventData("EVENT_START") |
| 141 | + Startthread() |
| 142 | + return |
| 143 | + |
| 144 | +def Startthread(): |
| 145 | + global TimerActive, Settings , TmpTime |
| 146 | + |
| 147 | + if not TimerActive: |
| 148 | + TimerThread=threading.Thread(target=StartTimer, args=()) |
| 149 | + TimerThread.start() |
| 150 | + return |
| 151 | + |
| 152 | +def Stop(): |
| 153 | + global TimerActive, TmpTime, Settings |
| 154 | + EventData("EVENT_STOP") |
| 155 | + TimerActive=False |
| 156 | + TmpTime = Settings["EndTime"] |
| 157 | + WriteTimerFile("Timer Stopped!") |
| 158 | + return |
| 159 | + |
| 160 | +def AddMin(min = 15): |
| 161 | + global TmpTime,TimerActive |
| 162 | + try: |
| 163 | + if TimerActive: |
| 164 | + tmp=TmpTime.split(":") |
| 165 | + else: |
| 166 | + tmp=str(datetime.now().time().strftime('%H:%M:%S')).split(":") |
| 167 | + tmp[1] = int(tmp[1]) + min |
| 168 | + if int(tmp[1]) > 59: |
| 169 | + hr=int(tmp[1])/60 |
| 170 | + tmp[0]=int(tmp[0]) + hr |
| 171 | + if int(tmp[0]) >= 24: |
| 172 | + tmp[0] = 0 |
| 173 | + tmp[1] = int(tmp[1]) - (hr * 60) |
| 174 | + TmpTime = str(tmp[0]) + ":" + str(tmp[1]) + ":" + str(tmp[2]) |
| 175 | + EventData("EVENT_ADD_MIN") |
| 176 | + Startthread() |
| 177 | + return True |
| 178 | + except: |
| 179 | + return False |
| 180 | + |
| 181 | +def AddHour(hr = 1): |
| 182 | + global TmpTime,TimerActive |
| 183 | + try: |
| 184 | + if TimerActive: |
| 185 | + tmp=TmpTime.split(":") |
| 186 | + else: |
| 187 | + tmp=str(datetime.now().time().strftime('%H:%M:%S')).split(":") |
| 188 | + tmp[0] = int(tmp[0]) + hr |
| 189 | + if int(tmp[0]) >= 24: |
| 190 | + tmp[0] = int(tmp[0]) - 24 |
| 191 | + TmpTime = str(tmp[0]) + ":" + str(tmp[1]) + ":" + str(tmp[2]) |
| 192 | + EventData("EVENT_ADD_HOUR") |
| 193 | + Startthread() |
| 194 | + return True |
| 195 | + except: |
| 196 | + return False |
| 197 | + |
| 198 | +def setTime(stime): |
| 199 | + global Settings |
| 200 | + assert datetime.strptime(stime,"%H:%M:%S"), "Please format time 00:00:00" |
| 201 | + Settings["EndTime"] = stime |
| 202 | + Save() |
| 203 | + return |
| 204 | + |
| 205 | +def setEndMsg(msg): |
| 206 | + global Settings |
| 207 | + |
| 208 | + Settings["EndMsg"] = msg |
| 209 | + Save() |
| 210 | + return |
| 211 | + |
| 212 | +def setDisplayMsg(msg): |
| 213 | + global Settings |
| 214 | + Settings["DisplayMsg"] = msg |
| 215 | + Debug(msg) |
| 216 | + Save() |
| 217 | + return |
| 218 | + |
| 219 | +def Tick(): |
| 220 | + global timestampOfflineSince,Settings |
| 221 | + if Parent.IsLive(): |
| 222 | + if Settings["AutoStart"] and not TimerActive and (( time.time() - timestampOfflineSince > 60) or timestampOfflineSince == 0): |
| 223 | + Start() |
| 224 | + timestampOfflineSince = time.time() |
| 225 | + return |
| 226 | + |
| 227 | +def Unload(): |
| 228 | + Stop() |
| 229 | + return |
| 230 | + |
| 231 | +def Debug(message): |
| 232 | + if debuggingMode: |
| 233 | + Parent.Log("End of streamless",message) |
| 234 | + return |
| 235 | + |
| 236 | +def ReloadSettings(jsonData): |
| 237 | + global Settings, TmpTime, OldTime |
| 238 | + Settings=json.loads(jsonData, encoding='utf-8-sig') |
| 239 | + if OldTime != Settings["EndTime"] and TmpTime == OldTime: |
| 240 | + TmpTime = Settings["EndTime"] |
| 241 | + OldTime = Settings["EndTime"] |
| 242 | + EventData("EVENT_UPDATE") |
| 243 | + return |
| 244 | + |
| 245 | +def TimeDiff(EndTime): |
| 246 | + Start = datetime.strptime(str(datetime.now().time().strftime('%H:%M:%S')),'%H:%M:%S') |
| 247 | + End = datetime.strptime(EndTime,'%H:%M:%S') |
| 248 | + if Start > End: |
| 249 | + End += timedelta(1) |
| 250 | + assert End > Start |
| 251 | + return End - Start |
| 252 | + |
| 253 | +def Open(): |
| 254 | + global Path, TimeDir |
| 255 | + os.startfile(os.path.join(Path,TimeDir)) |
| 256 | + return |
| 257 | + |
| 258 | +def OpenSounds(): |
| 259 | + global Path |
| 260 | + os.startfile(os.path.join(Path,"Sounds")) |
| 261 | + return |
| 262 | + |
| 263 | +def Save(): |
| 264 | + global SettingsFile |
| 265 | + with codecs.open(os.path.join(Path, SettingsFile), encoding='utf-8-sig', mode='w+') as file: |
| 266 | + json.dump(Settings, file) |
| 267 | + with codecs.open(os.path.join(Path, SettingsFile), encoding='utf-8-sig', mode='r') as file: |
| 268 | + ReloadSettings(json.dumps(json.load(file, encoding='utf-8-sig'))) |
| 269 | + return |
| 270 | + |
| 271 | +def PlaySound(): |
| 272 | + global Path, Settings |
| 273 | + Parent.PlaySound(os.path.join( Path,"Sounds\\" + Settings["SoundFile"]),float(Settings["SoundVolume"])/100) |
| 274 | + |
| 275 | +def EventData(event): |
| 276 | + global Settings,TmpTime, eventDATA |
| 277 | + t=TmpTime.split(":") |
| 278 | + eventDATA["Hr"] = t[0] |
| 279 | + eventDATA["Min"] = t[1] |
| 280 | + eventDATA["DisplayMsg"] = Settings["DisplayMsg"] |
| 281 | + eventDATA["EndMsg"] = Settings["EndMsg"] |
| 282 | + eventDATA["TextSize"] = Settings["TextSizeSlider"] |
| 283 | + eventDATA["Style"] = Settings["Style"] |
| 284 | + eventDATA["RainbowSlider"] = Settings["RainbowSlider"] |
| 285 | + eventDATA["Colors"] = Settings["Colors"] |
| 286 | + eventDATA["Color1"] = Settings["Color1"] |
| 287 | + eventDATA["Color2"] = Settings["Color2"] |
| 288 | + eventDATA["Color3"] = Settings["Color3"] |
| 289 | + eventDATA["Color4"] = Settings["Color4"] |
| 290 | + eventDATA["Color5"] = Settings["Color5"] |
| 291 | + eventDATA["Color6"] = Settings["Color6"] |
| 292 | + eventDATA["Color7"] = Settings["Color7"] |
| 293 | + data=json.dumps(eventDATA) |
| 294 | + Parent.BroadcastWsEvent(event,data) |
| 295 | + return |
| 296 | + |
| 297 | + |
| 298 | + |
0 commit comments