Skip to content

Commit 4cea8a2

Browse files
committed
new way to get a token #10
1 parent 657ca97 commit 4cea8a2

File tree

3 files changed

+45
-28
lines changed

3 files changed

+45
-28
lines changed

InnoScript.iss

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#define ScriptVersion "0.3"
1+
#define ScriptVersion "0.3.1"
22
[Setup]
33
AppName=YandexMusicRPC
44
AppPublisher=FozerG

getToken.py

+42-25
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,75 @@
11
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget
22
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage
3-
from PyQt5.QtCore import QUrl
3+
from PyQt5.QtCore import QUrl, pyqtSignal
44
from PyQt5.QtGui import QIcon
55
import sys
66
import re
77

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=([^&]*)'
109

1110
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+
1813
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)
2018

2119

2220
class TokenWindow(QMainWindow):
23-
def __init__(self, url, icon_path):
21+
def __init__(self, initial_url, icon_path=None):
2422
super().__init__()
2523
self.setWindowTitle("Authorization")
2624
self.setGeometry(100, 100, 700, 800)
27-
self.setWindowIcon(QIcon(icon_path))
28-
25+
if icon_path:
26+
self.setWindowIcon(QIcon(icon_path))
27+
2928
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)
3232
self.browser.urlChanged.connect(self.on_url_changed)
33+
self.browser.page().profile().cookieStore().deleteAllCookies()
34+
self.browser.setUrl(QUrl(initial_url))
3335

34-
self.browser.setUrl(QUrl(url))
35-
3636
central_widget = QWidget()
3737
layout = QVBoxLayout()
3838
layout.addWidget(self.browser)
3939
central_widget.setLayout(layout)
4040
self.setCentralWidget(central_widget)
41-
41+
4242
self.token = None
4343

44+
def on_token_found(self, token):
45+
self.token = token
46+
QApplication.quit()
47+
4448
def on_url_changed(self, url):
4549
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()
5055

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)
5168

52-
def update_token(icon_path):
69+
def get_yandex_music_token(icon_path=None):
5370
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)
5673
token_window.show()
5774
app.exec_()
5875
return token_window.token

main.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
CLIENT_ID_RU_DECLINED = "1269826362399522849" # Яндекс Музыку (склонение для активности "Слушает")
4141

4242
# Версия (tag) скрипта для проверки на актуальность через Github Releases
43-
CURRENT_VERSION = "v0.3"
43+
CURRENT_VERSION = "v0.3.1"
4444

4545
# Ссылка на репозиторий
4646
REPO_URL = "https://github.com/FozerG/YandexMusicRPC"
@@ -1030,7 +1030,7 @@ def Remove_yaToken_From_Memory():
10301030

10311031

10321032
def update_token_task(icon_path, queue):
1033-
result = getToken.update_token(icon_path)
1033+
result = getToken.get_yandex_music_token(icon_path)
10341034
queue.put(result)
10351035

10361036

0 commit comments

Comments
 (0)