11from bs4 import BeautifulSoup
22import requests
33import 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
378def 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()
0 commit comments