chore: move code from src to root, made many dependencies optional

This commit is contained in:
Puqns67 2024-10-16 01:07:02 +08:00
parent 228c52e7ce
commit 264bca2983
Signed by: Puqns67
GPG Key ID: 9669DF042554F536
13 changed files with 100 additions and 41 deletions

4
ncmlyrics/__init__.py Normal file
View File

@ -0,0 +1,4 @@
from .__version__ import __title__, __description__, __version__
from .__main__ import main
__all__ = ["__title__", "__description__", "__version__", "main"]

3
ncmlyrics/__version__.py Normal file
View File

@ -0,0 +1,3 @@
__title__ = "ncmlyrics"
__description__ = "No description at now."
__version__ = "0.1.0a2"

View File

@ -16,14 +16,38 @@ __all__ = ["NCMApi"]
REQUEST_HEADERS = { REQUEST_HEADERS = {
"Accept": "application/json", "Accept": "application/json",
"Accept-Encoding": "zstd, br, gzip, deflate", "Accept-Encoding": "gzip, deflate",
"Connection": "keep-alive", "Connection": "keep-alive",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36",
} }
try:
import brotlicffi as brotli # type: ignore
except ImportError:
try:
import brotli # type: ignore # noqa: F401
except ImportError:
pass
else:
REQUEST_HEADERS["Accept-Encoding"] = "br, " + REQUEST_HEADERS["Accept-Encoding"]
else:
REQUEST_HEADERS["Accept-Encoding"] = "br, " + REQUEST_HEADERS["Accept-Encoding"]
try:
import zstandard # type: ignore # noqa: F401
except ImportError:
pass
else:
REQUEST_HEADERS["Accept-Encoding"] = "zstd, " + REQUEST_HEADERS["Accept-Encoding"]
try:
import h2 # type: ignore
except ImportError:
h2 = None
class NCMApi: class NCMApi:
def __init__(self, http2: bool = True) -> None: def __init__(self) -> None:
self._cookiePath = PLATFORM.user_config_path / "cookies.txt" self._cookiePath = PLATFORM.user_config_path / "cookies.txt"
self._cookieJar = MozillaCookieJar() self._cookieJar = MozillaCookieJar()
@ -36,7 +60,7 @@ class NCMApi:
base_url=NCM_API_BASE_URL, base_url=NCM_API_BASE_URL,
cookies=self._cookieJar, cookies=self._cookieJar,
headers=REQUEST_HEADERS, headers=REQUEST_HEADERS,
http2=http2, http2=h2 is not None,
) )
def _fetch(self, request: HttpXRequest, retry: int | None = 4) -> HttpXResponse: def _fetch(self, request: HttpXRequest, retry: int | None = 4) -> HttpXResponse:

View File

@ -1,8 +1,7 @@
from httpx import URL from httpx import URL
from platformdirs import PlatformDirs from platformdirs import PlatformDirs
APP_NAME = "ncmlyrics" from .__version__ import __title__
APP_VERSION = "0.1.0a2"
NCM_API_BASE_URL = URL("https://interface.music.163.com/api") NCM_API_BASE_URL = URL("https://interface.music.163.com/api")
@ -11,4 +10,4 @@ CONFIG_LRC_AUTO_MERGE_OFFSET = 50
CONFIG_API_DETAIL_TRACK_PER_REQUEST = 150 CONFIG_API_DETAIL_TRACK_PER_REQUEST = 150
PLATFORM = PlatformDirs(appname=APP_NAME, ensure_exists=True) PLATFORM = PlatformDirs(appname=__title__, ensure_exists=True)

View File

@ -1,40 +1,45 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[project] [project]
name = "ncmlyrics" name = "ncmlyrics"
authors = [{ name = "Puqns67", email = "me@puqns67.icu" }] description = "No description at now."
dependencies = [
"httpx>=0.27.2",
"h2>=4.1.0",
"rich>=13.9.2",
"brotli>=1.1.0",
"zstandard>=0.23.0",
"click>=8.1.7",
"platformdirs>=4.3.6",
]
requires-python = ">=3.12" requires-python = ">=3.12"
readme = "README.md"
license = { text = "GPL-3.0-or-later" } license = { text = "GPL-3.0-or-later" }
dynamic = ["version"] authors = [{ name = "Puqns67", email = "me@puqns67.icu" }]
classifiers = [ classifiers = [
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.12",
"License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)",
"Operating System :: OS Independent",
"Development Status :: 1 - Planning", "Development Status :: 1 - Planning",
"Environment :: Console", "Environment :: Console",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Programming Language :: Python :: Implementation :: CPython",
"License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)",
"Operating System :: OS Independent",
"Natural Language :: Chinese (Simplified)", "Natural Language :: Chinese (Simplified)",
] ]
dependencies = ["httpx", "rich>=13", "click>=8", "platformdirs>=4"]
dynamic = ["readme", "version"]
[project.optional-dependencies]
brotli = [
"brotli; platform_python_implementation == 'CPython'",
"brotlicffi; platform_python_implementation != 'CPython'",
]
http2 = ["h2>=3,<5"]
zstd = ["zstandard>=0.18.0"]
[project.scripts]
ncmlyrics = "ncmlyrics:main"
[project.urls] [project.urls]
Homepage = "https://github.com/Puqns67/ncmlyrics" Homepage = "https://github.com/Puqns67/ncmlyrics"
Issues = "https://github.com/Puqns67/ncmlyrics/issues" Issues = "https://github.com/Puqns67/ncmlyrics/issues"
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[tool.hatch.version] [tool.hatch.version]
path = "src/ncmlyrics/constant.py" path = "ncmlyrics/__version__.py"
pattern = "^APP_VERSION = \"(?P<version>[^\"]+)\"$"
[tool.ruff] [tool.ruff]
target-version = "py312" target-version = "py312"

