mirror of
https://gitlab.com/gpvkt/twitchtts.git
synced 2025-04-21 19:22:26 +02:00
Added integrated OAuth token generator
This commit is contained in:
parent
40108b282b
commit
def6879ff3
2 changed files with 85 additions and 41 deletions
|
@ -83,6 +83,8 @@ whitelist:
|
||||||
* `server`: Twitch IRC server to be used (default should be fine)
|
* `server`: Twitch IRC server to be used (default should be fine)
|
||||||
* `clearmsg_timeout`: Time to wait for an moderator to delete a message, before it's added to the TTS queue
|
* `clearmsg_timeout`: Time to wait for an moderator to delete a message, before it's added to the TTS queue
|
||||||
|
|
||||||
|
You can generate your `oauth_token` by leaving the value empty when starting `tts.exe/tts.py`. The integrated webserver will then provide an OAuth-Generator. (Due to limitations to the `redirect_url` parameter used by twitch, this is only possible if you use Port `8080` or `80` as `http:bind`.)
|
||||||
|
|
||||||
Please note that the `oauth_token` is valid for approximately 60 days. If it become invalid the bot will not connect anymore and you will have to renew the token.
|
Please note that the `oauth_token` is valid for approximately 60 days. If it become invalid the bot will not connect anymore and you will have to renew the token.
|
||||||
|
|
||||||
##### http
|
##### http
|
||||||
|
|
44
tts.py
44
tts.py
|
@ -27,6 +27,8 @@ import sys
|
||||||
import time
|
import time
|
||||||
import datetime
|
import datetime
|
||||||
import socketserver
|
import socketserver
|
||||||
|
import urllib.request
|
||||||
|
import urllib.parse
|
||||||
|
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
from http.server import BaseHTTPRequestHandler
|
from http.server import BaseHTTPRequestHandler
|
||||||
|
@ -398,6 +400,39 @@ class HTTPserv(BaseHTTPRequestHandler):
|
||||||
self.end_headers()
|
self.end_headers()
|
||||||
self.wfile.write(bytes("Internal Server error\n", "utf-8"))
|
self.wfile.write(bytes("Internal Server error\n", "utf-8"))
|
||||||
|
|
||||||
|
elif self.path.startswith('/token') and conf['IRC_OAUTH_TOKEN'] == "Invalid":
|
||||||
|
data = {}
|
||||||
|
data['client_id'] = "ebo548vs6tq54c9zlrgin2yfzzlrrs"
|
||||||
|
data['response_type'] = "token"
|
||||||
|
data['scope'] = "chat:edit chat:read"
|
||||||
|
if conf['HTTP_PORT'] == 80:
|
||||||
|
data['redirect_uri'] = "http://localhost/token"
|
||||||
|
elif conf['HTTP_PORT'] == 8080:
|
||||||
|
data['redirect_uri'] = "http://localhost:8080/token"
|
||||||
|
else:
|
||||||
|
self.send_response(500)
|
||||||
|
self.send_header('Content-type', 'text/plain')
|
||||||
|
self.end_headers()
|
||||||
|
self.wfile.write(bytes("You can only use this function if HTTP_PORT is 80 or 8080. Please change your port or use https://www.21x9.org/twitch instead.\n", "utf-8"))
|
||||||
|
return False
|
||||||
|
|
||||||
|
try:
|
||||||
|
url_values = urllib.parse.urlencode(data)
|
||||||
|
url = "https://id.twitch.tv/oauth2/authorize"
|
||||||
|
full_url = url + "?" + url_values
|
||||||
|
data = urllib.request.urlopen(full_url)
|
||||||
|
if data:
|
||||||
|
self.send_response(200)
|
||||||
|
self.send_header('Content-type', 'text/html')
|
||||||
|
self.end_headers()
|
||||||
|
self.wfile.write(bytes("<html><head><title>OAuth Token Generator</title></head><body onload=\"displayCode();\"><div id=\"code\"><a href=\""+str(data.geturl())+"\">Click to start the OAuth process.</a></div><script>function displayCode() { var url = window.location.href; var test = url.indexOf(\"access_token\");if (test != -1) { token = url.substring(42,72); document.getElementById(\"code\").innerHTML = \"<p>oauth:\" + token + \"</p>\";}}</script></body></html>\n", "utf-8"))
|
||||||
|
else:
|
||||||
|
self.send_response(500)
|
||||||
|
self.send_header('Content-type', 'text/plain')
|
||||||
|
self.end_headers()
|
||||||
|
self.wfile.write(bytes("Could not get OAuth-URL from Twitch\n", "utf-8"))
|
||||||
|
except:
|
||||||
|
logging.error('Could not fetch OAuth-URL from Twitch.')
|
||||||
else:
|
else:
|
||||||
self.send_response(404)
|
self.send_response(404)
|
||||||
self.send_header('Server', 'TTS')
|
self.send_header('Server', 'TTS')
|
||||||
|
@ -477,7 +512,8 @@ def load_config():
|
||||||
if not conf['IRC_USERNAME']:
|
if not conf['IRC_USERNAME']:
|
||||||
raise ValueError('Please add the bots username to config.yml.')
|
raise ValueError('Please add the bots username to config.yml.')
|
||||||
if not conf['IRC_OAUTH_TOKEN']:
|
if not conf['IRC_OAUTH_TOKEN']:
|
||||||
raise ValueError('Please add the bots oauth-token to config.yml.')
|
conf['IRC_OAUTH_TOKEN'] = "Invalid"
|
||||||
|
return conf
|
||||||
if not conf['IRC_OAUTH_TOKEN'].startswith('oauth:'):
|
if not conf['IRC_OAUTH_TOKEN'].startswith('oauth:'):
|
||||||
raise ValueError('Your oauth-token is invalid, it has to start with: "oauth:"')
|
raise ValueError('Your oauth-token is invalid, it has to start with: "oauth:"')
|
||||||
|
|
||||||
|
@ -506,6 +542,12 @@ def main():
|
||||||
http_thread = Thread(target=http_serve_forever, daemon=True, args=(httpd, ))
|
http_thread = Thread(target=http_serve_forever, daemon=True, args=(httpd, ))
|
||||||
http_thread.start()
|
http_thread.start()
|
||||||
|
|
||||||
|
if conf['IRC_OAUTH_TOKEN'] == "Invalid":
|
||||||
|
logging.error('No OAuth Token, skipping start of IRC bot.')
|
||||||
|
while True:
|
||||||
|
logging.error('Please open http://'+str(conf['HTTP_BIND'])+':'+str(conf['HTTP_PORT'])+'/token to generate your OAuth-Token.')
|
||||||
|
time.sleep(10)
|
||||||
|
else:
|
||||||
logging.info("Starting IRC bot")
|
logging.info("Starting IRC bot")
|
||||||
irc = IRC()
|
irc = IRC()
|
||||||
irc.connect(conf['IRC_SERVER'], 6667, conf['IRC_CHANNEL'], conf['IRC_USERNAME'], conf['IRC_OAUTH_TOKEN'])
|
irc.connect(conf['IRC_SERVER'], 6667, conf['IRC_CHANNEL'], conf['IRC_USERNAME'], conf['IRC_OAUTH_TOKEN'])
|
||||||
|
|
Loading…
Add table
Reference in a new issue