Skip to content

Commit 3e15ab1

Browse files
committed
add docstring for main.py in apple-music-scraper
1 parent d88ae3c commit 3e15ab1

File tree

1 file changed

+170
-3
lines changed

1 file changed

+170
-3
lines changed

Apple-Music-Scraper/main.py

Lines changed: 170 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,25 @@
44

55

66
def room_scrape(link="https://music.apple.com/us/room/6748797380"):
7+
"""
8+
Scrape a shared Apple Music room and extract song URLs.
9+
10+
Parameters
11+
----------
12+
link : str, optional
13+
URL of the Apple Music room page. Defaults to an example room link.
14+
15+
Returns
16+
-------
17+
list[str]
18+
List of converted song URLs extracted from the room.
19+
20+
Notes
21+
-----
22+
This function parses the `serialized-server-data` script tag within
23+
the Apple Music room HTML, locates the 'copper-track-swoosh' section,
24+
and extracts track URLs.
25+
"""
726
result = []
827
headers = {
928
"User-Agent": "Mozilla/5.0"
@@ -29,6 +48,24 @@ def room_scrape(link="https://music.apple.com/us/room/6748797380"):
2948
return result
3049

3150
def playlist_scrape(link="https://music.apple.com/us/playlist/new-music-daily/pl.2b0e6e332fdf4b7a91164da3162127b5"):
51+
"""
52+
Scrape an Apple Music playlist and extract all track URLs.
53+
54+
Parameters
55+
----------
56+
link : str, optional
57+
URL of the Apple Music playlist. Defaults to New Music Daily.
58+
59+
Returns
60+
-------
61+
list[str]
62+
List of converted song URLs from the playlist.
63+
64+
Notes
65+
-----
66+
Uses the 'track-list' section from Apple Music's internal serialized
67+
server data to extract song action URLs.
68+
"""
3269
result = []
3370
headers = {
3471
"User-Agent": "Mozilla/5.0"
@@ -54,6 +91,28 @@ def playlist_scrape(link="https://music.apple.com/us/playlist/new-music-daily/pl
5491
return result
5592

5693
def search(keyword="sasha sloan"):
94+
"""
95+
Search Apple Music for artists, songs, albums, playlists and videos.
96+
97+
Parameters
98+
----------
99+
keyword : str, optional
100+
Search query to send to Apple Music. Defaults to "sasha sloan".
101+
102+
Returns
103+
-------
104+
dict
105+
Structured JSON-like dictionary containing search results:
106+
- artists
107+
- albums
108+
- songs
109+
- playlists
110+
- videos
111+
112+
Notes
113+
-----
114+
Scrapes `serialized-server-data` to access Apple Music's internal search structure.
115+
"""
57116
result = {
58117
'artists':[],
59118
'albums':[],
@@ -194,6 +253,30 @@ def search(keyword="sasha sloan"):
194253
return result
195254

196255
def song_scrape(url="https://music.apple.com/us/song/california/1821538031"):
256+
"""
257+
Scrape a single Apple Music song page and extract metadata.
258+
259+
Parameters
260+
----------
261+
url : str, optional
262+
URL of the Apple Music song. Defaults to sample link.
263+
264+
Returns
265+
-------
266+
dict
267+
Dictionary containing:
268+
- title
269+
- image (full resolution)
270+
- kind (song type)
271+
- album info (title + URL)
272+
- artist info (title + URL)
273+
- preview-url
274+
- list of more songs
275+
276+
Notes
277+
-----
278+
Uses the `schema:song` JSON-LD tag to extract preview URL.
279+
"""
197280
result = {
198281
'title':'',
199282
'image':'',
@@ -249,6 +332,37 @@ def song_scrape(url="https://music.apple.com/us/song/california/1821538031"):
249332
return result
250333

251334
def album_scrape(url="https://music.apple.com/us/album/1965/1817707266?i=1817707585"):
335+
"""
336+
Scrape an Apple Music album page and extract metadata, songs, related albums, videos, etc.
337+
338+
Parameters
339+
----------
340+
url : str, optional
341+
URL of the Apple Music album. Defaults to example album.
342+
343+
Returns
344+
-------
345+
dict
346+
Dictionary containing:
347+
- title
348+
- image
349+
- caption/description
350+
- artist info
351+
- song URLs
352+
- album info text
353+
- more songs (same artist)
354+
- similar (recommended) albums
355+
- videos related to the album
356+
357+
Notes
358+
-----
359+
Extracts multiple sections such as:
360+
- album-detail
361+
- track-list
362+
- similar albums
363+
- more by artist
364+
- album videos
365+
"""
252366
result = {
253367
'title':'',
254368
'image':'',
@@ -346,6 +460,30 @@ def album_scrape(url="https://music.apple.com/us/album/1965/1817707266?i=1817707
346460
return result
347461

348462
def video_scrape(url="https://music.apple.com/us/music-video/gucci-mane-visualizer/1810547026"):
463+
"""
464+
Scrape Apple Music music-video page and extract metadata + video file URL.
465+
466+
Parameters
467+
----------
468+
url : str, optional
469+
URL of the Apple Music music-video. Defaults to example.
470+
471+
Returns
472+
-------
473+
dict
474+
{
475+
title,
476+
image,
477+
artist: {title, url},
478+
video-url,
479+
more (same artist),
480+
similar (same genre)
481+
}
482+
483+
Notes
484+
-----
485+
Uses JSON-LD block `schema:music-video` to extract the direct video content URL.
486+
"""
349487
result = {
350488
'title': '',
351489
'image': '',
@@ -418,6 +556,38 @@ def video_scrape(url="https://music.apple.com/us/music-video/gucci-mane-visualiz
418556
return result
419557

420558
def artist_scrape(url="https://music.apple.com/us/artist/king-princess/1349968534"):
559+
"""
560+
Scrape an Apple Music artist page and extract all available metadata.
561+
562+
Parameters
563+
----------
564+
url : str, optional
565+
Apple Music artist page URL. Defaults to King Princess sample link.
566+
567+
Returns
568+
-------
569+
dict
570+
Dictionary containing:
571+
- title
572+
- image
573+
- latest release URL
574+
- list of top songs
575+
- all albums
576+
- singles & EPs
577+
- playlists
578+
- videos
579+
- similar artists
580+
- appears on
581+
- more-to-see (videos)
582+
- more-to-hear (songs)
583+
- about text
584+
- extra info (bio subtitle)
585+
586+
Notes
587+
-----
588+
This is the most complex scraper and extracts ~12 different sections
589+
from the artist page.
590+
"""
421591
result = {
422592
'title':'',
423593
'image':'',
@@ -552,6 +722,3 @@ def artist_scrape(url="https://music.apple.com/us/artist/king-princess/134996853
552722
pass
553723

554724
return result
555-
556-
557-
print(search())

0 commit comments

Comments
 (0)