imthaghost / scdl

SoundCloud music downloader 🎶
MIT License
61 stars 10 forks source link

panic: runtime error: invalid memory address or nil pointer dereference #2

Closed nourserry closed 3 years ago

nourserry commented 3 years ago

Ran into this when I tried to download a track Probably due to the / symbol.

'/home/photon/go/bin/scdl' https://soundcloud.com/otteswed/aatrkdjjg5dq
[+] Parse m3u8 file succed 
[-] Open file failed: open أيمن موسى // العقابية /نظرة اولى/حفل الاستقبال.mp3: no such file or directory
[+] Total 38 files to download 
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x74f025]

goroutine 1 [running]:
github.com/imthaghost/scdl/joiner.(*Joiner).Run(0x0, 0x26, 0x87e430, 0x1f)
    /home/photon/go/src/github.com/imthaghost/scdl/joiner/join.go:48 +0x45
github.com/imthaghost/scdl/mp3.Merge(0xc0001ff080, 0x2af, 0xc000128000, 0x54)
    /home/photon/go/src/github.com/imthaghost/scdl/mp3/merge.go:241 +0x75c
github.com/imthaghost/scdl/soundcloud.ExtractSong(0x7ffd2402c2a8, 0x2c)
    /home/photon/go/src/github.com/imthaghost/scdl/soundcloud/song.go:80 +0x4b3
github.com/imthaghost/scdl/cmd.scdl(0xc000098e80, 0x1, 0x1)
    /home/photon/go/src/github.com/imthaghost/scdl/cmd/base.go:17 +0x65
github.com/imthaghost/scdl/cmd.glob..func1(0xb40f00, 0xc000098e80, 0x1, 0x1)
    /home/photon/go/src/github.com/imthaghost/scdl/cmd/root.go:31 +0xac
github.com/spf13/cobra.(*Command).execute(0xb40f00, 0xc0000a8030, 0x1, 0x1, 0xb40f00, 0xc0000a8030)
    /home/photon/go/pkg/mod/github.com/spf13/cobra@v1.0.0/command.go:846 +0x2c2
github.com/spf13/cobra.(*Command).ExecuteC(0xb40f00, 0xb8d14a, 0x870463, 0x6)
    /home/photon/go/pkg/mod/github.com/spf13/cobra@v1.0.0/command.go:950 +0x375
github.com/spf13/cobra.(*Command).Execute(...)
    /home/photon/go/pkg/mod/github.com/spf13/cobra@v1.0.0/command.go:887
github.com/imthaghost/scdl/cmd.Execute()
    /home/photon/go/src/github.com/imthaghost/scdl/cmd/root.go:43 +0x9f
main.main()
    /home/photon/go/src/github.com/imthaghost/scdl/main.go:6 +0x25
imthaghost commented 3 years ago

Hey @nourserry thanks for opening this issue. I've actually been meaning to fix the file name parsing but haven't gotten around to it. There is currently no sanitization for special characters which means the filename won't parse properly and cause the error "open file failed". I have made a quick fix which doesn't really solve all edge cases but will solve this for your specific problem. If you want to write some tests and solve more edge cases and contribute go look at the file title.go in the soundcloud package. I'm going to keep this issue open for anyone else who comes across more edge cases with file name parsing.

// GetTitle returns title of the song
// TODO: implement tests
func GetTitle(data []byte) string {
    var title string
    r := bytes.NewReader(data)
    doc, err := goquery.NewDocumentFromReader(r)
    if err != nil {
        panic(err)
    }
    // the title of the song can be found in the meta tag from initial response
    doc.Find("meta[property='twitter:title']").Each(func(i int, s *goquery.Selection) {
        // get the data from found element's content attribute
        data, exists := s.Attr("content")
        if exists {
            title = data

        }
    })
    title = strings.ReplaceAll(title, "/", "")
    fmt.Println(title)
    return title
}

Screen Shot 2020-09-19 at 1 32 16 AM

nourserry commented 3 years ago

Hi Thanks for the quick fix, I add fixes for other character that may cause errors on Windows. I'm no coder, so this is probably not the best way to write it, but I tested it on Linux and it worked.

imthaghost commented 3 years ago

Closing since you have tested on other other operating systems.

Reference: PR #3