1
1
from PyQt5 .QtWidgets import QApplication , QMainWindow , QVBoxLayout , QWidget
2
2
from PyQt5 .QtWebEngineWidgets import QWebEngineView , QWebEnginePage
3
- from PyQt5 .QtCore import QUrl
3
+ from PyQt5 .QtCore import QUrl , pyqtSignal
4
4
from PyQt5 .QtGui import QIcon
5
5
import sys
6
6
import re
7
7
8
- URL_WITH_ACCESS_TOKEN_REGEX = r'https:\/\/music\.yandex\.(?:ru|com|by|kz|ua)\/#access_token=([^&]*)'
9
-
8
+ ACCESS_TOKEN_REGEX = r'access_token=([^&]*)'
10
9
11
10
class CustomWebEnginePage (QWebEnginePage ):
12
- def __init__ (self , parent = None ):
13
- super ().__init__ (parent )
14
- self .profile ().setHttpUserAgent (
15
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36"
16
- )
17
-
11
+ token_found = pyqtSignal (str )
12
+
18
13
def javaScriptConsoleMessage (self , level , message , lineNumber , sourceId ):
19
- pass
14
+ match = re .search (ACCESS_TOKEN_REGEX , message )
15
+ if match :
16
+ token = match .group (1 )
17
+ self .token_found .emit (token )
20
18
21
19
22
20
class TokenWindow (QMainWindow ):
23
- def __init__ (self , url , icon_path ):
21
+ def __init__ (self , initial_url , icon_path = None ):
24
22
super ().__init__ ()
25
23
self .setWindowTitle ("Authorization" )
26
24
self .setGeometry (100 , 100 , 700 , 800 )
27
- self .setWindowIcon (QIcon (icon_path ))
28
-
25
+ if icon_path :
26
+ self .setWindowIcon (QIcon (icon_path ))
27
+
29
28
self .browser = QWebEngineView ()
30
- self .browser .setPage (CustomWebEnginePage (self .browser ))
31
- self .browser .page ().profile ().cookieStore ().deleteAllCookies ()
29
+ self .page = CustomWebEnginePage (self .browser )
30
+ self .browser .setPage (self .page )
31
+ self .page .token_found .connect (self .on_token_found )
32
32
self .browser .urlChanged .connect (self .on_url_changed )
33
+ self .browser .page ().profile ().cookieStore ().deleteAllCookies ()
34
+ self .browser .setUrl (QUrl (initial_url ))
33
35
34
- self .browser .setUrl (QUrl (url ))
35
-
36
36
central_widget = QWidget ()
37
37
layout = QVBoxLayout ()
38
38
layout .addWidget (self .browser )
39
39
central_widget .setLayout (layout )
40
40
self .setCentralWidget (central_widget )
41
-
41
+
42
42
self .token = None
43
43
44
+ def on_token_found (self , token ):
45
+ self .token = token
46
+ QApplication .quit ()
47
+
44
48
def on_url_changed (self , url ):
45
49
url_str = url .toString ()
46
- match = re .search (URL_WITH_ACCESS_TOKEN_REGEX , url_str )
47
- if match :
48
- self .token = match .group (1 )
49
- self .close ()
50
+ print (url_str )
51
+ if "music" in url_str :
52
+ self .browser .setUrl (QUrl ("https://oauth.yandex.ru" ))
53
+ elif "oauth.yandex" in url_str :
54
+ self .execute_fetch_script ()
50
55
56
+ def execute_fetch_script (self ):
57
+ script = """
58
+ fetch("https://oauth.yandex.ru/authorize?response_type=token&client_id=23cabbbdc6cd418abb4b39c32c41195d")
59
+ .then((response) => response.text())
60
+ .then((text) => {
61
+ const tokenMatch = text.match(/access_token=(.*?)&/);
62
+ if (tokenMatch) {
63
+ console.log("access_token=" + tokenMatch[1]);
64
+ }
65
+ });
66
+ """
67
+ self .browser .page ().runJavaScript (script )
51
68
52
- def update_token (icon_path ):
69
+ def get_yandex_music_token (icon_path = None ):
53
70
app = QApplication (sys .argv )
54
- oauth_url = "https://oauth.yandex.ru/authorize?response_type=token&client_id=23cabbbdc6cd418abb4b39c32c41195d" # Official link to OAuth Yandex.Music
55
- token_window = TokenWindow (oauth_url , icon_path )
71
+ initial_url = "https://oauth.yandex.ru/authorize?response_type=token&client_id=23cabbbdc6cd418abb4b39c32c41195d" # Offical oauth url on Yandex.Music
72
+ token_window = TokenWindow (initial_url , icon_path )
56
73
token_window .show ()
57
74
app .exec_ ()
58
75
return token_window .token
0 commit comments