针对周杰伦特殊处理

This commit is contained in:
shaonianzhetnan 2023-02-26 18:40:33 +08:00
parent b42c17597d
commit 510c67ec2a
5 changed files with 81 additions and 40 deletions

View File

@ -41,9 +41,11 @@ https://github.com/Binaryify/NeteaseCloudMusicApi
播放喜马拉雅专辑 `cloudmusic://xmly/playlist?id=258244`
- cloudmusic://xmly/playlist?id=专辑ID
全网音乐搜索播放 `cloudmusic://search/name?kv=倒影 周杰伦`
(不建议使用)全网音乐搜索播放 `cloudmusic://search/name?kv=倒影 周杰伦`
- cloudmusic://search/name?kv=关键词
(推荐)音乐搜索播放 `cloudmusic://search/play?kv=倒影 周杰伦`
- cloudmusic://search/play?kv=关键词
configuration.yaml
```yaml
@ -63,11 +65,8 @@ media_player.yun_yin_le:
## 关联项目
- https://github.com/shaonianzhentan/cloud_music_mpd
- https://github.com/shaonianzhentan/ha_music_source
- https://github.com/shaonianzhentan/ha_windows
## 如果这个项目对你有帮助,请我喝杯<del style="font-size: 14px;">咖啡</del>奶茶吧😘
|支付宝|微信|
|---|---|

View File

@ -102,6 +102,7 @@ class CloudMusicRouter():
# 搜索名称
search_name = f'{search_protocol}name'
search_play = f'{search_protocol}play'
async def async_browse_media(media_player, media_content_type, media_content_id):
@ -705,6 +706,8 @@ async def async_play_media(media_player, cloud_music, media_content_id):
playlist = await cloud_music.async_fm_playlist(id, page, size)
elif media_content_id.startswith(CloudMusicRouter.search_name):
playlist = await cloud_music.async_search_song(keywords)
elif media_content_id.startswith(CloudMusicRouter.search_play):
playlist = await cloud_music.async_play_song(keywords)
if playlist is not None:
media_player.playindex = playindex

View File

@ -1,4 +1,4 @@
import uuid, time, logging, os, hashlib, aiohttp
import uuid, time, logging, os, hashlib, aiohttp, requests
from urllib.parse import quote
from homeassistant.helpers.network import get_url
from .http_api import http_get, http_cookie
@ -292,6 +292,36 @@ class CloudMusic():
return list(map(format_playlist, data['items']))
# 搜索音乐播放
async def async_play_song(self, name):
if '周杰伦' in name:
result = await self.async_music_source(name)
if result is not None:
return [ result ]
res = await self.netease_cloud_music(f'/cloudsearch?limit=1&keywords={name}')
if res['code'] == 200:
songs = res['result']['songs']
if len(songs) > 0:
item = songs[0]
al = item['al']
ar = item['ar'][0]
id = item['id']
song = item['name']
album = al.get('name')
singer = ar.get('name')
picUrl = self.netease_image_url(al.get('picUrl'))
duration = item.get('dt')
url = self.get_play_url(id, song, singer, MusicSource.PLAYLIST.value)
music_info = MusicInfo(id, song, singer, album, duration, url, picUrl, MusicSource.URL.value)
return [ music_info ]
# 音乐搜索
async def async_search_song(self, name):
ha_music_source = self.hass.data.get('ha_music_source')
@ -373,4 +403,30 @@ class CloudMusic():
"creator": item['name'],
"source": MusicSource.ARTISTS.value
}, res['result']['artists']))
return _list
return _list
async def async_music_source(self, keyword):
url = 'https://thewind.xyz/api/new/search'
files = {
"src": (None, "KW"),
"keyword": (None, keyword),
"num": (None, 10)
}
response = await self.hass.async_add_executor_job(requests.post, url, files)
result = response.json()
# print(result)
if len(result) > 0:
result = list(filter(lambda x: x.get('songId') is not None, result))
if len(result) > 0:
item = result[0]
albumName = item.get('albumName')
songSrc = item['songSrc']
songId = item['songId']
play_url = f'https://thewind.xyz/api/new/player?shareId={songSrc}_{songId}'
# print(play_url)
response = await self.hass.async_add_executor_job(requests.get, play_url)
result = response.json()
# print(result)
if isinstance(result, list) and len(result) > 0:
info = result[0]
return MusicInfo(songId, info.get('title'), info.get('author'), albumName, 0, info.get('url'), info.get('pic'), MusicSource.URL.value)

View File

@ -13,6 +13,9 @@ class HttpView(HomeAssistantView):
name = f"cloud_music:url"
requires_auth = False
play_key = None
play_url = None
async def get(self, request):
hass = request.app["hass"]
cloud_music = hass.data['cloud_music']
@ -30,6 +33,11 @@ class HttpView(HomeAssistantView):
if id is None or source is None:
return web.HTTPFound(play_url)
# 缓存KEY
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 \
@ -40,9 +48,9 @@ class HttpView(HomeAssistantView):
if url is not None:
# 收费音乐
if fee == 1:
result = await self.async_music_source(hass, song, singer)
result = await cloud_music.async_music_source(f'{song} - {singer}')
if result is not None:
url = result
url = result.url
play_url = url
else:
@ -51,37 +59,12 @@ class HttpView(HomeAssistantView):
if url is not None:
play_url = url
else:
result = await self.async_music_source(hass, song, singer)
result = await cloud_music.async_music_source(f'{song} - {singer}')
if result is not None:
play_url = result
play_url = result.url
print(play_url)
print(play_url)
self.play_key = play_key
self.play_url = play_url
# 重定向到可播放链接
return web.HTTPFound(play_url)
async def async_music_source(self, hass, song, singer):
# 使用全网音乐搜索
ha_music_source = hass.data.get('ha_music_source')
if ha_music_source is not None:
return await ha_music_source.async_song_url(song, singer)
else:
keyword = f'{song} - {singer}'
url = 'https://thewind.xyz/api/new/search'
files = {
"src": (None, "KW"),
"keyword": (None, keyword),
"num": (None, 10)
}
response = await hass.async_add_executor_job(requests.post, url, files=files)
result = response.json()
for item in result:
# print(item['songName'], item['singersName'], item['albumName'])
songSrc = item['songSrc']
songId = item['songId']
play_url = f'https://thewind.xyz/api/new/player?shareId={songSrc}_{songId}'
print(play_url)
response = await hass.async_add_executor_job(requests.get, url)
result = response.json()
if len(result) > 0:
return result[0].get('url')
return web.HTTPFound(play_url)

View File

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