-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitchClasses.py
56 lines (49 loc) · 2.04 KB
/
twitchClasses.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
import os
import sanic
from sanic import response
class StreamChannel:
def __init__(self, name):
self.name: str = name
self.chat: list = []
self.chatLogPath = os.path.join(os.getcwd(), "Chat_Logs", "Twitch", self.name)
self.giveawayPath = os.path.join(os.getcwd(), "Giveaways", self.name)
if not os.path.exists(self.chatLogPath):
os.makedirs(self.chatLogPath)
if not os.path.exists(self.giveawayPath):
os.makedirs(self.giveawayPath)
self.isLive: bool = False
self.giveawayWord = None
self.giveawayEntrants = []
self.spotifyNameSecret: str
self.spotifyTrack = None
self.channelObject = None
self.currentlyPlaying = None
self.displayName: str = ""
self.id: int = 0
self.webhookToken: str = ""
self.webhookId: int = 0
self.liveTime = 0
self.pollOptions = {}
self.pollEntrants = []
def __repr__(self):
return self.name
async def entrantsFile(self):
return os.path.join(self.giveawayPath, f"{self.giveawayWord}_giveaway_entrants.txt")
async def winnersFile(self):
return os.path.join(self.giveawayPath, f"{self.giveawayWord}_giveaway_winners.txt")
class WebhookServer(sanic.Sanic):
def __init__(self, loop, postCallback):
super().__init__()
self._app = sanic.Sanic(__name__, configure_logging=False)
self._app.add_route(self.handle_post, "/", methods=["POST"])
self._app.add_route(self.handle_get, "/", methods=["GET"])
self._host = "0.0.0.0"
self._port = 8000
self._loop = loop
self._postCallback = postCallback
async def handle_post(self, request):
self._loop.create_task(self._postCallback(request))
return response.HTTPResponse(body="202: OK", status=202)
async def handle_get(self, request):
if "hub.challenge" in request.args:
return response.HTTPResponse(body=request.args['hub.challenge'][0], headers={'Content-Type': 'application/json'})