Compare commits
4 Commits
fce38ab51d
...
74753122af
Author | SHA1 | Date |
---|---|---|
shaonianzhentan | 74753122af | |
shaonianzhentan | 20effe9503 | |
shaonianzhentan | d5542c381f | |
shaonianzhentan | e68cec504d |
|
@ -119,7 +119,6 @@ class CloudMusicRouter():
|
|||
|
||||
|
||||
async def async_browse_media(media_player, media_content_type, media_content_id):
|
||||
print(media_content_type, media_content_id)
|
||||
hass = media_player.hass
|
||||
cloud_music = hass.data['cloud_music']
|
||||
|
||||
|
@ -730,7 +729,6 @@ async def async_browse_media(media_player, media_content_type, media_content_id)
|
|||
|
||||
''' ================== 播放音乐 ================== '''
|
||||
async def async_play_media(media_player, cloud_music, media_content_id):
|
||||
print(media_content_id)
|
||||
hass = media_player.hass
|
||||
# 媒体库
|
||||
if media_source.is_media_source_id(media_content_id):
|
||||
|
|
|
@ -73,8 +73,6 @@ class CloudMusic():
|
|||
}
|
||||
save_json(self.userinfo_filepath, self.userinfo)
|
||||
return res_data
|
||||
else:
|
||||
print(res_data)
|
||||
|
||||
# 二维码登录
|
||||
async def qrcode_login(self, cookie_str):
|
||||
|
@ -127,9 +125,7 @@ class CloudMusic():
|
|||
async def netease_cloud_music(self, url):
|
||||
res = await http_get(self.api_url + url, self.userinfo.get('cookie', {}))
|
||||
code = res.get('code')
|
||||
print(code, url)
|
||||
if code != 200 and code != 801:
|
||||
print(res)
|
||||
msg = res.get('msg')
|
||||
if msg is not None:
|
||||
self.notification(msg)
|
||||
|
|
|
@ -35,8 +35,7 @@ class SimpleConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||
if res['data']['code'] == 200:
|
||||
user_input[CONF_URL] = url
|
||||
return self.async_create_entry(title=DOMAIN, data=user_input)
|
||||
except Exception as ex:
|
||||
print(ex)
|
||||
except Exception as ex:
|
||||
errors = { 'base': 'api_failed' }
|
||||
else:
|
||||
user_input = {}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import os, json, requests
|
||||
import base64
|
||||
import requests
|
||||
from urllib.parse import parse_qsl, quote
|
||||
from homeassistant.components.http import HomeAssistantView
|
||||
from aiohttp import web
|
||||
|
@ -17,57 +18,60 @@ 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
|
||||
print(query)
|
||||
query = {}
|
||||
data = request.query.get('data')
|
||||
if data is not None:
|
||||
decoded_data = base64.b64decode(data).decode('utf-8')
|
||||
qsl = parse_qsl(decoded_data)
|
||||
for q in qsl:
|
||||
query[q[0]] = q[1]
|
||||
|
||||
id = query.get('id')
|
||||
source = query.get('source')
|
||||
song = query.get('song')
|
||||
singer = query.get('singer')
|
||||
|
||||
not_found_tips = quote(f'当前没有找到编号是{id},歌名为{song},作者是{singer}的播放链接')
|
||||
play_url = f'https://fanyi.baidu.com/gettts?lan=zh&text={not_found_tips}&spd=5&source=web'
|
||||
|
||||
if id is None or source is None:
|
||||
return web.HTTPFound(play_url)
|
||||
play_url = f'http://fanyi.baidu.com/gettts?lan=zh&text={not_found_tips}&spd=5&source=web'
|
||||
|
||||
# 缓存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 \
|
||||
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
|
||||
self.play_url = play_url
|
||||
# 重定向到可播放链接
|
||||
return web.HTTPFound(play_url)
|
||||
|
||||
|
@ -80,7 +84,6 @@ class HttpView(HomeAssistantView):
|
|||
'source': 'netease'
|
||||
})
|
||||
data = res.json()
|
||||
# print(data)
|
||||
return data.get('url')
|
||||
except Exception as ex:
|
||||
print(ex)
|
||||
pass
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"domain": "ha_cloud_music",
|
||||
"name": "\u4E91\u97F3\u4E50",
|
||||
"version": "2023.11.19",
|
||||
"version": "2024.1.17",
|
||||
"config_flow": true,
|
||||
"documentation": "https://github.com/shaonianzhentan/ha_cloud_music",
|
||||
"requirements": [
|
||||
|
|
|
@ -4,9 +4,8 @@ from homeassistant.core import HomeAssistant
|
|||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.helpers.event import async_track_time_interval
|
||||
from homeassistant.components.media_player import MediaPlayerEntity, MediaPlayerDeviceClass
|
||||
from homeassistant.components.media_player import MediaPlayerEntity, MediaPlayerDeviceClass, MediaPlayerEntityFeature
|
||||
from homeassistant.components.media_player.const import (
|
||||
SUPPORT_BROWSE_MEDIA,
|
||||
SUPPORT_TURN_OFF,
|
||||
SUPPORT_TURN_ON,
|
||||
SUPPORT_VOLUME_STEP,
|
||||
|
@ -51,7 +50,7 @@ _LOGGER = logging.getLogger(__name__)
|
|||
|
||||
SUPPORT_FEATURES = SUPPORT_VOLUME_STEP | SUPPORT_VOLUME_MUTE | SUPPORT_VOLUME_SET | \
|
||||
SUPPORT_PLAY_MEDIA | SUPPORT_PLAY | SUPPORT_PAUSE | SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK | \
|
||||
SUPPORT_BROWSE_MEDIA | SUPPORT_SEEK | SUPPORT_CLEAR_PLAYLIST | SUPPORT_SHUFFLE_SET | SUPPORT_REPEAT_SET
|
||||
MediaPlayerEntityFeature.BROWSE_MEDIA | SUPPORT_SEEK | SUPPORT_CLEAR_PLAYLIST | SUPPORT_SHUFFLE_SET | SUPPORT_REPEAT_SET
|
||||
|
||||
# 定时器时间
|
||||
TIME_BETWEEN_UPDATES = datetime.timedelta(seconds=2)
|
||||
|
|
|
@ -1,45 +1,45 @@
|
|||
from bs4 import BeautifulSoup
|
||||
import requests, re
|
||||
import requests
|
||||
import re
|
||||
from .models.music_info import MusicInfo, MusicSource
|
||||
|
||||
|
||||
def get_last_part(path):
|
||||
last_slash_index = path.rfind('/')
|
||||
last_slash_index = path.rfind('/')
|
||||
if last_slash_index != -1:
|
||||
return path[last_slash_index + 1:]
|
||||
return path
|
||||
|
||||
|
||||
def get_music(keyword):
|
||||
# https://www.gequbao.com
|
||||
api = 'https://www.fangpi.net'
|
||||
session = requests.Session()
|
||||
try:
|
||||
response = session.get(f'{api}/s/{keyword}')
|
||||
soup = BeautifulSoup(response.text.encode(response.encoding), 'lxml')
|
||||
items = soup.select('.card-text .row')
|
||||
if len(items) > 1:
|
||||
row = items[1]
|
||||
# print(td)
|
||||
song = row.select('.col-5 a')[0].get_text().strip()
|
||||
singer = row.select('.col-4')[0].get_text().strip()
|
||||
# https://www.gequbao.com
|
||||
api = 'https://www.fangpi.net'
|
||||
session = requests.Session()
|
||||
try:
|
||||
response = session.get(f'{api}/s/{keyword}')
|
||||
soup = BeautifulSoup(response.text.encode(response.encoding), 'lxml')
|
||||
items = soup.select('.card-text .row')
|
||||
if len(items) > 1:
|
||||
row = items[1]
|
||||
song = row.select('.col-5 a')[0].get_text().strip()
|
||||
singer = row.select('.col-4')[0].get_text().strip()
|
||||
|
||||
a = row.select('.col-3 a')
|
||||
href = a[0].attrs['href']
|
||||
|
||||
# print(href)
|
||||
response = session.get(f'{api}{href}')
|
||||
html = response.text
|
||||
a = row.select('.col-3 a')
|
||||
href = a[0].attrs['href']
|
||||
|
||||
soup = BeautifulSoup(html, 'lxml')
|
||||
cover = soup.select('head meta[property="og:image"]')
|
||||
#print(cover)
|
||||
pic = 'https://p2.music.126.net/tGHU62DTszbFQ37W9qPHcg==/2002210674180197.jpg'
|
||||
# 封面
|
||||
if len(cover) > 0:
|
||||
pic = cover[0].attrs['content']
|
||||
response = session.get(f'{api}{href}')
|
||||
html = response.text
|
||||
|
||||
songId = get_last_part(href)
|
||||
album = ''
|
||||
audio_url = f'https://www.fangpi.net/api/play_url?id={songId}'
|
||||
return MusicInfo(songId, song, singer, album, 0, audio_url, pic, MusicSource.URL.value)
|
||||
except Exception as ex:
|
||||
print(ex)
|
||||
soup = BeautifulSoup(html, 'lxml')
|
||||
cover = soup.select('head meta[property="og:image"]')
|
||||
pic = 'https://p2.music.126.net/tGHU62DTszbFQ37W9qPHcg==/2002210674180197.jpg'
|
||||
# 封面
|
||||
if len(cover) > 0:
|
||||
pic = cover[0].attrs['content']
|
||||
|
||||
songId = get_last_part(href)
|
||||
album = ''
|
||||
audio_url = f'https://www.fangpi.net/api/play_url?id={songId}'
|
||||
return MusicInfo(songId, song, singer, album, 0, audio_url, pic, MusicSource.URL.value)
|
||||
except Exception as ex:
|
||||
pass
|
||||
|
|
Loading…
Reference in New Issue