import sys import datetime import json import BaseHTTPServer import SocketServer from threading import Thread from collections import OrderedDict from System.Collections.Generic import List ScriptName = "StreamAvatarsCurrency" Website = "https://www.streamavatars.com" Description = "Allow Stream Avatars to edit your Streamlabs currency" Creator = "Travis Miller" Version = "1.0.0.0" global Handler global httpd global thread class PointsHTTPServer(BaseHTTPServer.BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.send_header("Content-Type", "text/html") self.end_headers() self.wfile.write("OK") def do_POST(self): global ScriptName response_status = 500 response_body = "{}" try: request_body = self.rfile.read(int(self.headers.getheader('content-length'))) message = json.loads(request_body) reply = message_received(message) response_status = reply["status"] response_body = json.dumps(reply["data"]) except Exception as ex: Parent.Log(ScriptName, str(ex)) response_status = 500 response_body = "{}" try: self.send_response(response_status) self.send_header("Content-Type", "application/json") self.end_headers() self.wfile.write(response_body) self.wfile.close() except Exception as ex: Parent.Log(ScriptName, str(ex)) return def http_server_thread(): global httpd httpd.serve_forever() return def Init(): global Handler global httpd global thread Handler = PointsHTTPServer httpd = SocketServer.TCPServer(("127.0.0.1", 6779), Handler) thread = Thread(target = http_server_thread) thread.start() return def Execute(data): return def Tick(): return def ReloadSettings(jsonData): return def Unload(): global httpd httpd.socket.close() httpd.shutdown() return def ScriptToggled(state): if state == True: Unload() Init() else: Unload() return def message_received(message): points = 0 status = 200 response = None if message["method"] == "deposit": success = Parent.AddPoints(message["username"], message["username"], message["points"]) points = Parent.GetPoints(message["username"]) if points < 0: success = Parent.AddPoints(message["username"], message["username"], -points) points = Parent.GetPoints(message["username"]) if success == False: status = 500 elif message["method"] == "withdraw": success = Parent.RemovePoints(message["username"], message["username"], message["points"]) points = Parent.GetPoints(message["username"]) if success == False: status = 500 elif message["method"] == "set": points = message["points"] - Parent.GetPoints(message["username"]) success = Parent.AddPoints(message["username"], message["username"], points) points = Parent.GetPoints(message["username"]) if success == False: status = 500 elif message["method"] == "get": points = Parent.GetPoints(message["username"]) elif message["method"] == "top_points": topX = Parent.GetTopCurrency(message["limit"]) streamingService = Parent.GetStreamingService() if streamingService == "Youtube": for k, v in topX.items(): displayName = Parent.GetDisplayName(k) topX[displayName] = topX.pop(k) topX = OrderedDict(sorted(topX.items(), key=lambda t: t[1], reverse = True)) response = { "status": 200, "data": topX } elif message["method"] == "mass_deposit": streamingService = Parent.GetStreamingService() if streamingService == "Youtube": failedUsers = [] for k, v in message["deposits"].items(): success = Parent.AddPoints(k, k, v) if success == False: failedUsers.append(k) else: failedUsers = list(Parent.AddPointsAll(message["deposits"])) if len(failedUsers) > 0: status = 500 response = { "status": status, "data": failedUsers } elif message["method"] == "mass_withdraw": failedUsers = list(Parent.RemovePointsAll(message["withdrawals"])) if len(failedUsers) > 0: status = 500 response = { "status": status, "data": failedUsers } elif message["method"] == "mass_get": users = Parent.GetPointsAll(List[str](message["usernames"])) users = {k: int(v) for k, v in users.items()} response = { "status": 200, "data": users } if response is None: response = { "status": status, "data": int(points) } return response if __name__ == "__main__": Init()