-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
62 lines (47 loc) · 1.93 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
import sys
from urllib.parse import urlencode, parse_qs
from PyQt5 import QtCore
from PyQt5.QtWebEngineCore import QWebEngineUrlRequestInterceptor
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication
ClientId = ''
ClientSecret = ''
RedirectUrl = 'localhost/callback'
RedirectScheme = 'http://'
Scopes = ['user_read', 'channel_subscriptions', 'channel_check_subscription', 'user_subscriptions', 'channel_editor',
'chat_login']
ResponseType = 'code'
Headers = {'client_id': ClientId, 'redirect_uri': RedirectScheme+RedirectUrl, 'response_type': ResponseType,
'scope': str.join(' ', Scopes)}
AuthUrl = 'https://api.twitch.tv/kraken/oauth2/authorize?{headers}'.format(
headers=urlencode(Headers))
class RequestInterceptor(QWebEngineUrlRequestInterceptor):
def __init__(self, app):
super(RequestInterceptor, self).__init__()
self.app = app
def interceptRequest(self, info):
if RedirectUrl == (info.requestUrl().host()+info.requestUrl().path()):
params = parse_qs(info.requestUrl().query())
if 'code' in params.keys():
print('OAuth code is {code}'.format(code=params['code']))
self.app.quit()
class LoginWindow(QWebEngineView):
logged_in = QtCore.pyqtSignal(['QString'])
def __init__(self, app):
super(LoginWindow, self).__init__()
self.nam = self.page()
self.app = app
self.setUrl(QUrl(AuthUrl))
self.show()
self.loadFinished.connect(self._loadFinished)
interceptor = RequestInterceptor(self.app)
self.page().profile().setRequestInterceptor(interceptor)
sys.exit(app.exec_())
def _loadFinished(self, result):
self.page().toHtml(self.callable)
def callable(self, data):
self.html = data
if __name__ == "__main__":
app = QApplication(sys.argv)
browser = LoginWindow(app)