Skip to content

Commit

Permalink
Merge pull request #21 from itouakirai/main
Browse files Browse the repository at this point in the history
add artist cover download
  • Loading branch information
zhaarey authored Jun 29, 2024
2 parents 3052984 + 1e963f1 commit 726a21f
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 3 deletions.
1 change: 1 addition & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
media-user-token: "your-media-user-token"
embed-lrc: true
save-lrc-file: false
save-artist-cover: false
save-animated-artwork: false # If enabled, requires ffmpeg
emby-animated-artwork: false # If enabled, requires ffmpeg
embed-cover: true
Expand Down
14 changes: 13 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Config struct {
EmbyAnimatedArtwork bool `yaml:"emby-animated-artwork"`
EmbedLrc bool `yaml:"embed-lrc"`
EmbedCover bool `yaml:"embed-cover"`
SaveArtistCover bool `yaml:"save-artist-cover"`
CoverSize string `yaml:"cover-size"`
CoverFormat string `yaml:"cover-format"`
AlacSaveFolder string `yaml:"alac-save-folder"`
Expand Down Expand Up @@ -1154,7 +1155,7 @@ func getMeta(albumId string, token string, storefront string) (*AutoGenerated, e
query.Set("omit[resource]", "autos")
query.Set("include", "tracks,artists,record-labels")
query.Set("include[songs]", "artists")
query.Set("fields[artists]", "name")
query.Set("fields[artists]", "name,artwork")
query.Set("fields[albums:albums]", "artistName,artwork,name,releaseDate,url")
query.Set("fields[record-labels]", "name")
query.Set("extend", "editorialVideo")
Expand Down Expand Up @@ -1390,6 +1391,14 @@ func rip(albumId string, token string, storefront string, userToken string) erro
sanAlbumFolder := filepath.Join(singerFolder, forbiddenNames.ReplaceAllString(albumFolder, "_"))
os.MkdirAll(sanAlbumFolder, os.ModePerm)
fmt.Println(albumFolder)
//get artist cover
if config.SaveArtistCover && !(strings.Contains(albumId, "pl.")) {
err = writeCover(singerFolder, "folder", meta.Data[0].Relationships.Artists.Data[0].Attributes.Artwork.Url)
if err != nil {
fmt.Println("Failed to write artist cover.")
}
}
//get album cover
err = writeCover(sanAlbumFolder, "cover", meta.Data[0].Attributes.Artwork.URL)
if err != nil {
fmt.Println("Failed to write cover.")
Expand Down Expand Up @@ -2402,6 +2411,9 @@ type AutoGenerated struct {
Href string `json:"href"`
Attributes struct {
Name string `json:"name"`
Artwork struct {
Url string `json:"url"`
} `json:"artwork"`
} `json:"attributes"`
} `json:"data"`
} `json:"artists"`
Expand Down
95 changes: 94 additions & 1 deletion main_atmos.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ var (
type Config struct {
MediaUserToken string `yaml:"media-user-token"`
SaveLrcFile bool `yaml:"save-lrc-file"`
SaveAnimatedArtwork bool `yaml:"save-animated-artwork"`
EmbyAnimatedArtwork bool `yaml:"emby-animated-artwork"`
EmbedLrc bool `yaml:"embed-lrc"`
EmbedCover bool `yaml:"embed-cover"`
SaveArtistCover bool `yaml:"save-artist-cover"`
CoverSize string `yaml:"cover-size"`
CoverFormat string `yaml:"cover-format"`
AlacSaveFolder string `yaml:"alac-save-folder"`
Expand Down Expand Up @@ -1098,9 +1101,10 @@ func getMeta(albumId string, token string, storefront string) (*AutoGenerated, e
query.Set("omit[resource]", "autos")
query.Set("include", "tracks,artists,record-labels")
query.Set("include[songs]", "artists")
query.Set("fields[artists]", "name")
query.Set("fields[artists]", "name,artwork")
query.Set("fields[albums:albums]", "artistName,artwork,name,releaseDate,url")
query.Set("fields[record-labels]", "name")
query.Set("extend", "editorialVideo")
// query.Set("l", "en-gb")
req.URL.RawQuery = query.Encode()
do, err := http.DefaultClient.Do(req)
Expand Down Expand Up @@ -1315,10 +1319,48 @@ func rip(albumId string, token string, storefront string, userToken string) erro
sanAlbumFolder := filepath.Join(singerFolder, forbiddenNames.ReplaceAllString(albumFolder, "_"))
os.MkdirAll(sanAlbumFolder, os.ModePerm)
fmt.Println(albumFolder)
//get artist cover
if config.SaveArtistCover && !(strings.Contains(albumId, "pl.")) {
err = writeCover(singerFolder, "folder", meta.Data[0].Relationships.Artists.Data[0].Attributes.Artwork.Url)
if err != nil {
fmt.Println("Failed to write artist cover.")
}
}
//get album cover
err = writeCover(sanAlbumFolder, "cover", meta.Data[0].Attributes.Artwork.URL)
if err != nil {
fmt.Println("Failed to write cover.")
}
//get animated artwork
if config.SaveAnimatedArtwork && meta.Data[0].Attributes.EditorialVideo.MotionDetailSquare.Video != "" {
fmt.Println("Found Animation Artwork.")
motionvideoUrl, err := extractVideo(meta.Data[0].Attributes.EditorialVideo.MotionDetailSquare.Video)
if err != nil {
fmt.Println("no motion video.\n", err)
}
exists, err := fileExists(filepath.Join(sanAlbumFolder, "animated_artwork.mp4"))
if err != nil {
fmt.Println("Failed to check if animated artwork exists.")
}
if exists {
fmt.Println("Animated artwork already exists locally.")
} else {
fmt.Println("Animation Artwork Downloading...")
cmd := exec.Command("ffmpeg", "-loglevel", "quiet", "-y", "-i", motionvideoUrl, "-c", "copy", filepath.Join(sanAlbumFolder, "animated_artwork.mp4"))
if err := cmd.Run(); err != nil {
fmt.Printf("animated artwork dl err: %v\n", err)
} else {
fmt.Println("Animation Artwork Downloaded")
}
if config.EmbyAnimatedArtwork {
cmd2 := exec.Command("ffmpeg", "-i", filepath.Join(sanAlbumFolder, "animated_artwork.mp4"), "-vf", "scale=440:-1", "-r", "24", "-f", "gif", filepath.Join(sanAlbumFolder, "folder.jpg"))
if err := cmd2.Run(); err != nil {
fmt.Printf("animated artwork to gif err: %v\n", err)
}
}

}
}
trackTotal := len(meta.Data[0].Relationships.Tracks.Data)
for trackNum, track := range meta.Data[0].Relationships.Tracks.Data {
trackNum++
Expand Down Expand Up @@ -1651,6 +1693,46 @@ func extractMedia(b string) (string, []string, error) {
return streamUrl.String(), keys, nil
}

func extractVideo(c string) (string, error) {
MediaUrl, err := url.Parse(c)
if err != nil {
return "", err
}
resp, err := http.Get(c)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", errors.New(resp.Status)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
videoString := string(body)
from, listType, err := m3u8.DecodeFrom(strings.NewReader(videoString), true)
if err != nil || listType != m3u8.MASTER {
return "", errors.New("m3u8 not of media type")
}
video := from.(*m3u8.MasterPlaylist)
var streamUrl *url.URL
sort.Slice(video.Variants, func(i, j int) bool {
return video.Variants[i].AverageBandwidth > video.Variants[j].AverageBandwidth
})
if len(video.Variants) > 0 {
highestBandwidthVariant := video.Variants[0]
streamUrl, err = MediaUrl.Parse(highestBandwidthVariant.URI)
if err != nil {
return "", err
}
}
if streamUrl == nil {
return "", errors.New("no video codec found")
}
return streamUrl.String(), nil
}

func extractSong(url string) (*SongInfo, error) {
fmt.Println("Downloading...")
track, err := http.Get(url)
Expand Down Expand Up @@ -2101,6 +2183,14 @@ type AutoGenerated struct {
Kind string `json:"kind"`
} `json:"playParams"`
IsCompilation bool `json:"isCompilation"`
EditorialVideo struct {
MotionDetailSquare struct {
Video string `json:"video"`
} `json:"motionDetailSquare"`
MotionSquareVideo1x1 struct {
Video string `json:"video"`
} `json:"motionSquareVideo1x1"`
} `json:"editorialVideo"`
} `json:"attributes"`
Relationships struct {
RecordLabels struct {
Expand All @@ -2115,6 +2205,9 @@ type AutoGenerated struct {
Href string `json:"href"`
Attributes struct {
Name string `json:"name"`
Artwork struct {
Url string `json:"url"`
} `json:"artwork"`
} `json:"attributes"`
} `json:"data"`
} `json:"artists"`
Expand Down
94 changes: 93 additions & 1 deletion main_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ var (
type Config struct {
MediaUserToken string `yaml:"media-user-token"`
SaveLrcFile bool `yaml:"save-lrc-file"`
SaveAnimatedArtwork bool `yaml:"save-animated-artwork"`
EmbyAnimatedArtwork bool `yaml:"emby-animated-artwork"`
EmbedLrc bool `yaml:"embed-lrc"`
EmbedCover bool `yaml:"embed-cover"`
SaveArtistCover bool `yaml:"save-artist-cover"`
CoverSize string `yaml:"cover-size"`
CoverFormat string `yaml:"cover-format"`
AlacSaveFolder string `yaml:"alac-save-folder"`
Expand Down Expand Up @@ -1033,9 +1036,10 @@ func getMeta(albumId string, token string, storefront string) (*AutoGenerated, e
query.Set("omit[resource]", "autos")
query.Set("include", "tracks,artists,record-labels")
query.Set("include[songs]", "artists")
query.Set("fields[artists]", "name")
query.Set("fields[artists]", "name,artwork")
query.Set("fields[albums:albums]", "artistName,artwork,name,releaseDate,url")
query.Set("fields[record-labels]", "name")
query.Set("extend", "editorialVideo")
// query.Set("l", "en-gb")
req.URL.RawQuery = query.Encode()
do, err := http.DefaultClient.Do(req)
Expand Down Expand Up @@ -1273,10 +1277,48 @@ func rip(albumId string, token string, storefront string, userToken string) erro
sanAlbumFolder := filepath.Join(singerFolder, forbiddenNames.ReplaceAllString(albumFolder, "_"))
os.MkdirAll(sanAlbumFolder, os.ModePerm)
fmt.Println(albumFolder)
//get artist cover
if config.SaveArtistCover && !(strings.Contains(albumId, "pl.")) {
err = writeCover(singerFolder, "folder", meta.Data[0].Relationships.Artists.Data[0].Attributes.Artwork.Url)
if err != nil {
fmt.Println("Failed to write artist cover.")
}
}
//get album cover
err = writeCover(sanAlbumFolder, "cover", meta.Data[0].Attributes.Artwork.URL)
if err != nil {
fmt.Println("Failed to write cover.")
}
//get animated artwork
if config.SaveAnimatedArtwork && meta.Data[0].Attributes.EditorialVideo.MotionDetailSquare.Video != "" {
fmt.Println("Found Animation Artwork.")
motionvideoUrl, err := extractVideo(meta.Data[0].Attributes.EditorialVideo.MotionDetailSquare.Video)
if err != nil {
fmt.Println("no motion video.\n", err)
}
exists, err := fileExists(filepath.Join(sanAlbumFolder, "animated_artwork.mp4"))
if err != nil {
fmt.Println("Failed to check if animated artwork exists.")
}
if exists {
fmt.Println("Animated artwork already exists locally.")
} else {
fmt.Println("Animation Artwork Downloading...")
cmd := exec.Command("ffmpeg", "-loglevel", "quiet", "-y", "-i", motionvideoUrl, "-c", "copy", filepath.Join(sanAlbumFolder, "animated_artwork.mp4"))
if err := cmd.Run(); err != nil {
fmt.Printf("animated artwork dl err: %v\n", err)
} else {
fmt.Println("Animation Artwork Downloaded")
}
if config.EmbyAnimatedArtwork {
cmd2 := exec.Command("ffmpeg", "-i", filepath.Join(sanAlbumFolder, "animated_artwork.mp4"), "-vf", "scale=440:-1", "-r", "24", "-f", "gif", filepath.Join(sanAlbumFolder, "folder.jpg"))
if err := cmd2.Run(); err != nil {
fmt.Printf("animated artwork to gif err: %v\n", err)
}
}

}
}
trackTotal := len(meta.Data[0].Relationships.Tracks.Data)
arr := make([]int, trackTotal)
for i := 0; i < trackTotal; i++ {
Expand Down Expand Up @@ -1687,6 +1729,45 @@ func extractMedia(b string) (string, []string, error) {
}
return streamUrl.String(), keys, nil
}
func extractVideo(c string) (string, error) {
MediaUrl, err := url.Parse(c)
if err != nil {
return "", err
}
resp, err := http.Get(c)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", errors.New(resp.Status)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
videoString := string(body)
from, listType, err := m3u8.DecodeFrom(strings.NewReader(videoString), true)
if err != nil || listType != m3u8.MASTER {
return "", errors.New("m3u8 not of media type")
}
video := from.(*m3u8.MasterPlaylist)
var streamUrl *url.URL
sort.Slice(video.Variants, func(i, j int) bool {
return video.Variants[i].AverageBandwidth > video.Variants[j].AverageBandwidth
})
if len(video.Variants) > 0 {
highestBandwidthVariant := video.Variants[0]
streamUrl, err = MediaUrl.Parse(highestBandwidthVariant.URI)
if err != nil {
return "", err
}
}
if streamUrl == nil {
return "", errors.New("no video codec found")
}
return streamUrl.String(), nil
}

func extractSong(url string) (*SongInfo, error) {
fmt.Println("Downloading...")
Expand Down Expand Up @@ -2137,6 +2218,14 @@ type AutoGenerated struct {
Kind string `json:"kind"`
} `json:"playParams"`
IsCompilation bool `json:"isCompilation"`
EditorialVideo struct {
MotionDetailSquare struct {
Video string `json:"video"`
} `json:"motionDetailSquare"`
MotionSquareVideo1x1 struct {
Video string `json:"video"`
} `json:"motionSquareVideo1x1"`
} `json:"editorialVideo"`
} `json:"attributes"`
Relationships struct {
RecordLabels struct {
Expand All @@ -2151,6 +2240,9 @@ type AutoGenerated struct {
Href string `json:"href"`
Attributes struct {
Name string `json:"name"`
Artwork struct {
Url string `json:"url"`
} `json:"artwork"`
} `json:"attributes"`
} `json:"data"`
} `json:"artists"`
Expand Down

0 comments on commit 726a21f

Please sign in to comment.