File size: 5,420 Bytes
0aee47a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

bilibili_api.audio



音频相关

"""

from enum import Enum
from typing import Union, Optional

from .utils.utils import get_api
from .utils.credential import Credential
from .utils.network import Api

API = get_api("audio")


class Audio:
    """

    音频



    Attributes:

        credential (Credential): 凭据类

    """

    def __init__(self, auid: int, credential: Union[Credential, None] = None):
        """

        Args:

            auid       (int)                        : 音频 AU 号



            credential (Credential | None, optional): 凭据. Defaults to None

        """
        self.credential = credential if credential is not None else Credential()
        self.__auid = auid

    def get_auid(self) -> int:
        return self.__auid

    async def get_info(self) -> dict:
        """

        获取音频信息



        Returns:

            dict: 调用 API 返回的结果

        """

        api = API["audio_info"]["info"]
        params = {"sid": self.__auid}
        return (
            await Api(**api, credential=self.credential).update_params(**params).result
        )

    async def get_tags(self) -> dict:
        """

        获取音频 tags



        Returns:

            dict: 调用 API 返回的结果

        """
        api = API["audio_info"]["tag"]
        params = {"sid": self.__auid}
        return (
            await Api(**api, credential=self.credential).update_params(**params).result
        )

    async def get_download_url(self) -> dict:
        """

        获取音频下载链接



        Returns:

            dict: 调用 API 返回的结果

        """
        api = API["audio_info"]["download_url"]
        params = {"sid": self.__auid, "privilege": 2, "quality": 2}
        return (
            await Api(**api, credential=self.credential).update_params(**params).result
        )

    async def add_coins(self, num: int = 2) -> dict:
        """

        投币



        Args:

            num (int, optional): 投币数量。Defaults to 2.



        Returns:

            dict: 调用 API 返回的结果

        """
        self.credential.raise_for_no_sessdata()

        api = API["audio_operate"]["coin"]
        data = {"sid": self.__auid, "multiply": num}

        return await Api(**api, credential=self.credential).update_data(**data).result

    # TODO: 音频编辑


class AudioList:
    """

    歌单



    Attributes:

        credential (Credential): 凭据类

    """

    def __init__(self, amid: int, credential: Union[Credential, None] = None):
        """

        Args:

            amid       (int)                        : 歌单 ID



            credential (Credential | None, optional): 凭据. Defaults to None.

        """
        self.__amid = amid
        self.credential = credential if credential is not None else Credential()

    def get_amid(self) -> int:
        return self.__amid

    async def get_info(self) -> dict:
        """

        获取歌单信息



        Returns:

            dict: 调用 API 返回的结果

        """

        api = API["list_info"]["info"]
        params = {"sid": self.__amid}
        return (
            await Api(**api, credential=self.credential).update_params(**params).result
        )

    async def get_tags(self) -> dict:
        """

        获取歌单 tags



        Returns:

            dict: 调用 API 返回的结果

        """

        api = API["list_info"]["tag"]
        params = {"sid": self.__amid}
        return (
            await Api(**api, credential=self.credential).update_params(**params).result
        )

    async def get_song_list(self, pn: int = 1) -> dict:
        """

        获取歌单歌曲列表



        Args:

            pn (int, optional): 页码. Defaults to 1



        Returns:

            dict: 调用 API 返回的结果

        """
        api = API["list_info"]["song_list"]
        params = {"sid": self.__amid, "pn": pn, "ps": 100}

        return (
            await Api(**api, credential=self.credential).update_params(**params).result
        )

    # TODO: 歌单编辑


async def get_user_stat(uid: int, credential: Union[Credential, None] = None) -> dict:
    """

    获取用户数据(收听数,粉丝数等)



    Args:

        uid        (int)                        : 用户 UID



        credential (Credential | None, optional): 凭据. Defaults to None



    Returns:

        dict: 调用 API 返回的结果

    """
    credential = credential if credential is not None else Credential()
    api = API["audio_info"]["user"]
    params = {"uid": uid}
    return await Api(**api, credential=credential).update_params(**params).result


async def get_hot_song_list(

    pn: int = 1, credential: Union[Credential, None] = None

) -> dict:
    """

    获取热门歌单



    Args:

        pn(int, optional)                       : 页数. Defaults to 1



        credential (Credential | None, optional): 凭据. Defaults to None



    Returns:

        dict: 调用 API 返回的结果

    """
    credential = credential if credential is not None else Credential()
    api = API["list_info"]["hot"]
    params = {"pn": pn, "ps": 100}
    return await Api(**api, credential=credential).update_params(**params).result