修复重定向问题

This commit is contained in:
shaonianzhentan 2023-12-19 19:42:42 +08:00
parent fce38ab51d
commit 68c5c61447
2 changed files with 36 additions and 28 deletions

View File

@ -1,4 +1,6 @@
import os, json, requests
import os
import json
import requests
from urllib.parse import parse_qsl, quote
from homeassistant.components.http import HomeAssistantView
from aiohttp import web
@ -7,6 +9,7 @@ from .manifest import manifest
DOMAIN = manifest.domain
class HttpView(HomeAssistantView):
url = "/cloud_music/url"
@ -17,7 +20,7 @@ class HttpView(HomeAssistantView):
play_url = None
async def get(self, request):
hass = request.app["hass"]
hass = request.app["hass"]
cloud_music = hass.data['cloud_music']
query = request.query
@ -37,39 +40,44 @@ class HttpView(HomeAssistantView):
play_key = f'{id}{song}{singer}{source}'
if self.play_key == play_key:
return web.HTTPFound(self.play_url)
source = int(source)
if source == MusicSource.PLAYLIST.value \
or source == MusicSource.ARTISTS.value \
or source == MusicSource.DJRADIO.value \
or source == MusicSource.CLOUD.value:
# 获取播放链接
url, fee = await cloud_music.song_url(id)
if url is not None:
# 收费音乐
if fee == 1:
url = await hass.async_add_executor_job(self.getVipMusic, id)
if url is None or url == '':
result = await cloud_music.async_music_source(song, singer)
if result is not None:
url = result.url
play_url = url
else:
# 从云盘里获取
url = await cloud_music.cloud_song_url(id)
if url is not None:
play_url = url
else:
or source == MusicSource.ARTISTS.value \
or source == MusicSource.DJRADIO.value \
or source == MusicSource.CLOUD.value:
# 获取播放链接
url, fee = await cloud_music.song_url(id)
if url is not None:
# 收费音乐
if fee == 1:
url = await hass.async_add_executor_job(self.getVipMusic, id)
if url is None or url == '':
result = await cloud_music.async_music_source(song, singer)
if result is not None:
play_url = result.url
url = result.url
play_url = url
else:
# 从云盘里获取
url = await cloud_music.cloud_song_url(id)
if url is not None:
play_url = url
else:
result = await cloud_music.async_music_source(song, singer)
if result is not None:
play_url = result.url
print(play_url)
self.play_key = play_key
self.play_url = play_url
# 重定向到可播放链接
return web.HTTPFound(play_url)
response = web.HTTPFound(play_url, headers={
'Content-Length': 0,
'Cache-Control': 'no-cache, no-store, keep-alive=timeout=4'
})
response.remove_header('Content-Type')
return response
# VIP音乐资源
def getVipMusic(self, id):
@ -83,4 +91,4 @@ class HttpView(HomeAssistantView):
# print(data)
return data.get('url')
except Exception as ex:
print(ex)
print(ex)

View File

@ -1,7 +1,7 @@
{
"domain": "ha_cloud_music",
"name": "\u4E91\u97F3\u4E50",
"version": "2023.11.19",
"version": "2023.12.19",
"config_flow": true,
"documentation": "https://github.com/shaonianzhentan/ha_cloud_music",
"requirements": [