Skip to content

Commit 648485e

Browse files
committed
update docstring for main.py in apple-music-scraper
1 parent d6bc2e7 commit 648485e

File tree

1 file changed

+174
-2
lines changed

1 file changed

+174
-2
lines changed

Apple-Music-Scraper/main.py

Lines changed: 174 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,24 @@ def room_scrape(link="https://music.apple.com/us/room/6748797380"):
6262

6363

6464
def playlist_scrape(link="https://music.apple.com/us/playlist/new-music-daily/pl.2b0e6e332fdf4b7a91164da3162127b5"):
65+
"""
66+
Scrape an Apple Music playlist and extract all track URLs.
67+
68+
Parameters
69+
----------
70+
link : str, optional
71+
URL of the Apple Music playlist. Defaults to New Music Daily.
72+
73+
Returns
74+
-------
75+
list[str]
76+
List of converted song URLs from the playlist.
77+
78+
Notes
79+
-----
80+
Uses the 'track-list' section from Apple Music's internal serialized
81+
server data to extract song action URLs.
82+
"""
6583
result = []
6684
headers = {"User-Agent": "Mozilla/5.0"}
6785

@@ -101,6 +119,28 @@ def playlist_scrape(link="https://music.apple.com/us/playlist/new-music-daily/pl
101119

102120

103121
def search(keyword="sasha sloan"):
122+
"""
123+
Search Apple Music for artists, songs, albums, playlists and videos.
124+
125+
Parameters
126+
----------
127+
keyword : str, optional
128+
Search query to send to Apple Music. Defaults to "sasha sloan".
129+
130+
Returns
131+
-------
132+
dict
133+
Structured JSON-like dictionary containing search results:
134+
- artists
135+
- albums
136+
- songs
137+
- playlists
138+
- videos
139+
140+
Notes
141+
-----
142+
Scrapes `serialized-server-data` to access Apple Music's internal search structure.
143+
"""
104144
result = {"artists": [], "albums": [], "songs": [], "playlists": [], "videos": []}
105145
link = f"https://music.apple.com/us/search?term={keyword}"
106146
headers = {"User-Agent": "Mozilla/5.0"}
@@ -224,6 +264,30 @@ def search(keyword="sasha sloan"):
224264

225265

226266
def song_scrape(url="https://music.apple.com/us/song/california/1821538031"):
267+
"""
268+
Scrape a single Apple Music song page and extract metadata.
269+
270+
Parameters
271+
----------
272+
url : str, optional
273+
URL of the Apple Music song. Defaults to sample link.
274+
275+
Returns
276+
-------
277+
dict
278+
Dictionary containing:
279+
- title
280+
- image (full resolution)
281+
- kind (song type)
282+
- album info (title + URL)
283+
- artist info (title + URL)
284+
- preview-url
285+
- list of more songs
286+
287+
Notes
288+
-----
289+
Uses the `schema:song` JSON-LD tag to extract preview URL.
290+
"""
227291
result = {
228292
"title": "",
229293
"image": "",
@@ -303,6 +367,37 @@ def song_scrape(url="https://music.apple.com/us/song/california/1821538031"):
303367

304368

305369
def album_scrape(url="https://music.apple.com/us/album/1965/1817707266?i=1817707585"):
370+
"""
371+
Scrape an Apple Music album page and extract metadata, songs, related albums, videos, etc.
372+
373+
Parameters
374+
----------
375+
url : str, optional
376+
URL of the Apple Music album. Defaults to example album.
377+
378+
Returns
379+
-------
380+
dict
381+
Dictionary containing:
382+
- title
383+
- image
384+
- caption/description
385+
- artist info
386+
- song URLs
387+
- album info text
388+
- more songs (same artist)
389+
- similar (recommended) albums
390+
- videos related to the album
391+
392+
Notes
393+
-----
394+
Extracts multiple sections such as:
395+
- album-detail
396+
- track-list
397+
- similar albums
398+
- more by artist
399+
- album videos
400+
"""
306401
result = {
307402
"title": "",
308403
"image": "",
@@ -445,6 +540,30 @@ def album_scrape(url="https://music.apple.com/us/album/1965/1817707266?i=1817707
445540

446541

447542
def video_scrape(url="https://music.apple.com/us/music-video/gucci-mane-visualizer/1810547026"):
543+
"""
544+
Scrape Apple Music music-video page and extract metadata + video file URL.
545+
546+
Parameters
547+
----------
548+
url : str, optional
549+
URL of the Apple Music music-video. Defaults to example.
550+
551+
Returns
552+
-------
553+
dict
554+
{
555+
title,
556+
image,
557+
artist: {title, url},
558+
video-url,
559+
more (same artist),
560+
similar (same genre)
561+
}
562+
563+
Notes
564+
-----
565+
Uses JSON-LD block `schema:music-video` to extract the direct video content URL.
566+
"""
448567
result = {
449568
"title": "",
450569
"image": "",
@@ -546,6 +665,38 @@ def video_scrape(url="https://music.apple.com/us/music-video/gucci-mane-visualiz
546665

547666

548667
def artist_scrape(url="https://music.apple.com/us/artist/king-princess/1349968534"):
668+
"""
669+
Scrape an Apple Music artist page and extract all available metadata.
670+
671+
Parameters
672+
----------
673+
url : str, optional
674+
Apple Music artist page URL. Defaults to King Princess sample link.
675+
676+
Returns
677+
-------
678+
dict
679+
Dictionary containing:
680+
- title
681+
- image
682+
- latest release URL
683+
- list of top songs
684+
- all albums
685+
- singles & EPs
686+
- playlists
687+
- videos
688+
- similar artists
689+
- appears on
690+
- more-to-see (videos)
691+
- more-to-hear (songs)
692+
- about text
693+
- extra info (bio subtitle)
694+
695+
Notes
696+
-----
697+
This is the most complex scraper and extracts ~12 different sections
698+
from the artist page.
699+
"""
549700
result = {
550701
"title": "",
551702
"image": "",
@@ -745,8 +896,29 @@ def artist_scrape(url="https://music.apple.com/us/artist/king-princess/134996853
745896

746897
def test_all_functions():
747898
"""
748-
Test all scraper functions with sample URLs.
749-
Prints results count / key fields to verify basic functionality.
899+
Run integration-style tests for all scraper functions.
900+
901+
This function executes each scraper with sample inputs to verify that:
902+
- The function runs without raising exceptions.
903+
- The returned structures contain expected keys.
904+
- Basic counts (number of items, presence of preview/video URLs, etc.)
905+
match minimal sanity expectations.
906+
907+
Tests performed:
908+
1. room_scrape() – prints number of room items.
909+
2. playlist_scrape() – prints number of playlist items.
910+
3. search() – searches for "night tapes" and prints result counts.
911+
4. song_scrape() – scrapes a sample Apple Music song URL.
912+
5. album_scrape() – scrapes a sample Apple Music album URL.
913+
6. video_scrape() – scrapes a sample Apple Music video URL.
914+
7. artist_scrape() – scrapes a sample Apple Music artist page.
915+
916+
This is not a formal unit test suite, but a quick manual verification tool
917+
intended to confirm scraper functionality during development.
918+
919+
Prints:
920+
- Counts of returned items.
921+
- Key fields such as title, preview-url existence, etc.
750922
"""
751923

752924
print("\n=== TEST: room_scrape ===")

0 commit comments

Comments
 (0)