-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
247 lines (191 loc) · 7.05 KB
/
main.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
# This file is ugly and needs to be split and organized ;w;
import asyncio
from contextlib import asynccontextmanager
import json
import os
import logging
import sys
from dotenv import load_dotenv
from fastapi import FastAPI, Request, WebSocket
from twitchAPI.twitch import Twitch
from twitchAPI.oauth import UserAuthenticator
from twitchAPI.type import (
AuthScope,
AuthType,
TwitchAPIException,
)
from twitchAPI.eventsub.webhook import EventSubWebhook
from twitchAPI.object.eventsub import ChannelPointsCustomRewardRedemptionAddEvent
from twitchAPI.helper import first
load_dotenv()
APP_HOST = os.getenv("APP_HOST", "localhost")
APP_PORT = os.getenv("APP_PORT")
APP_SCHEME = os.getenv("APP_SCHEME", "http")
EVENTSUB_USERNAME = os.getenv("EVENTSUB_USERNAME")
EVENTSUB_URL = os.getenv("EVENTSUB_URL")
EVENTSUB_PORT = int(os.getenv("EVENTSUB_PORT", "8081"))
TWITCH_APP_ID = os.getenv("TWITCH_APP_ID")
TWITCH_APP_SECRET = os.getenv("TWITCH_APP_SECRET")
TARGET_SCOPE = [
AuthScope.CHANNEL_READ_ADS,
AuthScope.CHANNEL_READ_GOALS,
AuthScope.CHANNEL_READ_SUBSCRIPTIONS,
AuthScope.CHANNEL_READ_POLLS,
AuthScope.CHANNEL_READ_PREDICTIONS,
AuthScope.CHANNEL_READ_REDEMPTIONS,
AuthScope.CHANNEL_MANAGE_REDEMPTIONS,
]
logger = logging.getLogger("uvicorn.error")
@asynccontextmanager
async def lifespan(app: FastAPI):
await twitch_setup()
yield
await close_twitch()
app = FastAPI(lifespan=lifespan)
twitch: Twitch
auth: UserAuthenticator
eventsub: EventSubWebhook
class ConnectionManager:
def __init__(self):
self.active_connections: list[WebSocket] = []
async def connect(self, websocket: WebSocket):
await websocket.accept()
self.active_connections.append(websocket)
def disconnect(self, websocket: WebSocket):
self.active_connections.remove(websocket)
async def broadcast(self, message: str):
for connection in self.active_connections:
await connection.send_text(message)
manager = ConnectionManager()
def get_url(path: str):
if APP_PORT:
return f"{APP_SCHEME}://{APP_HOST}:{APP_PORT}{path}"
return f"{APP_SCHEME}://{APP_HOST}{path}"
@app.get("/")
async def get_status(request: Request):
global twitch
return {
"success": True,
"message": "Server is running",
"twitch": twitch.has_required_auth(AuthType.USER, TARGET_SCOPE),
"login_url": get_url("/login"),
"logout_url": get_url("/logout"),
"eventsub_url": EVENTSUB_URL,
"eventsub_port": EVENTSUB_PORT,
"eventsub_username": EVENTSUB_USERNAME
}
@app.get("/login")
async def login(request: Request):
global twitch, auth
if twitch.get_user_auth_token() is not None:
return {
"success": True,
"message": "Already authenticated",
"logout_url": get_url("/logout"),
}
return {
"success": True,
"message": "Please visit the following URL to authenticate",
"url": auth.return_auth_url(),
}
@app.get("/login/callback")
async def login_callback(request: Request):
global token, refresh, eventsub
state = request.query_params.get("state")
if state != auth.state:
return {"success": False, "error": "Invalid state"}
code = request.query_params.get("code")
if code is None:
return {"success": False, "error": "No code provided"}
try:
result = await auth.authenticate(user_token=code)
if result is None:
return {"success": False, "error": "Failed to authenticate"}
token, refresh = result
await twitch.set_user_authentication(token, TARGET_SCOPE, refresh)
except TwitchAPIException as e:
logger.error(f"Failed to authenticate: {e}")
return {"success": False, "error": "Failed to authenticate"}
with open("user_token.json", "w") as f:
json.dump({"token": token, "refresh": refresh}, f)
logger.info("Authenticated with Twitch")
return {"success": True, "message": "You may now close this window"}
@app.get("/logout")
async def logout(request: Request):
global twitch
await twitch.close()
with open("user_token.json", "w") as f:
json.dump({"token": "", "refresh": ""}, f)
await twitch_setup(app) # type: ignore
logger.info("Logged out")
return dict(success=True, message="Logged out")
@app.websocket("/ws")
async def websocket(ws: WebSocket):
await manager.connect(ws)
while True:
data = await ws.receive_text()
await ws.send_text(f"Message text was: {data}")
async def close_twitch():
global twitch, eventsub
await twitch.close()
async def on_redeem(data: ChannelPointsCustomRewardRedemptionAddEvent):
logger.debug(
f"Redeemed {data.event.reward.title} by {data.event.user_name} with message {data.event.user_input}"
)
await manager.broadcast(f"Redeemed {data.event.reward.title} by {data.event.user_name} with message {data.event.user_input}")
async def refresh_callback(token, refresh):
logger.info("Refreshing token")
with open("user_token.json", "w") as f:
json.dump({"token": token, "refresh": refresh}, f)
return
async def twitch_setup():
global twitch, auth
if TWITCH_APP_ID is None or TWITCH_APP_SECRET is None:
logger.error(
"Please set TWITCH_APP_ID and TWITCH_APP_SECRET environment variables"
)
return
twitch = await Twitch(TWITCH_APP_ID, TWITCH_APP_SECRET)
auth = UserAuthenticator(
twitch, TARGET_SCOPE, url=get_url("/login/callback"), force_verify=True
)
twitch.user_auth_refresh_callback = refresh_callback
try:
with open("user_token.json", "r") as f:
data = json.load(f)
token = data["token"]
refresh = data["refresh"]
try:
await twitch.set_user_authentication(token, TARGET_SCOPE, refresh)
except TwitchAPIException:
logger.info("No token found, please authenticate: " + get_url("/login"))
return
logger.info("Authenticated with stored token.")
except FileNotFoundError:
logger.info("No token found, please authenticate: " + get_url("/login"))
return
await eventsub_setup()
async def eventsub_setup():
global twitch, eventsub
if EVENTSUB_USERNAME is None or EVENTSUB_URL is None:
logger.error(
"Please set EVENTSUB_USERNAME and EVENTSUB_URL environment variables"
)
return
eventsub = EventSubWebhook(
EVENTSUB_URL, EVENTSUB_PORT, twitch
)
user = await first(twitch.get_users(logins=[EVENTSUB_USERNAME]))
if user is None:
logger.error(f"User {EVENTSUB_USERNAME} not found")
return
await eventsub.unsubscribe_all()
eventsub.start()
try:
await eventsub.listen_channel_points_custom_reward_redemption_add(
user.id, on_redeem
)
except Exception as e:
logger.error(f"Failed to subscribe to event!")
logger.info("EventSub setup complete!")
return