From 85e133dcbe0ccf048937f01fc457c17918d95ae4 Mon Sep 17 00:00:00 2001 From: shaonianzhentan Date: Sat, 25 Mar 2023 13:51:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=8C=E7=BB=B4=E7=A0=815=E5=88=86=E9=92=9F?= =?UTF-8?q?=E8=BF=87=E6=9C=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ha_cloud_music/browse_media.py | 36 +++++++++++-------- .../ha_cloud_music/cloud_music.py | 6 ++++ .../ha_cloud_music/manifest.json | 2 +- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/custom_components/ha_cloud_music/browse_media.py b/custom_components/ha_cloud_music/browse_media.py index e34586d..e2a3489 100644 --- a/custom_components/ha_cloud_music/browse_media.py +++ b/custom_components/ha_cloud_music/browse_media.py @@ -1,6 +1,6 @@ """Support for media browsing.""" from enum import Enum -import logging, os, random +import logging, os, random, time from urllib.parse import urlparse, parse_qs, parse_qsl, quote from homeassistant.util.json import save_json from custom_components.ha_cloud_music.http_api import http_get @@ -186,20 +186,26 @@ async def async_browse_media(media_player, media_content_type, media_content_id) } ]) else: - res = await cloud_music.netease_cloud_music('/login/qr/key') - if res['code'] == 200: - codekey = res['data']['unikey'] - res = await cloud_music.netease_cloud_music(f'/login/qr/create?key={codekey}') - qrurl = res['data']['qrurl'] - print(qrurl) - children.extend([ - { - 'title': 'APP扫码授权后,点击这里登录', - 'path': CloudMusicRouter.my_login + '?id=' + codekey, - 'type': MEDIA_TYPE_MUSIC, - 'thumbnail': f'https://cdn.dotmaui.com/qrc/?t={qrurl}' - } - ]) + qr = cloud_music.login_qrcode + now = int(time.time()) + # 超过5分钟重新获取验证码 + if qr['time'] is None or now - qr['time'] > 300: + res = await cloud_music.netease_cloud_music('/login/qr/key') + if res['code'] == 200: + codekey = res['data']['unikey'] + res = await cloud_music.netease_cloud_music(f'/login/qr/create?key={codekey}') + qr['key'] = codekey + qr['url'] = res['data']['qrurl'] + qr['time'] = now + + children.extend([ + { + 'title': 'APP扫码授权后,点击这里登录', + 'path': CloudMusicRouter.my_login + '?id=' + qr['key'], + 'type': MEDIA_TYPE_MUSIC, + 'thumbnail': f'https://cdn.dotmaui.com/qrc/?t={qr["url"]}' + } + ]) # 扩展资源 children.extend([ diff --git a/custom_components/ha_cloud_music/cloud_music.py b/custom_components/ha_cloud_music/cloud_music.py index 2c1c0a1..b87b1b6 100644 --- a/custom_components/ha_cloud_music/cloud_music.py +++ b/custom_components/ha_cloud_music/cloud_music.py @@ -35,6 +35,12 @@ class CloudMusic(): self.userinfo_filepath = self.get_storage_dir('cloud_music.userinfo') if os.path.exists(self.userinfo_filepath): self.userinfo = load_json(self.userinfo_filepath) + # 登录二维码 + self.login_qrcode = { + 'key': None, + 'time': None, + 'url': None + } def get_storage_dir(self, file_name): return os.path.abspath(f'{STORAGE_DIR}/{file_name}') diff --git a/custom_components/ha_cloud_music/manifest.json b/custom_components/ha_cloud_music/manifest.json index 5d56af2..f872b1c 100644 --- a/custom_components/ha_cloud_music/manifest.json +++ b/custom_components/ha_cloud_music/manifest.json @@ -1,7 +1,7 @@ { "domain": "ha_cloud_music", "name": "\u4E91\u97F3\u4E50", - "version": "2023.3.24", + "version": "2023.3.25", "config_flow": true, "documentation": "https://github.com/shaonianzhentan/ha_cloud_music", "requirements": [],