Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
chenmozhijin committed Feb 15, 2024
1 parent b3aa291 commit ed0d6bf
Show file tree
Hide file tree
Showing 22 changed files with 1,697 additions and 804 deletions.
375 changes: 18 additions & 357 deletions LDDC.py

Large diffs are not rendered by default.

224 changes: 203 additions & 21 deletions api.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
from __future__ import annotations

# SPDX-License-Identifier: GPL-3.0-or-later
# SPDX-FileCopyrightText: Copyright (c) 2024 沉默の金
import base64
import json
import logging
import random
import string
import time

import requests


def get_latest_version() -> tuple[bool, str]:
try:
latest_release = requests.get("https://api.github.com/repos/chenmozhijin/LDDC/releases/latest", timeout=5).json()
except Exception as e:
logging.exception("获取最新版本信息时错误")
return False, str(e)
else:
if "tag_name" in latest_release:
latest_version = latest_release["tag_name"]
return True, latest_version
return False, "获取最新版本信息失败"


class QMSearchType:
SONG = 0
ARTIST = 1
Expand All @@ -16,6 +30,14 @@ class QMSearchType:
LYRIC = 7


QMD_headers = {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN",
"User-Agent": "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)",
}


def get_qrc(songid: str) -> str | requests.Response:
params = {
'version': '15',
Expand All @@ -36,7 +58,48 @@ def get_qrc(songid: str) -> str | requests.Response:
return response


def qm_search(keyword: str, search_type: QMSearchType) -> list | str:
def qm_get_lyric(mid: str) -> tuple[str | None, str | None] | str:
url = 'https://c.y.qq.com/lyric/fcgi-bin/fcg_query_lyric_new.fcg'
headers = {
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
'Referer': 'https://y.qq.com/',
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_3_1 like Mac OS X; zh-CN) AppleWebKit/537.51.1 ('
'KHTML, like Gecko) Mobile/17D50 UCBrowser/12.8.2.1268 Mobile AliApp(TUnionSDK/0.1.20.3) ',
}
params = {
'_': int(time.time()),
'cv': '4747474',
'ct': '24',
'format': 'json',
'inCharset': 'utf-8',
'outCharset': 'utf-8',
'notice': '0',
'platform': 'yqq.json',
'needNewCode': '1',
'g_tk': '5381',
'songmid': mid,
}
try:
response = requests.get(url, params=params, headers=headers, timeout=10)
response.raise_for_status()
response_json = response.json()
if 'lyric' not in response_json or 'trans' not in response_json:
return f'获取歌词失败, code: {response_json["code"]}'
orig_base64 = response_json['lyric']
ts_base64 = response_json['trans']
orig = base64.b64decode(orig_base64).decode("utf-8") if orig_base64 else None
ts = base64.b64decode(ts_base64).decode("utf-8") if ts_base64 else None
except Exception as e:
logging.exception("请求歌词时错误")
return str(e)
else:
logging.debug(f"请求歌词成功, orig: {orig}, ts: {ts}")
return orig, ts


def qm_search(keyword: str, search_type: int) -> list | str:
"""
搜索
:param keyword:关键字
Expand All @@ -46,16 +109,10 @@ def qm_search(keyword: str, search_type: QMSearchType) -> list | str:
logging.debug(f"开始搜索:{keyword}, 类型为{search_type}")
if search_type not in (QMSearchType.SONG, QMSearchType.ARTIST, QMSearchType.ALBUM, QMSearchType.SONGLIST):
return f"搜索类型错误,类型为{search_type}"
headers = {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN",
"User-Agent": "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)",
}
data = json.dumps({
"comm": {
"g_tk": 997034911,
"uin": ''.join(random.sample(string.digits, 10)),
"uin": random.randint(1000000000, 9999999999), # noqa: S311
"format": "json",
"inCharset": "utf-8",
"outCharset": "utf-8",
Expand All @@ -77,7 +134,7 @@ def qm_search(keyword: str, search_type: QMSearchType) -> list | str:
},
}, ensure_ascii=False).encode("utf-8")
try:
response = requests.post('https://u.y.qq.com/cgi-bin/musicu.fcg', headers=headers, data=data, timeout=3)
response = requests.post('https://u.y.qq.com/cgi-bin/musicu.fcg', headers=QMD_headers, data=data, timeout=3)
response.raise_for_status()
infos = response.json()['req_0']['data']['body']
results = []
Expand All @@ -94,17 +151,20 @@ def qm_search(keyword: str, search_type: QMSearchType) -> list | str:
# 添加结果
results.append({
'id': song['id'],
'name': song['name'],
'mid': song['mid'],
'title': song['title'],
'subtitle': song['subtitle'],
'artist': artist,
'album': song['album']['name'],
'interval': song['interval'],
'source': 'qm',
})

case QMSearchType.ALBUM:
for album in infos['album']['list']:
results.append({
'id': album['albumID'],
'mid': album['albumMID'],
'name': album['albumName'],
'pic': album['albumPic'], # 专辑封面
'count': album['song_count'], # 歌曲数量
Expand All @@ -120,6 +180,8 @@ def qm_search(keyword: str, search_type: QMSearchType) -> list | str:
'name': songlist['dissname'],
'pic': songlist['imgurl'], # 歌单封面
'count': songlist['song_count'], # 歌曲数量
'time': songlist['createtime'],
'creator': songlist['creator']['name'],
'source': 'qm',
})

Expand Down Expand Up @@ -150,14 +212,134 @@ def qm_search(keyword: str, search_type: QMSearchType) -> list | str:
return results


def get_latest_version() -> tuple[bool, str]:
def qm_get_album_song_list(album_mid: str) -> list | str:
data = json.dumps({
"comm": {
"cv": 4747474,
"ct": 24,
"format": "json",
"inCharset": "utf-8",
"outCharset": "utf-8",
"notice": 0,
"platform": "yqq.json",
"needNewCode": 1,
"uin": random.randint(1000000000, 9999999999), # noqa: S311
"g_tk_new_20200303": 708550273,
"g_tk": 708550273,
},
"req_1": {
"module": "music.musichallAlbum.AlbumSongList",
"method": "GetAlbumSongList",
"param": {"albumMid": album_mid, "begin": 0, "num": -1, "order": 2},
},
}, ensure_ascii=False).encode("utf-8")
try:
latest_release = requests.get("https://api.github.com/repos/chenmozhijin/LDDC/releases/latest", timeout=5).json()
response = requests.post("https://u.y.qq.com/cgi-bin/musicu.fcg", headers=QMD_headers, data=data, timeout=10)
response.raise_for_status()
response_json = response.json()
album_song_list = response_json["req_1"]["data"]["songList"]
results = []
for song in album_song_list:
info = song['songInfo']
# 处理艺术家
artist = ""
for singer in info['singer']:
if artist != "":
artist += "/"
artist += singer['name']
results.append({
'id': info['id'],
'mid': info['mid'],
'title': info['title'],
'subtitle': info['subtitle'],
'artist': artist,
'album': info['album']['name'],
'interval': info['interval'],
'source': 'qm',
})
if response_json['req_1']['data']['totalNum'] != len(results):
logging.error("获取到的歌曲数量与实际数量不一致")
return "专辑歌曲获取不完整"

except requests.HTTPError as e:
logging.exception("请求专辑数据时错误")
return str(e)
except requests.RequestException as e:
logging.exception("请求专辑数据时错误")
return str(e)
except json.JSONDecodeError as e:
logging.exception("解析专辑数据时错误")
return str(e)
except Exception as e:
logging.exception("获取最新版本信息时错误")
return False, str(e)
logging.exception("未知错误")
return str(e)
else:
if "tag_name" in latest_release:
latest_version = latest_release["tag_name"]
return True, latest_version
return False, "获取最新版本信息失败"
logging.info("获取专辑信息成功")
logging.debug(f"获取结果:{json.dumps(results, ensure_ascii=False, indent=4)}")
return results


def qm_get_songlist_song_list(songlist_id: str) -> str | list:
params = {
'_webcgikey': 'uniform_get_Dissinfo',
'_': int(time.time() * 1000),
}
data = {
"comm": {
"g_tk": 5381,
"uin": "",
"format": "json",
"inCharset": "utf-8",
"outCharset": "utf-8",
"notice": 0,
"platform": "h5",
"needNewCode": 1,
},
"req_0": {
"module": "music.srfDissInfo.aiDissInfo",
"method": "uniform_get_Dissinfo",
"param": {
"disstid": int(songlist_id),
"enc_host_uin": "",
"tag": 1,
"userinfo": 1,
"song_begin": 0,
"song_num": -1,
},
},
}

url = 'https://u.y.qq.com/cgi-bin/musicu.fcg'

try:
response = requests.post(url, headers=QMD_headers, params=params, json=data, timeout=20)
response.raise_for_status()
response_json = response.json()
songlist = response_json['req_0']['data']['songlist']
results = []
for song in songlist:
# 处理艺术家
artist = ""
for singer in song['singer']:
if artist != "":
artist += "/"
artist += singer['name']
results.append({
'id': song['id'],
'mid': song['mid'],
'title': song['title'],
'subtitle': song['subtitle'],
'artist': artist,
'album': song['album']['name'],
'interval': song['interval'],
'source': 'qm',
})
if response_json['req_0']['data']['total_song_num'] != len(results):
return "获取歌曲列表失败, 返回的歌曲数量与实际数量不一致"
except requests.exceptions.RequestException as e:
print(f"获取歌单失败 {e}")
return str(e)
else:
logging.info(f"获取歌单成功, 数量: {len(results)}")
logging.debug(f"获取歌单成功,获取结果: {results}")
return results
12 changes: 5 additions & 7 deletions data.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
# SPDX-License-Identifier: GPL-3.0-or-later
# SPDX-FileCopyrightText: Copyright (c) 2024 沉默の金
from __future__ import annotations

import ast
import logging
import os
import sqlite3
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from PySide6.QtCore import QMutex
from PySide6.QtCore import QMutex


class Data:
Expand All @@ -26,9 +22,11 @@ def __init__(self, current_directory: str, mutex: QMutex) -> None:
else:
self.cfg = {
"log_level": "INFO",
"lyrics_file_name_format": "%<artist> - %<name> (%<id>)",
"lyrics_file_name_format": "%<artist> - %<title> (%<id>)",
"default_save_path": os.path.join(current_directory, "lyrics"),
"lyrics_order": ["罗马音", "原文", "译文"],
"skip_inst_lyrics": True,
"get_normal_lyrics": True,
}
self.init_db()
self.read_config()
Expand Down Expand Up @@ -69,7 +67,7 @@ def read_config(self) -> None:
if isinstance(self.cfg[setting[1]], str):
self.cfg[setting[1]] = setting[2]
elif isinstance(self.cfg[setting[1]], bool):
if setting[2] == 'True':
if setting[2] == '1':
self.cfg[setting[1]] = True
else:
self.cfg[setting[1]] = False
Expand Down
2 changes: 0 additions & 2 deletions decryptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

# SPDX-License-Identifier: GPL-3.0-or-later
# SPDX-FileCopyrightText: Copyright (c) 2024 沉默の金
from __future__ import annotations

import logging
from zlib import decompress

Expand Down
Loading

0 comments on commit ed0d6bf

Please sign in to comment.