67 lines
1.5 KiB
Go
67 lines
1.5 KiB
Go
package netease
|
|
|
|
import (
|
|
"errors"
|
|
netURL "net/url"
|
|
"strings"
|
|
|
|
"github.com/iawia002/annie/downloader"
|
|
"github.com/iawia002/annie/extractors"
|
|
"github.com/iawia002/annie/request"
|
|
"github.com/iawia002/annie/utils"
|
|
)
|
|
|
|
// Extract is the main function for extracting data
|
|
func Extract(url string) ([]downloader.Data, error) {
|
|
url = strings.Replace(url, "/#/", "/", 1)
|
|
vid := utils.MatchOneOf(url, `/(mv|video)\?id=(\w+)`)
|
|
if vid == nil {
|
|
return nil, errors.New("invalid url for netease music")
|
|
}
|
|
|
|
html, err := request.Get(url, url, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if strings.Contains(html, "u-errlg-404") {
|
|
return nil, errors.New("404 music not found")
|
|
}
|
|
|
|
titles := utils.MatchOneOf(html, `<meta property="og:title" content="(.+?)" />`)
|
|
if titles == nil || len(titles) < 2 {
|
|
return nil, extractors.ErrURLParseFailed
|
|
}
|
|
title := titles[1]
|
|
|
|
realURLs := utils.MatchOneOf(html, `<meta property="og:video" content="(.+?)" />`)
|
|
if realURLs == nil || len(realURLs) < 2 {
|
|
return nil, extractors.ErrURLParseFailed
|
|
}
|
|
realURL, _ := netURL.QueryUnescape(realURLs[1])
|
|
|
|
size, err := request.Size(realURL, url)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
urlData := downloader.URL{
|
|
URL: realURL,
|
|
Size: size,
|
|
Ext: "mp4",
|
|
}
|
|
streams := map[string]downloader.Stream{
|
|
"default": {
|
|
URLs: []downloader.URL{urlData},
|
|
Size: size,
|
|
},
|
|
}
|
|
return []downloader.Data{
|
|
{
|
|
Site: "网易云音乐 music.163.com",
|
|
Title: title,
|
|
Type: "video",
|
|
Streams: streams,
|
|
URL: url,
|
|
},
|
|
}, nil
|
|
}
|