yutipy 1.0.0__tar.gz → 1.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of yutipy might be problematic. Click here for more details.

Files changed (49) hide show
  1. {yutipy-1.0.0 → yutipy-1.1.0}/.github/workflows/release.yml +1 -0
  2. {yutipy-1.0.0 → yutipy-1.1.0}/PKG-INFO +13 -1
  3. {yutipy-1.0.0 → yutipy-1.1.0}/README.md +12 -0
  4. {yutipy-1.0.0 → yutipy-1.1.0}/docs/api_reference.rst +9 -7
  5. yutipy-1.1.0/docs/available_platforms.rst +13 -0
  6. {yutipy-1.0.0 → yutipy-1.1.0}/docs/faq.rst +8 -2
  7. {yutipy-1.0.0 → yutipy-1.1.0}/docs/index.rst +4 -4
  8. {yutipy-1.0.0 → yutipy-1.1.0}/docs/usage_examples.rst +32 -3
  9. yutipy-1.1.0/tests/test_kkbox.py +28 -0
  10. {yutipy-1.0.0 → yutipy-1.1.0}/yutipy/exceptions.py +4 -0
  11. yutipy-1.1.0/yutipy/kkbox.py +320 -0
  12. {yutipy-1.0.0 → yutipy-1.1.0}/yutipy/spotify.py +36 -19
  13. {yutipy-1.0.0 → yutipy-1.1.0}/yutipy.egg-info/PKG-INFO +13 -1
  14. {yutipy-1.0.0 → yutipy-1.1.0}/yutipy.egg-info/SOURCES.txt +3 -0
  15. {yutipy-1.0.0 → yutipy-1.1.0}/.gitattributes +0 -0
  16. {yutipy-1.0.0 → yutipy-1.1.0}/.github/FUNDING.yml +0 -0
  17. {yutipy-1.0.0 → yutipy-1.1.0}/.github/workflows/pytest-unit-testing.yml +0 -0
  18. {yutipy-1.0.0 → yutipy-1.1.0}/.gitignore +0 -0
  19. {yutipy-1.0.0 → yutipy-1.1.0}/.readthedocs.yaml +0 -0
  20. {yutipy-1.0.0 → yutipy-1.1.0}/LICENSE +0 -0
  21. {yutipy-1.0.0 → yutipy-1.1.0}/MANIFEST.in +0 -0
  22. {yutipy-1.0.0 → yutipy-1.1.0}/docs/Makefile +0 -0
  23. {yutipy-1.0.0 → yutipy-1.1.0}/docs/_static/yutipy_header.png +0 -0
  24. {yutipy-1.0.0 → yutipy-1.1.0}/docs/_static/yutipy_logo.png +0 -0
  25. {yutipy-1.0.0 → yutipy-1.1.0}/docs/conf.py +0 -0
  26. {yutipy-1.0.0 → yutipy-1.1.0}/docs/installation.rst +0 -0
  27. {yutipy-1.0.0 → yutipy-1.1.0}/docs/make.bat +0 -0
  28. {yutipy-1.0.0 → yutipy-1.1.0}/docs/requirements.txt +0 -0
  29. {yutipy-1.0.0 → yutipy-1.1.0}/pyproject.toml +0 -0
  30. {yutipy-1.0.0 → yutipy-1.1.0}/requirements-dev.txt +0 -0
  31. {yutipy-1.0.0 → yutipy-1.1.0}/requirements.txt +0 -0
  32. {yutipy-1.0.0 → yutipy-1.1.0}/setup.cfg +0 -0
  33. {yutipy-1.0.0 → yutipy-1.1.0}/tests/__init__.py +0 -0
  34. {yutipy-1.0.0 → yutipy-1.1.0}/tests/test_deezer.py +0 -0
  35. {yutipy-1.0.0 → yutipy-1.1.0}/tests/test_itunes.py +0 -0
  36. {yutipy-1.0.0 → yutipy-1.1.0}/tests/test_models.py +0 -0
  37. {yutipy-1.0.0 → yutipy-1.1.0}/tests/test_musicyt.py +0 -0
  38. {yutipy-1.0.0 → yutipy-1.1.0}/tests/test_spotify.py +0 -0
  39. {yutipy-1.0.0 → yutipy-1.1.0}/tests/test_utils.py +0 -0
  40. {yutipy-1.0.0 → yutipy-1.1.0}/yutipy/__init__.py +0 -0
  41. {yutipy-1.0.0 → yutipy-1.1.0}/yutipy/deezer.py +0 -0
  42. {yutipy-1.0.0 → yutipy-1.1.0}/yutipy/itunes.py +0 -0
  43. {yutipy-1.0.0 → yutipy-1.1.0}/yutipy/models.py +0 -0
  44. {yutipy-1.0.0 → yutipy-1.1.0}/yutipy/musicyt.py +0 -0
  45. {yutipy-1.0.0 → yutipy-1.1.0}/yutipy/utils/__init__.py +0 -0
  46. {yutipy-1.0.0 → yutipy-1.1.0}/yutipy/utils/cheap_utils.py +0 -0
  47. {yutipy-1.0.0 → yutipy-1.1.0}/yutipy.egg-info/dependency_links.txt +0 -0
  48. {yutipy-1.0.0 → yutipy-1.1.0}/yutipy.egg-info/requires.txt +0 -0
  49. {yutipy-1.0.0 → yutipy-1.1.0}/yutipy.egg-info/top_level.txt +0 -0
