feat(auth): pywebview вместо webengine
- добавлен auth_webview.py и режим --auth - build.py обновлён, WebEngine исключён - pywebview добавлен в requirements
This commit is contained in:
78
auth_webview.py
Normal file
78
auth_webview.py
Normal file
@@ -0,0 +1,78 @@
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import json
|
||||
import threading
|
||||
from urllib.parse import urlparse, parse_qs, unquote
|
||||
|
||||
import webview
|
||||
|
||||
|
||||
def extract_token(url_string):
|
||||
token = None
|
||||
expires_in = 3600
|
||||
parsed = urlparse(url_string)
|
||||
|
||||
if parsed.fragment:
|
||||
params = parse_qs(parsed.fragment)
|
||||
else:
|
||||
params = parse_qs(parsed.query)
|
||||
|
||||
if 'access_token' in params:
|
||||
token = params['access_token'][0]
|
||||
if 'expires_in' in params:
|
||||
try:
|
||||
expires_in = int(params['expires_in'][0])
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
if not token:
|
||||
start_marker = "access_token%253D"
|
||||
end_marker = "%25"
|
||||
|
||||
start_index = url_string.find(start_marker)
|
||||
if start_index != -1:
|
||||
token_start_index = start_index + len(start_marker)
|
||||
remaining_url = url_string[token_start_index:]
|
||||
end_index = remaining_url.find(end_marker)
|
||||
|
||||
if end_index != -1:
|
||||
raw_token = remaining_url[:end_index]
|
||||
else:
|
||||
amp_index = remaining_url.find('&')
|
||||
if amp_index != -1:
|
||||
raw_token = remaining_url[:amp_index]
|
||||
else:
|
||||
raw_token = remaining_url
|
||||
token = unquote(raw_token)
|
||||
|
||||
return token, expires_in
|
||||
|
||||
|
||||
def main_auth(auth_url, output_path):
|
||||
def poll_url():
|
||||
try:
|
||||
url = window.get_current_url()
|
||||
except Exception:
|
||||
url = None
|
||||
if url:
|
||||
token, expires_in = extract_token(url)
|
||||
if token:
|
||||
data = {"token": token, "expires_in": expires_in}
|
||||
with open(output_path, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f)
|
||||
window.destroy()
|
||||
return
|
||||
threading.Timer(0.5, poll_url).start()
|
||||
|
||||
def on_loaded():
|
||||
threading.Timer(0.5, poll_url).start()
|
||||
|
||||
window = webview.create_window("VK Авторизация", auth_url)
|
||||
window.events.loaded += on_loaded
|
||||
storage_path = os.path.join(os.path.dirname(output_path), "webview_profile")
|
||||
webview.start(private_mode=False, storage_path=storage_path)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user