Skip to content

Commit 856d0b3

Browse files
committed
change code style for ai checks
1 parent a44bd62 commit 856d0b3

File tree

2 files changed

+127
-43
lines changed

2 files changed

+127
-43
lines changed

Apple-Music-Scraper/main.py

Lines changed: 13 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,8 @@
11
from bs4 import BeautifulSoup
22
import requests
33
import json
4-
from utils import convert_album_to_song_url, get_cover, get_all_singles
5-
6-
7-
def safe_action_url(item):
8-
try:
9-
# segue-based URLs (most items)
10-
return item["segue"]["actionMetrics"]["data"][0]["fields"]["actionUrl"]
11-
except Exception:
12-
pass
13-
14-
try:
15-
# fallback: plain contentDescriptor
16-
return item["contentDescriptor"]["url"]
17-
except Exception:
18-
return None
19-
20-
21-
def find_section(sections, key):
22-
for sec in sections:
23-
if key in sec.get("id", ""):
24-
return sec
25-
return None
26-
27-
28-
def append_urls_from_section(section, target_list):
29-
if not section:
30-
return
31-
for it in section.get("items", []):
32-
url = safe_action_url(it)
33-
if url:
34-
target_list.append(url)
4+
from utils import convert_album_to_song_url, get_cover
5+
from utils import safe_action_url, find_section, append_urls_from_section
356

367

378
def room_scrape(link="https://music.apple.com/us/room/6748797380"):
@@ -697,7 +668,6 @@ def video_scrape(
697668
item = (music_video_header or {}).get("items", [{}])[0]
698669
result["title"] = item.get("title", "")
699670

700-
701671
# IMAGE
702672
try:
703673
artwork = item.get("artwork", {}).get("dictionary", {})
@@ -824,16 +794,16 @@ def artist_scrape(url="https://music.apple.com/us/artist/king-princess/134996853
824794
except (KeyError, IndexError, json.JSONDecodeError):
825795
return result
826796

827-
artist_detail = find_section(sections, "artist-detail-header-section")
828-
latest_and_top = find_section(sections, "latest-release-and-top-songs")
829-
albums = find_section(sections, "full-albums")
830-
playlists = find_section(sections, "playlists")
831-
videos = find_section(sections, "music-videos")
832-
appears_on = find_section(sections, "appears-on")
833-
more_to_see = find_section(sections, "more-to-see")
834-
more_to_hear = find_section(sections, "more-to-hear")
835-
bio = find_section(sections, "artist-bio")
836-
similar = find_section(sections, "similar-artists")
797+
artist_detail = find_section(sections, "artist-detail-header-section")
798+
latest_and_top = find_section(sections, "latest-release-and-top-songs")
799+
albums = find_section(sections, "full-albums")
800+
playlists = find_section(sections, "playlists")
801+
videos = find_section(sections, "music-videos")
802+
appears_on = find_section(sections, "appears-on")
803+
more_to_see = find_section(sections, "more-to-see")
804+
more_to_hear = find_section(sections, "more-to-hear")
805+
bio = find_section(sections, "artist-bio")
806+
similar = find_section(sections, "similar-artists")
837807

838808
# HEADER
839809
try:
@@ -987,4 +957,4 @@ def test_all_functions():
987957
print("\n=== ALL TESTS COMPLETED ===")
988958

989959

990-
test_all_functions()
960+
# test_all_functions()

Apple-Music-Scraper/utils.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,117 @@ def get_all_singles(url="https://music.apple.com/us/artist/king-princess/1349968
156156
continue
157157

158158
return result
159+
160+
161+
def safe_action_url(item):
162+
"""
163+
Safely extract an Apple Music "actionUrl" from a section item.
164+
165+
This function attempts to extract a playable or navigational URL from
166+
Apple Music's internal JSON structure. It first looks for URLs provided
167+
via `segue -> actionMetrics`, which is the most common structure. If that
168+
fails, it falls back to the `contentDescriptor` URL when available.
169+
170+
Parameters
171+
----------
172+
item : dict
173+
A dictionary representing an Apple Music content item inside a section.
174+
175+
Returns
176+
-------
177+
str or None
178+
The extracted URL if available, otherwise None.
179+
180+
Notes
181+
-----
182+
This helper prevents repetitive try/except blocks throughout all scraper
183+
functions and gracefully handles missing keys, unexpected formats, or
184+
incomplete items.
185+
"""
186+
try:
187+
# segue-based URLs (most items)
188+
return item["segue"]["actionMetrics"]["data"][0]["fields"]["actionUrl"]
189+
except Exception:
190+
pass
191+
192+
try:
193+
# fallback: plain contentDescriptor
194+
return item["contentDescriptor"]["url"]
195+
except Exception:
196+
return None
197+
198+
199+
def find_section(sections, key):
200+
"""
201+
Locate a specific Apple Music section by matching a substring in its ID.
202+
203+
This utility searches through the list of sections extracted from
204+
Apple Music's `serialized-server-data` and returns the first section
205+
whose "id" field contains the provided key substring.
206+
207+
Parameters
208+
----------
209+
sections : list[dict]
210+
List of section dictionaries parsed from Apple Music page data.
211+
key : str
212+
Substring to search for inside the section ID.
213+
214+
Returns
215+
-------
216+
dict or None
217+
The matching section dictionary if found, otherwise None.
218+
219+
Notes
220+
-----
221+
Apple Music uses structured section IDs such as:
222+
- "artist-detail-header-section"
223+
- "track-list"
224+
- "music-videos"
225+
- "similar-artists"
226+
This function simplifies section lookup and reduces repeated loops and
227+
conditional chains in scraper functions.
228+
"""
229+
for sec in sections:
230+
if key in sec.get("id", ""):
231+
return sec
232+
return None
233+
234+
235+
def append_urls_from_section(section, target_list):
236+
"""
237+
Extract URLs from a section and append them to a target list.
238+
239+
This helper iterates through all items inside a given Apple Music
240+
section, uses `safe_action_url()` to safely extract their URLs,
241+
and appends each valid URL to the provided list.
242+
243+
Parameters
244+
----------
245+
section : dict or None
246+
The section dictionary containing an "items" list. If None, the
247+
function does nothing.
248+
target_list : list
249+
The list to which valid extracted URLs will be appended.
250+
251+
Returns
252+
-------
253+
None
254+
This function modifies target_list in-place.
255+
256+
Notes
257+
-----
258+
Many Apple Music sections such as:
259+
- top songs
260+
- albums
261+
- playlists
262+
- videos
263+
- similar artists
264+
share the same internal structure. This helper removes code duplication
265+
and ensures unified URL extraction behavior.
266+
"""
267+
if not section:
268+
return
269+
for it in section.get("items", []):
270+
url = safe_action_url(it)
271+
if url:
272+
target_list.append(url)

0 commit comments

Comments
 (0)