View File

@ -1,3 +0,0 @@
from .constant import APP_VERSION as __version__
__all__ = ["__version__"]

47
uv.lock generated
View File

@ -34,6 +34,23 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/3d/d5/942051b45a9e883b5b6e98c041698b1eb2012d25e5948c58d6bf85b1bb43/Brotli-1.1.0-cp312-cp312-win_amd64.whl", hash = "sha256:906bc3a79de8c4ae5b86d3d75a8b77e44404b0f4261714306e3ad248d8ab0951", size = 357255 }, { url = "https://files.pythonhosted.org/packages/3d/d5/942051b45a9e883b5b6e98c041698b1eb2012d25e5948c58d6bf85b1bb43/Brotli-1.1.0-cp312-cp312-win_amd64.whl", hash = "sha256:906bc3a79de8c4ae5b86d3d75a8b77e44404b0f4261714306e3ad248d8ab0951", size = 357255 },
] ]
[[package]]
name = "brotlicffi"
version = "1.1.0.0"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "cffi" },
]
sdist = { url = "https://files.pythonhosted.org/packages/95/9d/70caa61192f570fcf0352766331b735afa931b4c6bc9a348a0925cc13288/brotlicffi-1.1.0.0.tar.gz", hash = "sha256:b77827a689905143f87915310b93b273ab17888fd43ef350d4832c4a71083c13", size = 465192 }
wheels = [
{ url = "https://files.pythonhosted.org/packages/a2/11/7b96009d3dcc2c931e828ce1e157f03824a69fb728d06bfd7b2fc6f93718/brotlicffi-1.1.0.0-cp37-abi3-macosx_10_9_x86_64.whl", hash = "sha256:9b7ae6bd1a3f0df532b6d67ff674099a96d22bc0948955cb338488c31bfb8851", size = 453786 },
{ url = "https://files.pythonhosted.org/packages/d6/e6/a8f46f4a4ee7856fbd6ac0c6fb0dc65ed181ba46cd77875b8d9bbe494d9e/brotlicffi-1.1.0.0-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:19ffc919fa4fc6ace69286e0a23b3789b4219058313cf9b45625016bf7ff996b", size = 2911165 },
{ url = "https://files.pythonhosted.org/packages/be/20/201559dff14e83ba345a5ec03335607e47467b6633c210607e693aefac40/brotlicffi-1.1.0.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:9feb210d932ffe7798ee62e6145d3a757eb6233aa9a4e7db78dd3690d7755814", size = 2927895 },
{ url = "https://files.pythonhosted.org/packages/cd/15/695b1409264143be3c933f708a3f81d53c4a1e1ebbc06f46331decbf6563/brotlicffi-1.1.0.0-cp37-abi3-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:84763dbdef5dd5c24b75597a77e1b30c66604725707565188ba54bab4f114820", size = 2851834 },
{ url = "https://files.pythonhosted.org/packages/b4/40/b961a702463b6005baf952794c2e9e0099bde657d0d7e007f923883b907f/brotlicffi-1.1.0.0-cp37-abi3-win32.whl", hash = "sha256:1b12b50e07c3911e1efa3a8971543e7648100713d4e0971b13631cce22c587eb", size = 341731 },
{ url = "https://files.pythonhosted.org/packages/1c/fa/5408a03c041114ceab628ce21766a4ea882aa6f6f0a800e04ee3a30ec6b9/brotlicffi-1.1.0.0-cp37-abi3-win_amd64.whl", hash = "sha256:994a4f0681bb6c6c3b0925530a1926b7a189d878e6e5e38fae8efa47c5d9c613", size = 366783 },
]
[[package]] [[package]]
name = "certifi" name = "certifi"
version = "2024.8.30" version = "2024.8.30"
@ -198,27 +215,37 @@ wheels = [
[[package]] [[package]]
name = "ncmlyrics" name = "ncmlyrics"
version = "0.1.0a2" version = "0.1.0a3"
source = { editable = "." } source = { editable = "." }
dependencies = [ dependencies = [
{ name = "brotli" },
{ name = "click" }, { name = "click" },
{ name = "h2" },
{ name = "httpx" }, { name = "httpx" },
{ name = "platformdirs" }, { name = "platformdirs" },
{ name = "rich" }, { name = "rich" },
]
[package.optional-dependencies]
brotli = [
{ name = "brotli", marker = "platform_python_implementation == 'CPython'" },
{ name = "brotlicffi", marker = "platform_python_implementation != 'CPython'" },
]
http2 = [
{ name = "h2" },
]
zstd = [
{ name = "zstandard" }, { name = "zstandard" },
] ]
[package.metadata] [package.metadata]
requires-dist = [ requires-dist = [
{ name = "brotli", specifier = ">=1.1.0" }, { name = "brotli", marker = "platform_python_implementation == 'CPython' and extra == 'brotli'" },
{ name = "click", specifier = ">=8.1.7" }, { name = "brotlicffi", marker = "platform_python_implementation != 'CPython' and extra == 'brotli'" },
{ name = "h2", specifier = ">=4.1.0" }, { name = "click", specifier = ">=8" },
{ name = "httpx", specifier = ">=0.27.2" }, { name = "h2", marker = "extra == 'http2'", specifier = ">=3,<5" },
{ name = "platformdirs", specifier = ">=4.3.6" }, { name = "httpx" },
{ name = "rich", specifier = ">=13.9.2" }, { name = "platformdirs", specifier = ">=4" },
{ name = "zstandard", specifier = ">=0.23.0" }, { name = "rich", specifier = ">=13" },
{ name = "zstandard", marker = "extra == 'zstd'", specifier = ">=0.18.0" },
] ]
[[package]] [[package]]