@@ -117,3 +117,4 @@ jobs:
117
117
  uses: pypa/gh-action-pypi-publish@76f52bc884231f62b9a034ebfe128415bbaabdfc # v1.12.4
118
118
  with:
119
119
  repository-url: https://test.pypi.org/legacy/
120
+ verbose: true
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: yutipy
3
- Version: 1.0.0
3
+ Version: 1.1.0
4
4
  Summary: A simple package for retrieving music information from various music platforms APIs.
5
5
  Author: Cheap Nightbot
6
6
  Author-email: Cheap Nightbot <hi@cheapnightbot.slmail.me>
@@ -85,6 +85,7 @@ A _**simple**_ Python package for searching and retrieving music information fro
85
85
  ## Table of Contents
86
86
 
87
87
  - [Features](#features)
88
+ - [Available Music Platforms](#available-music-platforms)
88
89
  - [Installation](#installation)
89
90
  - [Usage Example](#usage-example)
90
91
  - [Contributing](#contributing)
@@ -97,6 +98,17 @@ A _**simple**_ Python package for searching and retrieving music information fro
97
98
  - It uses `RapidFuzz` to compare & return the best match so that you can be sure you got what you asked for without having to worry and doing all that work by yourself.
98
99
  - Retrieve detailed music information, including album art, release dates, lyrics, ISRC, and UPC codes.
99
100
 
101
+ ### Available Music Platforms
102
+
103
+ Right now, the following music platforms are available in yutipy for searching music. New platforms will be added in the future.
104
+ Feel free to request any music platform you would like me to add by opening an issue on [GitHub](https://github.com/CheapNightbot/yutipy/issues) or by emailing me.
105
+
106
+ - `Deezer`: https://www.deezer.com
107
+ - `iTunes`: https://music.apple.com
108
+ - `KKBOX`: https://www.kkbox.com
109
+ - `Spotify`: https://spotify.com
110
+ - `YouTube Music`: https://music.youtube.com
111
+
100
112
  ## Installation
101
113
 
102
114
  You can install the package using pip. Make sure you have Python 3.8 or higher installed.
@@ -28,6 +28,7 @@ A _**simple**_ Python package for searching and retrieving music information fro
28
28
  ## Table of Contents
29
29
 
30
30
  - [Features](#features)
31
+ - [Available Music Platforms](#available-music-platforms)
31
32
  - [Installation](#installation)
32
33
  - [Usage Example](#usage-example)
33
34
  - [Contributing](#contributing)
@@ -40,6 +41,17 @@ A _**simple**_ Python package for searching and retrieving music information fro
40
41
  - It uses `RapidFuzz` to compare & return the best match so that you can be sure you got what you asked for without having to worry and doing all that work by yourself.
41
42
  - Retrieve detailed music information, including album art, release dates, lyrics, ISRC, and UPC codes.
42
43
 
44
+ ### Available Music Platforms
45
+
46
+ Right now, the following music platforms are available in yutipy for searching music. New platforms will be added in the future.
47
+ Feel free to request any music platform you would like me to add by opening an issue on [GitHub](https://github.com/CheapNightbot/yutipy/issues) or by emailing me.
48
+
49
+ - `Deezer`: https://www.deezer.com
50
+ - `iTunes`: https://music.apple.com
51
+ - `KKBOX`: https://www.kkbox.com
52
+ - `Spotify`: https://spotify.com
53
+ - `YouTube Music`: https://music.youtube.com
54
+
43
55
  ## Installation
44
56
 
45
57
  You can install the package using pip. Make sure you have Python 3.8 or higher installed.
@@ -5,13 +5,6 @@ API Reference
5
5
  Main Classes
6
6
  =============
7
7
 
8
- The following classes are available for use, utilizing the APIs of their respective music streaming platforms:
9
-
10
- - ``Deezer``: https://www.deezer.com
11
- - ``iTunes``: https://music.apple.com
12
- - ``Spotify``: https://spotify.com
13
- - ``YouTube Music``: https://music.youtube.com
14
-
15
8
  Deezer
16
9
  ------
17
10
 
@@ -30,6 +23,15 @@ iTunes
30
23
  :noindex:
31
24
  :exclude-members: is_session_closed
32
25
 
26
+ KKBox
27
+ -------
28
+
29
+ .. autoclass:: yutipy.kkbox.KKBox
30
+ :members:
31
+ :inherited-members:
32
+ :noindex:
33
+ :exclude-members: is_session_closed
34
+
33
35
  Spotify
34
36
  -------
35
37
 
@@ -0,0 +1,13 @@
1
+ =========================
2
+ Available Music Platforms
3
+ =========================
4
+
5
+ Right now, the following music platforms are available in yutipy for searching music. New platforms will be added in the future.
6
+ Feel free to request any music platform you would like me to add by opening an issue on `GitHub <https://github.com/CheapNightbot/yutipy/issues>`_ or by emailing me.
7
+
8
+
9
+ - ``Deezer``: https://www.deezer.com
10
+ - ``iTunes``: https://music.apple.com
11
+ - ``KKBOX``: https://www.kkbox.com
12
+ - ``Spotify``: https://spotify.com
13
+ - ``YouTube Music``: https://music.youtube.com
@@ -58,12 +58,18 @@ obtained from the Spotify for Developers website. To do this, follow these steps
58
58
 
59
59
  .. code-block:: bash
60
60
 
61
- CLIENT_ID=<spotify_client_id>
62
- CLIENT_SECRET=<spotify_client_secret>
61
+ SPOTIFY_CLIENT_ID=<spotify_client_id>
62
+ SPOTIFY_CLIENT_SECRET=<spotify_client_secret>
63
63
  - Ensure you replace ``<spotify_client_id>`` and ``<spotify_client_secret>`` with the values you copied in steps :ref:`5 <step_5>` and :ref:`6 <step_6>` respectively.
64
64
 
65
65
  After completing these steps, you should be able to use the ``Spotify`` class and its methods as expected.
66
66
 
67
+ Why am I receiving a ``KKBoxException`` when trying to use the ``KKBox`` class?
68
+ -------------------------------------------------------------------------------
69
+
70
+ Unfortunately, it's the same case as with Spotify. You will need a ``Client ID`` and ``Client Secret``
71
+ obtained from the KKBOX for Developers website. Please visit https://developer.kkbox.com/ for more information.
72
+
67
73
  ----
68
74
 
69
75
  .. [#] There may be additional features in the future.
@@ -3,9 +3,8 @@
3
3
  :target: https://github.com/CheapNightbot/yutipy
4
4
  :align: center
5
5
 
6
- **yutipy** is a Python package for searching and retrieving music information from various music platforms,
7
- including Deezer, iTunes, Spotify, and YouTube Music. This documentation will help you get started with yutipy
8
- and provide detailed information about its features and usage.
6
+ **yutipy** is a Python package for searching and retrieving music information from various music platforms (see list of :doc:`available muisc platforms <available_platforms>`).
7
+ This documentation will help you get started with yutipy and provide detailed information about its features and usage.
9
8
 
10
9
  .. raw:: html
11
10
 
@@ -45,8 +44,9 @@ Get Started
45
44
  :maxdepth: 2
46
45
 
47
46
  installation
48
- usage_examples
47
+ available_platforms
49
48
  api_reference
49
+ usage_examples
50
50
  faq
51
51
 
52
52
  .. toctree::
@@ -32,17 +32,46 @@ iTunes
32
32
  result = itunes.search("Artist Name", "Song Title")
33
33
  print(result)
34
34
 
35
+
36
+ KKBOX
37
+ -------
38
+
39
+ To use the KKBOX Open API, you need to set the ``KKBOX_CLIENT_ID`` and ``KKBOX_CLIENT_SECRET`` for KKBOX. You can do this by creating a ``.env`` file in the root directory of your project with the following content:
40
+
41
+ .. admonition:: .env
42
+
43
+ .. code-block:: bash
44
+
45
+ KKBOX_CLIENT_ID=<your_kkbox_client_id>
46
+ KKBOX_CLIENT_SECRET=<your_kkbox_client_secret>
47
+
48
+ Alternatively, you can manually provide these values when creating an object of the `KKBox` class:
49
+
50
+ .. code-block:: python
51
+
52
+ from yutipy.kkbox import KKBox
53
+
54
+ kkbox = KKBox(client_id="your_kkbox_client_id", client_secret="your_kkbox_client_secret")
55
+
56
+ .. code-block:: python
57
+
58
+ from yutipy.kkbox import KKBox
59
+
60
+ with KKBox() as kkbox:
61
+ result = kkbox.search("Artist Name", "Song Title")
62
+ print(result)
63
+
35
64
  Spotify
36
65
  -------
37
66
 
38
- To use the Spotify API, you need to set the ``CLIENT_ID`` and ``CLIENT_SECRET`` for Spotify. You can do this by creating a ``.env`` file in the root directory of your project with the following content:
67
+ To use the Spotify API, you need to set the ``SPOTIFY_CLIENT_ID`` and ``SPOTIFY_CLIENT_SECRET`` for Spotify. You can do this by creating a ``.env`` file in the root directory of your project with the following content:
39
68
 
40
69
  .. admonition:: .env
41
70
 
42
71
  .. code-block:: bash
43
72
 
44
- CLIENT_ID=your_spotify_client_id
45
- CLIENT_SECRET=your_spotify_client_secret
73
+ SPOTIFY_CLIENT_ID=<your_spotify_client_id>
74
+ SPOTIFY_CLIENT_SECRET=<your_spotify_client_secret>
46
75
 
47
76
  Alternatively, you can manually provide these values when creating an object of the `Spotify` class:
48
77
 
@@ -0,0 +1,28 @@
1
+ import pytest
2
+
3
+ from yutipy.exceptions import KKBoxException
4
+ from yutipy.models import MusicInfo
5
+ from yutipy.kkbox import KKBox
6
+
7
+
8
+ @pytest.fixture(scope="module")
9
+ def kkbox():
10
+ try:
11
+ return KKBox()
12
+ except KKBoxException:
13
+ pytest.skip("KKBOX credentials not found")
14
+
15
+
16
+ def test_search(kkbox):
17
+ artist = "Porter Robinson"
18
+ song = "Shelter"
19
+ result = kkbox.search(artist, song)
20
+ assert result is not None
21
+ assert isinstance(result, MusicInfo)
22
+ assert result.title == song
23
+ assert artist in result.artists
24
+
25
+
26
+ def test_close_session(kkbox):
27
+ kkbox.close_session()
28
+ assert kkbox.is_session_closed
@@ -50,3 +50,7 @@ class InvalidResponseException(YutipyException):
50
50
  """Exception raised for invalid responses from APIs."""
51
51
 
52
52
  pass
53
+
54
+
55
+ class KKBoxException(YutipyException):
56
+ """Exception raised for erros related to the KKBOX Open API."""
@@ -0,0 +1,320 @@
1
+ import base64
2
+ import os
3
+ import time
4
+ from pprint import pprint
5
+ from typing import Optional, Union
6
+
7
+ import requests
8
+ from dotenv import load_dotenv
9
+
10
+ from yutipy.exceptions import (
11
+ AuthenticationException,
12
+ InvalidResponseException,
13
+ InvalidValueException,
14
+ KKBoxException,
15
+ NetworkException,
16
+ )
17
+ from yutipy.models import MusicInfo
18
+ from yutipy.utils.cheap_utils import are_strings_similar, is_valid_string
19
+
20
+ load_dotenv()
21
+
22
+ KKBOX_CLIENT_ID = os.getenv("KKBOX_CLIENT_ID")
23
+ KKBOX_CLIENT_SECRET = os.getenv("KKBOX_CLIENT_SECRET")
24
+
25
+
26
+ class KKBox:
27
+ """
28
+ A class to interact with KKBOX Open API.
29
+
30
+ This class reads the ``KKBOX_CLIENT_ID`` and ``KKBOX_CLIENT_SECRET`` from environment variables or the ``.env`` file by default.
31
+ Alternatively, you can manually provide these values when creating an object.
32
+ """
33
+
34
+ def __init__(
35
+ self, client_id: str = KKBOX_CLIENT_ID, client_secret: str = KKBOX_CLIENT_SECRET
36
+ ) -> None:
37
+ """
38
+ Initializes the KKBox class and sets up the session.
39
+
40
+ Parameters
41
+ ----------
42
+ client_id : str, optional
43
+ The Client ID for the KKBOX Open API. Defaults to ``KKBOX_CLIENT_ID`` from .env file.
44
+ client_secret : str, optional
45
+ The Client secret for the KKBOX Open API. Defaults to ``KKBOX_CLIENT_SECRET`` from .env file.
46
+ """
47
+ if not client_id or not client_secret:
48
+ raise KKBoxException(
49
+ "Failed to read `KKBOX_CLIENT_ID` and/or `KKBOX_CLIENT_SECRET` from environment variables. Client ID and Client Secret must be provided."
50
+ )
51
+
52
+ self.client_id = client_id
53
+ self.client_secret = client_secret
54
+ self._session = requests.Session()
55
+ self.api_url = "https://api.kkbox.com/v1.1"
56
+ self.__header, self.__expires_in = self.__authenticate()
57
+ self.__start_time = time.time()
58
+ self._is_session_closed = False
59
+
60
+ def __enter__(self):
61
+ """Enters the runtime context related to this object."""
62
+ return self
63
+
64
+ def __exit__(self, exc_type, exc_value, exc_traceback):
65
+ """Exits the runtime context related to this object."""
66
+ self._close_session()
67
+
68
+ def close_session(self) -> None:
69
+ """Closes the current session."""
70
+ if not self.is_session_closed:
71
+ self._session.close()
72
+ self._is_session_closed = True
73
+
74
+ @property
75
+ def is_session_closed(self) -> bool:
76
+ """Checks if the session is closed."""
77
+ return self._is_session_closed
78
+
79
+ def __authenticate(self) -> tuple:
80
+ """
81
+ Authenticates with the KKBOX Open API and returns the authorization header.
82
+
83
+ Returns
84
+ -------
85
+ dict
86
+ The authorization header.
87
+ """
88
+ try:
89
+ token, expires_in = self.__get_access_token()
90
+ return {"Authorization": f"Bearer {token}"}, expires_in
91
+ except Exception as e:
92
+ raise AuthenticationException(
93
+ "Failed to authenticate with KKBOX Open API"
94
+ ) from e
95
+
96
+ def __get_access_token(self) -> tuple:
97
+ """
98
+ Gets the KKBOX Open API token.
99
+
100
+ Returns
101
+ -------
102
+ str
103
+ The KKBOX Open API token.
104
+ """
105
+ auth_string = f"{self.client_id}:{self.client_secret}"
106
+ auth_base64 = base64.b64encode(auth_string.encode("utf-8")).decode("utf-8")
107
+
108
+ url = " https://account.kkbox.com/oauth2/token"
109
+ headers = {
110
+ "Authorization": f"Basic {auth_base64}",
111
+ "Content-Type": "application/x-www-form-urlencoded",
112
+ }
113
+ data = {"grant_type": "client_credentials"}
114
+
115
+ try:
116
+ response = self._session.post(
117
+ url=url, headers=headers, data=data, timeout=30
118
+ )
119
+ response.raise_for_status()
120
+ except requests.RequestException as e:
121
+ raise NetworkException(f"Network error occurred: {e}")
122
+
123
+ try:
124
+ response_json = response.json()
125
+ return response_json.get("access_token"), response_json.get("expires_in")
126
+ except (KeyError, ValueError) as e:
127
+ raise InvalidResponseException(f"Invalid response received: {e}")
128
+
129
+ def __refresh_token_if_expired(self):
130
+ """Refreshes the token if it has expired."""
131
+ if time.time() - self.__start_time >= self.__expires_in:
132
+ self.__header, self.__expires_in = self.__authenticate()
133
+ self.__start_time = time.time()
134
+
135
+ def search(
136
+ self, artist: str, song: str, territory: str = "TW"
137
+ ) -> Optional[MusicInfo]:
138
+ """
139
+ Searches for a song by artist and title.
140
+
141
+ Parameters
142
+ ----------
143
+ artist : str
144
+ The name of the artist.
145
+ song : str
146
+ The title of the song.
147
+ territory : str
148
+ Two-letter country codes from ISO 3166-1 alpha-2.
149
+ Allowed values: ``HK``, ``JP``, ``MY``, ``SG``, ``TW``.
150
+
151
+ Returns
152
+ -------
153
+ Optional[MusicInfo_]
154
+ The music information if found, otherwise None.
155
+ """
156
+ if not is_valid_string(artist) or not is_valid_string(song):
157
+ raise InvalidValueException(
158
+ "Artist and song names must be valid strings and can't be empty."
159
+ )
160
+
161
+ self.__refresh_token_if_expired()
162
+
163
+ query = f"?q={artist} - {song}&type=track,album&territory={territory}&limit=10"
164
+ query_url = f"{self.api_url}/search{query}"
165
+
166
+ try:
167
+ response = self._session.get(query_url, headers=self.__header, timeout=30)
168
+ response.raise_for_status()
169
+ except requests.RequestException as e:
170
+ raise NetworkException(f"Network error occurred: {e}")
171
+
172
+ if response.status_code != 200:
173
+ raise KKBoxException(f"Failed to search for music: {response.json()}")
174
+
175
+ return self._find_music_info(artist, song, response.json())
176
+
177
+ def _find_music_info(
178
+ self, artist: str, song: str, response_json: dict
179
+ ) -> Optional[MusicInfo]:
180
+ """
181
+ Finds the music information from the search results.
182
+
183
+ Parameters
184
+ ----------
185
+ artist : str
186
+ The name of the artist.
187
+ song : str
188
+ The title of the song.
189
+ response_json : dict
190
+ The JSON response from the API.
191
+
192
+ Returns
193
+ -------
194
+ Optional[MusicInfo]
195
+ The music information if found, otherwise None.
196
+ """
197
+ try:
198
+ for track in response_json["tracks"]["data"]:
199
+ music_info = self._find_track(song, artist, track)
200
+ if music_info:
201
+ return music_info
202
+ except KeyError:
203
+ pass
204
+
205
+ try:
206
+ for album in response_json["albums"]["data"]:
207
+ music_info = self._find_album(song, artist, album)
208
+ if music_info:
209
+ return music_info
210
+ except KeyError:
211
+ pass
212
+
213
+ return None
214
+
215
+ def _find_track(self, song: str, artist: str, track: dict) -> Optional[MusicInfo]:
216
+ """
217
+ Finds the track information from the search results.
218
+
219
+ Parameters
220
+ ----------
221
+ song : str
222
+ The title of the song.
223
+ artist : str
224
+ The name of the artist.
225
+ track : dict
226
+ A single track from the search results.
227
+ artist_ids : list
228
+ A list of artist IDs.
229
+
230
+ Returns
231
+ -------
232
+ Optional[MusicInfo]
233
+ The music information if found, otherwise None.
234
+ """
235
+ if not are_strings_similar(track["name"], song):
236
+ return None
237
+
238
+ artists_name = track["album"]["artist"]["name"]
239
+ matching_artists = (
240
+ artists_name if are_strings_similar(artists_name, artist) else None
241
+ )
242
+
243
+ if matching_artists:
244
+ return MusicInfo(
245
+ album_art=track["album"]["images"][2]["url"],
246
+ album_title=track["album"]["name"],
247
+ album_type=None,
248
+ artists=artists_name,
249
+ genre=None,
250
+ id=track["id"],
251
+ isrc=track["isrc"],
252
+ lyrics=None,
253
+ release_date=track["album"]["release_date"],
254
+ tempo=None,
255
+ title=track["name"],
256
+ type="track",
257
+ upc=None,
258
+ url=track["url"],
259
+ )
260
+
261
+ return None
262
+
263
+ def _find_album(self, song: str, artist: str, album: dict) -> Optional[MusicInfo]:
264
+ """
265
+ Finds the album information from the search results.
266
+
267
+ Parameters
268
+ ----------
269
+ song : str
270
+ The title of the song.
271
+ artist : str
272
+ The name of the artist.
273
+ album : dict
274
+ A single album from the search results.
275
+ artist_ids : list
276
+ A list of artist IDs.
277
+
278
+ Returns
279
+ -------
280
+ Optional[MusicInfo]
281
+ The music information if found, otherwise None.
282
+ """
283
+ if not are_strings_similar(album["name"], song):
284
+ return None
285
+
286
+ artists_name = album["artist"]["name"]
287
+ matching_artists = (
288
+ artists_name if are_strings_similar(artists_name, artist) else None
289
+ )
290
+
291
+ if matching_artists:
292
+ return MusicInfo(
293
+ album_art=album["images"][2]["url"],
294
+ album_title=album["name"],
295
+ album_type=None,
296
+ artists=artists_name,
297
+ genre=None,
298
+ id=album["id"],
299
+ isrc=None,
300
+ lyrics=None,
301
+ release_date=album["release_date"],
302
+ tempo=None,
303
+ title=album["name"],
304
+ type="album",
305
+ upc=None,
306
+ url=album["url"],
307
+ )
308
+
309
+ return None
310
+
311
+
312
+ if __name__ == "__main__":
313
+ kkbox = KKBox(KKBOX_CLIENT_ID, KKBOX_CLIENT_SECRET)
314
+
315
+ try:
316
+ artist_name = input("Artist Name: ")
317
+ song_name = input("Song Name: ")
318
+ pprint(kkbox.search(artist_name, song_name))
319
+ finally:
320
+ kkbox.close_session()
@@ -36,7 +36,9 @@ class Spotify:
36
36
  """
37
37
 
38
38
  def __init__(
39
- self, client_id: str = SPOTIFY_CLIENT_ID, client_secret: str = SPOTIFY_CLIENT_SECRET
39
+ self,
40
+ client_id: str = SPOTIFY_CLIENT_ID,
41
+ client_secret: str = SPOTIFY_CLIENT_SECRET,
40
42
  ) -> None:
41
43
  """
42
44
  Initializes the Spotify class and sets up the session.
@@ -50,7 +52,7 @@ class Spotify:
50
52
  """
51
53
  if not client_id or not client_secret:
52
54
  raise SpotifyException(
53
- "Failed to read `SPOTIFY_CLIENT_ID` and `SPOTIFY_CLIENT_SECRET` from environment variables. Client ID and Client Secret must be provided."
55
+ "Failed to read `SPOTIFY_CLIENT_ID` and/or `SPOTIFY_CLIENT_SECRET` from environment variables. Client ID and Client Secret must be provided."
54
56
  )
55
57
 
56
58
  self.client_id = client_id
@@ -157,22 +159,37 @@ class Spotify:
157
159
  "Artist and song names must be valid strings and can't be empty."
158
160
  )
159
161
 
160
- self.__refresh_token_if_expired()
162
+ music_info = None
163
+ queries = [
164
+ f"?q=artist:{artist} track:{song}&type=track&limit=10",
165
+ f"?q=artist:{artist} album:{song}&type=album&limit=10",
166
+ ]
161
167
 
162
- query = f"?q=artist:{artist} track:{song}&type=track&limit=10"
163
- query_url = f"{self.api_url}/search{query}"
168
+ for query in queries:
169
+ if music_info:
170
+ return music_info
164
171
 
165
- try:
166
- response = self._session.get(query_url, headers=self.__header, timeout=30)
167
- response.raise_for_status()
168
- except requests.RequestException as e:
169
- raise NetworkException(f"Network error occurred: {e}")
172
+ self.__refresh_token_if_expired()
170
173
 
171
- if response.status_code != 200:
172
- raise SpotifyException(f"Failed to search for music: {response.json()}")
174
+ query_url = f"{self.api_url}/search{query}"
173
175
 
174
- artist_ids = self._get_artists_ids(artist)
175
- return self._find_music_info(artist, song, response.json(), artist_ids)
176
+ try:
177
+ response = self._session.get(
178
+ query_url, headers=self.__header, timeout=30
179
+ )
180
+ response.raise_for_status()
181
+ except requests.RequestException as e:
182
+ raise NetworkException(f"Network error occurred: {e}")
183
+
184
+ if response.status_code != 200:
185
+ raise SpotifyException(f"Failed to search for music: {response.json()}")
186
+
187
+ artist_ids = self._get_artists_ids(artist)
188
+ music_info = self._find_music_info(
189
+ artist, song, response.json(), artist_ids
190
+ )
191
+
192
+ return music_info
176
193
 
177
194
  def search_advanced(
178
195
  self, artist: str, song: str, isrc: str = None, upc: str = None
@@ -282,7 +299,7 @@ class Spotify:
282
299
  """
283
300
  try:
284
301
  for track in response_json["tracks"]["items"]:
285
- music_info = self._find_tracks(song, artist, track, artist_ids)
302
+ music_info = self._find_track(song, artist, track, artist_ids)
286
303
  if music_info:
287
304
  return music_info
288
305
  except KeyError:
@@ -298,7 +315,7 @@ class Spotify:
298
315
 
299
316
  return None
300
317
 
301
- def _find_tracks(
318
+ def _find_track(
302
319
  self, song: str, artist: str, track: dict, artist_ids: list
303
320
  ) -> Optional[MusicInfo]:
304
321
  """
@@ -404,11 +421,11 @@ class Spotify:
404
421
 
405
422
 
406
423
  if __name__ == "__main__":
407
- Spotify = Spotify(SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET)
424
+ spotify = Spotify(SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET)
408
425
 
409
426
  try:
410
427
  artist_name = input("Artist Name: ")
411
428
  song_name = input("Song Name: ")
412
- pprint(Spotify.search(artist_name, song_name))
429
+ pprint(spotify.search(artist_name, song_name))
413
430
  finally:
414
- Spotify.close_session()
431
+ spotify.close_session()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: yutipy
3
- Version: 1.0.0
3
+ Version: 1.1.0
4
4
  Summary: A simple package for retrieving music information from various music platforms APIs.
5
5
  Author: Cheap Nightbot
6
6
  Author-email: Cheap Nightbot <hi@cheapnightbot.slmail.me>
@@ -85,6 +85,7 @@ A _**simple**_ Python package for searching and retrieving music information fro
85
85
  ## Table of Contents
86
86
 
87
87
  - [Features](#features)
88
+ - [Available Music Platforms](#available-music-platforms)
88
89
  - [Installation](#installation)
89
90
  - [Usage Example](#usage-example)
90
91
  - [Contributing](#contributing)
@@ -97,6 +98,17 @@ A _**simple**_ Python package for searching and retrieving music information fro
97
98
  - It uses `RapidFuzz` to compare & return the best match so that you can be sure you got what you asked for without having to worry and doing all that work by yourself.
98
99
  - Retrieve detailed music information, including album art, release dates, lyrics, ISRC, and UPC codes.
99
100
 
101
+ ### Available Music Platforms
102
+
103
+ Right now, the following music platforms are available in yutipy for searching music. New platforms will be added in the future.
104
+ Feel free to request any music platform you would like me to add by opening an issue on [GitHub](https://github.com/CheapNightbot/yutipy/issues) or by emailing me.
105
+
106
+ - `Deezer`: https://www.deezer.com
107
+ - `iTunes`: https://music.apple.com
108
+ - `KKBOX`: https://www.kkbox.com
109
+ - `Spotify`: https://spotify.com
110
+ - `YouTube Music`: https://music.youtube.com
111
+
100
112
  ## Installation
101
113
 
102
114
  You can install the package using pip. Make sure you have Python 3.8 or higher installed.
@@ -12,6 +12,7 @@ requirements.txt
12
12
  .github/workflows/release.yml
13
13
  docs/Makefile
14
14
  docs/api_reference.rst
15
+ docs/available_platforms.rst
15
16
  docs/conf.py
16
17
  docs/faq.rst
17
18
  docs/index.rst
@@ -24,6 +25,7 @@ docs/_static/yutipy_logo.png
24
25
  tests/__init__.py
25
26
  tests/test_deezer.py
26
27
  tests/test_itunes.py
28
+ tests/test_kkbox.py
27
29
  tests/test_models.py
28
30
  tests/test_musicyt.py
29
31
  tests/test_spotify.py
@@ -32,6 +34,7 @@ yutipy/__init__.py
32
34
  yutipy/deezer.py
33
35
  yutipy/exceptions.py
34
36
  yutipy/itunes.py
37
+ yutipy/kkbox.py
35
38
  yutipy/models.py
36
39
  yutipy/musicyt.py
37
40
  yutipy/spotify.py
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes