File size: 8,276 Bytes
0a1b571
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import base64
import json
import secrets
import string
from datetime import timedelta
from enum import IntEnum
from ipaddress import IPv4Address
from random import randint
from typing import Annotated, Any, Optional

from Cryptodome.Cipher import AES
from Cryptodome.Util.Padding import pad
from fastapi import Query

from hibiapi.api.netease.constants import NeteaseConstants
from hibiapi.utils.cache import cache_config
from hibiapi.utils.decorators import enum_auto_doc
from hibiapi.utils.exceptions import UpstreamAPIException
from hibiapi.utils.net import catch_network_error
from hibiapi.utils.routing import BaseEndpoint, dont_route


@enum_auto_doc
class SearchType(IntEnum):
    """搜索内容类型"""

    SONG = 1
    """单曲"""
    ALBUM = 10
    """专辑"""
    ARTIST = 100
    """歌手"""
    PLAYLIST = 1000
    """歌单"""
    USER = 1002
    """用户"""
    MV = 1004
    """MV"""
    LYRICS = 1006
    """歌词"""
    DJ = 1009
    """主播电台"""
    VIDEO = 1014
    """视频"""


@enum_auto_doc
class BitRateType(IntEnum):
    """歌曲码率"""

    LOW = 64000
    MEDIUM = 128000
    STANDARD = 198000
    HIGH = 320000


@enum_auto_doc
class MVResolutionType(IntEnum):
    """MV分辨率"""

    QVGA = 240
    VGA = 480
    HD = 720
    FHD = 1080


@enum_auto_doc
class RecordPeriodType(IntEnum):
    """听歌记录时段类型"""

    WEEKLY = 1
    """本周"""
    ALL = 0
    """所有时段"""


class _EncryptUtil:
    alphabets = bytearray(ord(char) for char in string.ascii_letters + string.digits)

    @staticmethod
    def _aes(data: bytes, key: bytes) -> bytes:
        data = pad(data, 16) if len(data) % 16 else data
        return base64.encodebytes(
            AES.new(
                key=key,
                mode=AES.MODE_CBC,
                iv=NeteaseConstants.AES_IV,
            ).encrypt(data)
        )

    @staticmethod
    def _rsa(data: bytes):
        result = pow(
            base=int(data.hex(), 16),
            exp=NeteaseConstants.RSA_PUBKEY,
            mod=NeteaseConstants.RSA_MODULUS,
        )
        return f"{result:0>256x}"

    @classmethod
    def encrypt(cls, data: dict[str, Any]) -> dict[str, str]:
        secret = bytes(secrets.choice(cls.alphabets) for _ in range(16))
        secure_key = cls._rsa(bytes(reversed(secret)))
        return {
            "params": cls._aes(
                data=cls._aes(
                    data=json.dumps(data).encode(),
                    key=NeteaseConstants.AES_KEY,
                ),
                key=secret,
            ).decode("ascii"),
            "encSecKey": secure_key,
        }


class NeteaseEndpoint(BaseEndpoint):
    def _construct_headers(self):
        headers = self.client.headers.copy()
        headers["X-Real-IP"] = str(
            IPv4Address(
                randint(
                    int(NeteaseConstants.SOURCE_IP_SEGMENT.network_address),
                    int(NeteaseConstants.SOURCE_IP_SEGMENT.broadcast_address),
                )
            )
        )
        return headers

    @dont_route
    @catch_network_error
    async def request(
        self, endpoint: str, *, params: Optional[dict[str, Any]] = None
    ) -> dict[str, Any]:
        params = {
            **(params or {}),
            "csrf_token": self.client.cookies.get("__csrf", ""),
        }
        response = await self.client.post(
            self._join(
                NeteaseConstants.HOST,
                endpoint=endpoint,
                params=params,
            ),
            headers=self._construct_headers(),
            data=_EncryptUtil.encrypt(params),
        )
        response.raise_for_status()
        if not response.text.strip():
            raise UpstreamAPIException(
                f"Upstream API {endpoint=} returns blank content"
            )
        return response.json()

    async def search(
        self,
        *,
        s: str,
        search_type: SearchType = SearchType.SONG,
        limit: int = 20,
        offset: int = 0,
    ):
        return await self.request(
            "api/cloudsearch/pc",
            params={
                "s": s,
                "type": search_type,
                "limit": limit,
                "offset": offset,
                "total": True,
            },
        )

    async def artist(self, *, id: int):
        return await self.request(
            "weapi/v1/artist/{artist_id}",
            params={
                "artist_id": id,
            },
        )

    async def album(self, *, id: int):
        return await self.request(
            "weapi/v1/album/{album_id}",
            params={
                "album_id": id,
            },
        )

    async def detail(
        self,
        *,
        id: Annotated[list[int], Query()],
    ):
        return await self.request(
            "api/v3/song/detail",
            params={
                "c": json.dumps(
                    [{"id": str(i)} for i in id],
                ),
            },
        )

    @cache_config(ttl=timedelta(minutes=20))
    async def song(
        self,
        *,
        id: Annotated[list[int], Query()],
        br: BitRateType = BitRateType.STANDARD,
    ):
        return await self.request(
            "weapi/song/enhance/player/url",
            params={
                "ids": [str(i) for i in id],
                "br": br,
            },
        )

    async def playlist(self, *, id: int):
        return await self.request(
            "weapi/v6/playlist/detail",
            params={
                "id": id,
                "total": True,
                "offset": 0,
                "limit": 1000,
                "n": 1000,
            },
        )

    async def lyric(self, *, id: int):
        return await self.request(
            "weapi/song/lyric",
            params={
                "id": id,
                "os": "pc",
                "lv": -1,
                "kv": -1,
                "tv": -1,
            },
        )

    async def mv(self, *, id: int):
        return await self.request(
            "api/v1/mv/detail",
            params={
                "id": id,
            },
        )

    async def mv_url(
        self,
        *,
        id: int,
        res: MVResolutionType = MVResolutionType.FHD,
    ):
        return await self.request(
            "weapi/song/enhance/play/mv/url",
            params={
                "id": id,
                "r": res,
            },
        )

    async def comments(self, *, id: int, offset: int = 0, limit: int = 1):
        return await self.request(
            "weapi/v1/resource/comments/R_SO_4_{song_id}",
            params={
                "song_id": id,
                "offset": offset,
                "total": True,
                "limit": limit,
            },
        )

    async def record(self, *, id: int, period: RecordPeriodType = RecordPeriodType.ALL):
        return await self.request(
            "weapi/v1/play/record",
            params={
                "uid": id,
                "type": period,
            },
        )

    async def djradio(self, *, id: int):
        return await self.request(
            "api/djradio/v2/get",
            params={
                "id": id,
            },
        )

    async def dj(self, *, id: int, offset: int = 0, limit: int = 20, asc: bool = False):
        # NOTE: Possible not same with origin
        return await self.request(
            "weapi/dj/program/byradio",
            params={
                "radioId": id,
                "offset": offset,
                "limit": limit,
                "asc": asc,
            },
        )

    async def detail_dj(self, *, id: int):
        return await self.request(
            "api/dj/program/detail",
            params={
                "id": id,
            },
        )

    async def user(self, *, id: int):
        return await self.request(
            "weapi/v1/user/detail/{id}",
            params={"id": id},
        )

    async def user_playlist(self, *, id: int, limit: int = 50, offset: int = 0):
        return await self.request(
            "weapi/user/playlist",
            params={
                "uid": id,
                "limit": limit,
                "offset": offset,
            },
        )