Skip to content

Commit

Permalink
feat: support proxy setting (#12)
Browse files Browse the repository at this point in the history
* feat: support proxy setting

fix #10

* fix: unit test out of range panic

* perf: compatible files have no errors

* perf: code optimize

Co-authored-by: Liu <liu.chuan@siemens.com>
  • Loading branch information
chuan-T and Liu committed Jan 17, 2023
1 parent ba119f7 commit 2371d55
Show file tree
Hide file tree
Showing 20 changed files with 116 additions and 40 deletions.
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LF
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
golang.org/x/net v0.0.0-20211029224645-99673261e6eb h1:pirldcYWx7rx7kE5r+9WsOXPXK0+WH5+uZ7uPmJ44uM=
golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
Expand All @@ -41,6 +44,7 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
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/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
Expand Down
9 changes: 6 additions & 3 deletions processor/bilibili/bilibili.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bilibili

import (
"encoding/json"
"errors"
"strings"

"github.com/foamzou/audio-get/args"
Expand Down Expand Up @@ -31,7 +32,7 @@ func (c *Core) GetSourceName() string {

func (c *Core) FetchMetaAndResourceInfo() (mediaMeta *meta.MediaMeta, err error) {
if strings.Contains(c.Opts.Url, "b23.tv") {
if redirectUrl, err := utils.GetLocation(c.Opts.Url, map[string]string{
if redirectUrl, err := utils.GetLocation(consts.SourceNameBilibili, c.Opts.Url, map[string]string{
"user-agent": consts.UAAndroid,
}); err == nil {
c.Opts.Url = redirectUrl
Expand Down Expand Up @@ -68,7 +69,9 @@ func (c *Core) FetchMetaAndResourceInfo() (mediaMeta *meta.MediaMeta, err error)
mediaMeta.Title = audioMeta.VideoData.Title
mediaMeta.Description = audioMeta.VideoData.Description
}

if len(resource.Data.Dash.Audios) == 0 {
return nil, errors.New("no audio data")
}
audio := resource.Data.Dash.Audios[0]
mediaMeta.Artist = getSinger(audioMeta)
mediaMeta.Album = Album
Expand Down Expand Up @@ -131,7 +134,7 @@ func getSinger(audioMeta *AudioMeta) string {
}

func fetchHtml(url string) (string, error) {
return utils.HttpGet(url, map[string]string{
return utils.HttpGet(consts.SourceNameBilibili, url, map[string]string{
"user-agent": consts.UAMac,
})
}
6 changes: 3 additions & 3 deletions processor/bilibili/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ const APIFinger = "https://api.bilibili.com/x/frontend/finger/spi"
const SearchPageUrl = "https://search.bilibili.com/all"

func (c *Core) SearchSong() ([]*meta.SearchSongItem, error) {
cookie, err := utils.GetCookie(SearchPageUrl, map[string]string{
cookie, err := utils.GetCookie(consts.SourceNameBilibili, SearchPageUrl, map[string]string{
"User-Agent": consts.UAMac,
"Referer": "https://search.bilibili.com/",
}, false)
if err != nil {
return nil, err
}

fingerJsonStr, err := utils.HttpGet(APIFinger, map[string]string{
fingerJsonStr, err := utils.HttpGet(consts.SourceNameBilibili, APIFinger, map[string]string{
"User-Agent": consts.UAMac,
"Referer": "https://search.bilibili.com/all",
"Cookie": cookie,
Expand All @@ -40,7 +40,7 @@ func (c *Core) SearchSong() ([]*meta.SearchSongItem, error) {

var searchSongItems []*meta.SearchSongItem
api := fmt.Sprintf(APISearch, url.QueryEscape(c.Opts.Search.Keyword))
jsonStr, err := utils.HttpGet(api, map[string]string{
jsonStr, err := utils.HttpGet(consts.SourceNameBilibili, api, map[string]string{
"User-Agent": consts.UAMac,
"Referer": "https://search.bilibili.com/all",
"Cookie": cookie,
Expand Down
4 changes: 2 additions & 2 deletions processor/douyin/douyin.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (c *Core) FetchMetaAndResourceInfo() (mediaMeta *meta.MediaMeta, err error)
redirectUrl = c.Opts.Url

if videoId == "" {
redirectUrl, err = utils.GetLocation(c.Opts.Url, map[string]string{
redirectUrl, err = utils.GetLocation(consts.SourceNameDouyin, c.Opts.Url, map[string]string{
"user-agent": consts.UAAndroid,
})

Expand All @@ -50,7 +50,7 @@ func (c *Core) FetchMetaAndResourceInfo() (mediaMeta *meta.MediaMeta, err error)
}
}

metaInfo, err := utils.HttpGet(ApiGetItemInfo+videoId, map[string]string{
metaInfo, err := utils.HttpGet(consts.SourceNameDouyin, ApiGetItemInfo+videoId, map[string]string{
"user-agent": consts.UAAndroid,
"referer": redirectUrl,
})
Expand Down
6 changes: 3 additions & 3 deletions processor/douyin/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ func (c *Core) SearchSong() ([]*meta.SearchSongItem, error) {
apiQuery += "&X-Bogus=" + xb
api := APISearchAPI + apiQuery

cookie, err := utils.GetCookie(searchUrl, map[string]string{
cookie, err := utils.GetCookie(consts.SourceNameDouyin, searchUrl, map[string]string{
"User-Agent": consts.UAMac,
}, false)
if err != nil {
return nil, err
}

cookie, err = utils.GetCookie(searchUrl, map[string]string{
cookie, err = utils.GetCookie(consts.SourceNameDouyin, searchUrl, map[string]string{
"User-Agent": consts.UAMac,
"Referer": searchUrl,
"Cookie": cookie,
Expand All @@ -43,7 +43,7 @@ func (c *Core) SearchSong() ([]*meta.SearchSongItem, error) {
return nil, err
}

jsonStr, err := utils.HttpGet(api, map[string]string{
jsonStr, err := utils.HttpGet(consts.SourceNameDouyin, api, map[string]string{
"User-Agent": consts.UAMac,
"Referer": searchUrl,
"Cookie": cookie,
Expand Down
4 changes: 2 additions & 2 deletions processor/kugou/kugou.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func tryGetSongMeta(hash string, albumID, mixSongID int) (songMeta *SongMeta, er
}

func getSongMeta(url string) (songMeta *SongMeta, err error) {
kugouMetaJson, err := utils.HttpGet(url, map[string]string{
kugouMetaJson, err := utils.HttpGet(consts.SourceNameKugou, url, map[string]string{
"user-agent": consts.UAMac,
})
if err != nil {
Expand All @@ -99,7 +99,7 @@ func getSongIdInfo(url string) (hash string, albumID int, mixSongID int, err err
mixSongID = albumID
return
}
html, err := utils.HttpGet(url, map[string]string{
html, err := utils.HttpGet(consts.SourceNameKugou, url, map[string]string{
"user-agent": consts.UAMac,
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion processor/kugou/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (c *Core) SearchSong() ([]*meta.SearchSongItem, error) {
var searchSongItems []*meta.SearchSongItem
api := fmt.Sprintf(APISearch, url.QueryEscape(c.Opts.Search.Keyword))

jsonStr, err := utils.HttpGet(api, map[string]string{
jsonStr, err := utils.HttpGet(consts.SourceNameKugou, api, map[string]string{
"User-Agent": consts.UAAndroid,
"Referer": "https://m3ws.kugou.com/",
})
Expand Down
4 changes: 2 additions & 2 deletions processor/kuwo/kuwo.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (c *Core) FetchMetaAndResourceInfo() (mediaMeta *meta.MediaMeta, err error)

func getSongMeta(songID int) (songMeta *SongMeta, err error) {
reqID := utils.GenReqID()
kuwoMetaJson, err := utils.HttpGet(fmt.Sprintf(InfoAPI, songID, reqID), map[string]string{
kuwoMetaJson, err := utils.HttpGet(consts.SourceNameKuwo, fmt.Sprintf(InfoAPI, songID, reqID), map[string]string{
"user-agent": consts.UAMac,
})
if err != nil {
Expand All @@ -79,7 +79,7 @@ func getSongMeta(songID int) (songMeta *SongMeta, err error) {

func getSongUrl(songID int) (songUrl string, err error) {
reqID := utils.GenReqID()
kuwoMetaJson, err := utils.HttpGet(fmt.Sprintf(PlayAPI, songID, reqID), map[string]string{
kuwoMetaJson, err := utils.HttpGet(consts.SourceNameKuwo, fmt.Sprintf(PlayAPI, songID, reqID), map[string]string{
"user-agent": consts.UAMac,
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion processor/kuwo/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (c *Core) SearchSong() ([]*meta.SearchSongItem, error) {
var searchSongItems []*meta.SearchSongItem
api := fmt.Sprintf(APISearch, url.QueryEscape(c.Opts.Search.Keyword))

jsonStr, err := utils.HttpGet(api, map[string]string{
jsonStr, err := utils.HttpGet(consts.SourceNameKuwo, api, map[string]string{
"User-Agent": consts.UAMac,
"Referer": "https://m.kuwo.cn/",
})
Expand Down
2 changes: 1 addition & 1 deletion processor/migu/fetch_song.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (c *Core) fetchFromSong() (mediaMeta *meta.MediaMeta, err error) {
return
}

songJson, err := utils.HttpGet(fmt.Sprintf(ApiUrlSong, songId), map[string]string{
songJson, err := utils.HttpGet(consts.SourceNameMigu, fmt.Sprintf(ApiUrlSong, songId), map[string]string{
"user-agent": consts.UAMac,
"referer": c.Opts.Url,
})
Expand Down
6 changes: 4 additions & 2 deletions processor/migu/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
"github.com/foamzou/audio-get/utils"
)

const APISearch = "https://m.music.migu.cn/migumusic/h5/search/all?text=%s&pageNo=1&pageSize=30"
const (
APISearch = "https://m.music.migu.cn/migumusic/h5/search/all?text=%s&pageNo=1&pageSize=30"
)

func (c *Core) SearchSong() ([]*meta.SearchSongItem, error) {
var searchSongItems []*meta.SearchSongItem
Expand All @@ -23,7 +25,7 @@ func (c *Core) SearchSong() ([]*meta.SearchSongItem, error) {
api := fmt.Sprintf(APISearch, url.QueryEscape(keyword))

ua := consts.UAAndroid
jsonStr, err := utils.HttpGet(api, map[string]string{
jsonStr, err := utils.HttpGet(consts.SourceNameMigu, api, map[string]string{
"User-Agent": ua,
"By": utils.Md5(ua),
"Referer": "https://m.music.migu.cn/v4/search",
Expand Down
2 changes: 1 addition & 1 deletion processor/netease/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func getSongUrl(songId int) string {
}

func fetchHtml(url string) (string, error) {
return utils.HttpGet(url, map[string]string{
return utils.HttpGet(consts.SourceNameNetease, url, map[string]string{
"user-agent": consts.UAMac,
})
}
2 changes: 1 addition & 1 deletion processor/netease/mlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (c *Core) fetchFromMlog() (mediaMeta *meta.MediaMeta, err error) {
if err != nil {
return
}
videoIdJson, err := utils.PostForm(ApiMlogVideoConvertId,
videoIdJson, err := utils.PostForm(consts.SourceNameNetease, ApiMlogVideoConvertId,
map[string]string{
"params": params,
"encSecKey": encSecKey,
Expand Down
2 changes: 1 addition & 1 deletion processor/netease/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (c *Core) SearchSong() ([]*meta.SearchSongItem, error) {
if err != nil {
return nil, err
}
jsonStr, err := utils.PostForm(APISearch,
jsonStr, err := utils.PostForm(consts.SourceNameNetease, APISearch,
map[string]string{
"params": params,
"encSecKey": encSecKey,
Expand Down
2 changes: 1 addition & 1 deletion processor/qmkg/qmkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (c *Core) GetSourceName() string {

func (c *Core) FetchMetaAndResourceInfo() (mediaMeta *meta.MediaMeta, err error) {

html, err := utils.HttpGet(c.Opts.Url, map[string]string{
html, err := utils.HttpGet(consts.SourceNameQMKG, c.Opts.Url, map[string]string{
"user-agent": consts.UAAndroid,
"referer": c.Opts.Url,
})
Expand Down
4 changes: 2 additions & 2 deletions processor/qqmusic/qqmusic.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (c *Core) FetchMetaAndResourceInfo() (mediaMeta *meta.MediaMeta, err error)
}

func getSongUrlAndCoverUrl(songMid string) (songUrl, coverUrl string, err error) {
html, err := utils.HttpGet(fmt.Sprintf(SongHtmlURL, songMid), map[string]string{
html, err := utils.HttpGet(consts.SourceNameQq, fmt.Sprintf(SongHtmlURL, songMid), map[string]string{
"user-agent": consts.UAAndroid,
})
if err != nil {
Expand Down Expand Up @@ -114,7 +114,7 @@ func getSongMid(url string) (string, error) {

func getSongMeta(songMid string) (songMeta *SongMeta, err error) {
songAPI := fmt.Sprintf(SongInfoAPI, songMid)
qqMetaJson, err := utils.HttpGet(songAPI, map[string]string{
qqMetaJson, err := utils.HttpGet(consts.SourceNameQq, songAPI, map[string]string{
"user-agent": consts.UAMac,
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion processor/qqmusic/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (c *Core) SearchSong() ([]*meta.SearchSongItem, error) {
var searchSongItems []*meta.SearchSongItem
api := fmt.Sprintf(APISearch, url.QueryEscape(c.Opts.Search.Keyword))

jsonStr, err := utils.HttpGet(api, map[string]string{
jsonStr, err := utils.HttpGet(consts.SourceNameQq, api, map[string]string{
"User-Agent": consts.UAAndroid,
"Referer": "https://i.y.qq.com/",
})
Expand Down
6 changes: 4 additions & 2 deletions processor/youtube/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import (
"github.com/foamzou/audio-get/utils"
)

const SearchPage = "https://www.youtube.com/results?search_query=%s"
const (
SearchPage = "https://www.youtube.com/results?search_query=%s"
)

func (c *Core) SearchSong() ([]*meta.SearchSongItem, error) {
var searchSongItems []*meta.SearchSongItem
searchUrl := fmt.Sprintf(SearchPage, url.QueryEscape(c.Opts.Search.Keyword))

htmlInfo, err := utils.HttpGet(searchUrl, map[string]string{
htmlInfo, err := utils.HttpGet(consts.SourceNameYoutube, searchUrl, map[string]string{
"user-agent": consts.UAMac,
"referer": "https://www.youtube.com/",
})
Expand Down
2 changes: 1 addition & 1 deletion processor/youtube/youtube.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (c *Core) GetSourceName() string {
}

func (c *Core) FetchMetaAndResourceInfo() (mediaMeta *meta.MediaMeta, err error) {
htmlInfo, err := utils.HttpGet(c.Opts.Url, map[string]string{
htmlInfo, err := utils.HttpGet(consts.SourceNameYoutube, c.Opts.Url, map[string]string{
"user-agent": consts.UAMac,
"referer": "https://www.youtube.com/",
})
Expand Down

0 comments on commit 2371d55

Please sign in to comment.