Skip to content

Commit

Permalink
Merge pull request #34 from itouakirai/main
Browse files Browse the repository at this point in the history
add artist-folder-format tag {ArtistName}/{UrlArtistName}
  • Loading branch information
zhaarey authored Sep 19, 2024
2 parents eade99d + fa3295a commit 1edcb1d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
2 changes: 1 addition & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ playlist-folder-format: "{PlaylistName}"
#{SongId} {SongNumer} {SongName} {DiscNumber} {TrackNumber} {Quality} {Codec} {Tag}
#example: Disk {DiscNumber} - Track {TrackNumber} {SongName} [{Quality}]{{Tag}}"
song-file-format: "{SongNumer}. {SongName}"
#{ArtistId} {ArtistName}
#{ArtistId} {ArtistName}/{UrlArtistName}
#if artist-folder-format set "",will not make artist folder
artist-folder-format: "{ArtistName}"
#if set "" will not add tag
Expand Down
44 changes: 40 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,30 @@ func checkUrlArtist(url string) (string, string) {
return matches[0][1], matches[0][2]
}
}
func getUrlArtistName(artistUrl string, token string) (string, error) {
storefront, artistId := checkUrlArtist(artistUrl)
req, err := http.NewRequest("GET", fmt.Sprintf("https://amp-api.music.apple.com/v1/catalog/%s/artists/%s", storefront, artistId), nil)
if err != nil {
return "", err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
req.Header.Set("Origin", "https://music.apple.com")
do, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer do.Body.Close()
if do.StatusCode != http.StatusOK {
return "", errors.New(do.Status)
}
obj := new(AutoGeneratedArtist)
err = json.NewDecoder(do.Body).Decode(&obj)
if err != nil {
return "", err
}
return obj.Data[0].Attributes.Name, nil
}

func checkArtist(artistUrl string, token string) ([]string, error) {
storefront, artistId := checkUrlArtist(artistUrl)
Expand Down Expand Up @@ -1408,14 +1432,17 @@ func rip(albumId string, token string, storefront string, userToken string) erro
singerFoldername = strings.NewReplacer(
"{ArtistName}", "Apple Music",
"{ArtistId}", "",
"{UrlArtistName}", "Apple Music",
).Replace(config.ArtistFolderFormat)
} else if len(meta.Data[0].Relationships.Artists.Data) > 0 {
singerFoldername = strings.NewReplacer(
"{UrlArtistName}", LimitString(meta.Data[0].Attributes.ArtistName),
"{ArtistName}", LimitString(meta.Data[0].Attributes.ArtistName),
"{ArtistId}", meta.Data[0].Relationships.Artists.Data[0].ID,
).Replace(config.ArtistFolderFormat)
} else {
singerFoldername = strings.NewReplacer(
"{UrlArtistName}", LimitString(meta.Data[0].Attributes.ArtistName),
"{ArtistName}", LimitString(meta.Data[0].Attributes.ArtistName),
"{ArtistId}", "",
).Replace(config.ArtistFolderFormat)
Expand Down Expand Up @@ -1613,12 +1640,12 @@ func rip(albumId string, token string, storefront string, userToken string) erro
fmt.Printf("Track %d of %d:\n", trackNum, trackTotal)
manifest, err := getInfoFromAdam(track.ID, token, storefront)
if err != nil {
fmt.Println("Failed to get manifest.\n", err)
fmt.Println("\u26A0 Failed to get manifest:", err)
counter.NotSong++
continue
}
if manifest.Attributes.ExtendedAssetUrls.EnhancedHls == "" {
fmt.Println("Unavailable.")
fmt.Println("\u26A0 Unavailable.")
counter.Unavailable++
continue
}
Expand Down Expand Up @@ -1731,8 +1758,8 @@ func rip(albumId string, token string, storefront string, userToken string) erro

trackUrl, keys, err := extractMedia(manifest.Attributes.ExtendedAssetUrls.EnhancedHls)
if err != nil {
fmt.Println("Failed to extract info from manifest.\n", err)
counter.Error++
fmt.Println("\u26A0 Failed to extract info from manifest:", err)
counter.Unavailable++
continue
}
info, err := extractSong(trackUrl)
Expand Down Expand Up @@ -1894,6 +1921,15 @@ func main() {
}
os.Args = args
if strings.Contains(os.Args[0], "/artist/") {
urlArtistName, err := getUrlArtistName(os.Args[0], token)
if err != nil {
fmt.Println("Failed to get artistname.")
return
}
//fmt.Println("get artistname:", urlArtistName)
config.ArtistFolderFormat = strings.NewReplacer(
"{UrlArtistName}", LimitString(urlArtistName),
).Replace(config.ArtistFolderFormat)
newArgs, err := checkArtist(os.Args[0], token)
if err != nil {
fmt.Println("Failed to get artist.")
Expand Down

0 comments on commit 1edcb1d

Please sign in to comment.