Skip to content

Commit 2b777ff

Browse files
committed
add docstring for utils.py in apple-music-scraper
1 parent 3e15ab1 commit 2b777ff

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

Apple-Music-Scraper/utils.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,66 @@
33
from bs4 import BeautifulSoup
44

55
def get_cover(url, width, height, format="jpg", crop_option=""):
6+
"""
7+
Generate a full Apple Music artwork URL with proper width, height, format, and crop settings.
8+
9+
Parameters
10+
----------
11+
url : str
12+
The original Apple Music artwork template URL containing `{w}`, `{h}`, `{f}`, `{c}`.
13+
width : int or str
14+
Target width of the image.
15+
height : int or str
16+
Target height of the image.
17+
format : str, optional
18+
Image format (jpg, png, etc.). Defaults to `"jpg"`.
19+
crop_option : str, optional
20+
Cropping mode used by Apple Music artwork URLs. Defaults to empty string.
21+
22+
Returns
23+
-------
24+
str
25+
Fully formatted artwork URL.
26+
27+
Notes
28+
-----
29+
Apple Music uses dynamic artwork URLs where dimensions and format are embedded
30+
in the URL as placeholders such as `{w}`, `{h}`, `{f}`, and `{c}`.
31+
"""
632
new_url = url.replace("{w}", str(width))
733
new_url = new_url.replace("{h}", str(height))
834
new_url = new_url.replace("{c}", crop_option)
935
new_url = new_url.replace("{f}", format)
1036
return new_url
1137

1238
def convert_album_to_song_url(album_url):
39+
"""
40+
Convert an Apple Music album-track URL into a direct Apple Music song URL.
41+
42+
Parameters
43+
----------
44+
album_url : str
45+
Full Apple Music album URL that contains a track ID via the query parameter `?i=...`.
46+
47+
Returns
48+
-------
49+
str or None
50+
Direct Apple Music song URL if `i` parameter exists.
51+
Otherwise, returns `None`.
52+
53+
Examples
54+
--------
55+
Input:
56+
https://music.apple.com/us/album/song-name/12345?i=67890
57+
58+
Output:
59+
https://music.apple.com/us/song/song-name/67890
60+
61+
Notes
62+
-----
63+
Apple Music album pages embed individual song IDs through the query parameter `i`,
64+
which must be extracted and placed into a `/song/` URL.
65+
"""
1366
parsed = urllib.parse.urlparse(album_url)
1467
query_params = urllib.parse.parse_qs(parsed.query)
1568
song_id = query_params.get('i', [None])[0]
@@ -24,6 +77,26 @@ def convert_album_to_song_url(album_url):
2477
return f"https://music.apple.com/{country}/song/{title}/{song_id}"
2578

2679
def get_all_singles(url="https://music.apple.com/us/artist/king-princess/1349968534"):
80+
"""
81+
Fetch all singles & EP URLs from an Apple Music artist page.
82+
83+
Parameters
84+
----------
85+
url : str, optional
86+
Base artist page URL. Defaults to the sample King Princess artist link.
87+
88+
Returns
89+
-------
90+
list[str]
91+
A list of Apple Music URLs for all singles & EPs for the artist.
92+
93+
Notes
94+
-----
95+
- Apple Music loads singles under the `/see-all?section=singles` endpoint.
96+
- This function retrieves the serialized server data, parses the `items` section,
97+
and extracts the correct song/EP URLs.
98+
- Used internally by `artist_scrape()`.
99+
"""
27100
result = []
28101
url = url+"/see-all?section=singles"
29102

0 commit comments

Comments
 (0)