Skip to content

Commit

Permalink
Added tags/metadata to the track after downloading.
Browse files Browse the repository at this point in the history
- Set the tags `(Artwork image, artist id, createdAt date, genre)`
  • Loading branch information
AYehia0 committed Jan 13, 2023
1 parent a86f5b6 commit 0dd0311
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 3 deletions.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ go 1.19

require (
github.com/PuerkitoBio/goquery v1.8.0
github.com/bogem/id3v2 v1.2.0
github.com/fatih/color v1.13.0
github.com/grafov/m3u8 v0.11.1
github.com/manifoldco/promptui v0.9.0
github.com/schollz/progressbar/v3 v3.13.0
github.com/spf13/cobra v1.6.1
)
Expand All @@ -14,7 +16,6 @@ require (
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/manifoldco/promptui v0.9.0 // indirect
github.com/mattn/go-colorable v0.1.9 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
Expand All @@ -24,4 +25,5 @@ require (
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8 // indirect
golang.org/x/sys v0.4.0 // indirect
golang.org/x/term v0.4.0 // indirect
golang.org/x/text v0.6.0 // indirect
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ github.com/PuerkitoBio/goquery v1.8.0 h1:PJTF7AmFCFKk1N6V6jmKfrNH9tV5pNE6lZMkG0g
github.com/PuerkitoBio/goquery v1.8.0/go.mod h1:ypIiRMtY7COPGk+I/YbZLbxsxn9g5ejnI2HSMtkjZvI=
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
github.com/bogem/id3v2 v1.2.0 h1:hKDF+F1gOgQ5r1QmBCEZUk4MveJbKxCeIDSBU7CQ4oI=
github.com/bogem/id3v2 v1.2.0/go.mod h1:t78PK5AQ56Q47kizpYiV6gtjj3jfxlz87oFpty8DYs8=
github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -58,7 +62,10 @@ golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.4.0 h1:O7UWfv5+A2qiuulQk30kVinPoMtoIPeVaKLEgLpVkvg=
golang.org/x/term v0.4.0/go.mod h1:9P2UbLfCdcvo3p/nzKvsmas4TnlujnuoV9hGgYzW1lQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.6.0 h1:3XmdazWV+ubf7QgHSTWeykHOci5oeekaGJBLkrkaw4k=
golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
11 changes: 9 additions & 2 deletions internal/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ func Sc(args []string, bestQuality bool) {
defaultQuality = getHighestQuality(qualities)
}

soundcloud.Download(chooseTrackDownload(list, defaultQuality), downloadPath)
fmt.Printf("\n%s Track saved to : %s\n", theme.Green("[-]"), theme.Magenta(downloadPath))
track := chooseTrackDownload(list, defaultQuality)
filePath := soundcloud.Download(track, downloadPath)

// add tags
err = soundcloud.AddMetadata(track, filePath)
if err != nil {
fmt.Printf("Error happend while adding tags to the track : %s\n", err)
}
fmt.Printf("\n%s Track saved to : %s\n", theme.Green("[-]"), theme.Magenta(filePath))
}
60 changes: 60 additions & 0 deletions pkg/soundcloud/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// adding tags to the track after downloading it.
package soundcloud

import (
"fmt"
"net/http"
"strings"

"github.com/AYehia0/soundcloud-dl/pkg/client"
"github.com/bogem/id3v2"
)

func AddMetadata(track DownloadTrack, filePath string) error {
t500 := "t500x500" // for getting a higher res img
imgBytes := make([]byte, 0)

// check for artist thing
if track.SoundData.ArtworkUrl != "" {
url := strings.Replace(track.SoundData.ArtworkUrl, "large", t500, 1)

// fetching the data
statusCode, data, err := client.Get(url)
if err != nil || statusCode != http.StatusOK {
return err
}
imgBytes = data
}

tag, err := id3v2.Open(filePath, id3v2.Options{Parse: true})
if err != nil {
return err
}
defer tag.Close()

// setting metadata
tag.SetTitle(track.SoundData.Title)
tag.SetGenre(track.SoundData.Genre)
tag.SetYear(track.SoundData.CreatedAt)

// extracting the usr
artistName := strings.Split(track.SoundData.PermalinkUrl, "/")
tag.SetArtist(artistName[3])

fmt.Println("Img url is : ", imgBytes)
if imgBytes != nil {
tag.AddAttachedPicture(
id3v2.PictureFrame{
Encoding: id3v2.EncodingUTF8,
MimeType: "image/jpeg",
Picture: imgBytes,
Description: track.SoundData.Description, // well, coz why not :D
},
)
}
if err = tag.Save(); err != nil {
return err
}
return nil

}

0 comments on commit 0dd0311

Please sign in to comment.