-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add qingting.fm support #650
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,142 @@ | ||||||
package qingting | ||||||
|
||||||
import ( | ||||||
"encoding/json" | ||||||
"fmt" | ||||||
"github.com/iawia002/annie/config" | ||||||
"github.com/iawia002/annie/downloader" | ||||||
"github.com/iawia002/annie/extractors" | ||||||
"github.com/iawia002/annie/request" | ||||||
"github.com/iawia002/annie/utils" | ||||||
"io/ioutil" | ||||||
"net/http" | ||||||
"strings" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please group those packages |
||||||
) | ||||||
|
||||||
type ChannelInfoApi struct { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
Data ChannelInfo | ||||||
Code int | ||||||
} | ||||||
|
||||||
type ChannelInfo struct { | ||||||
ProgramCount int `json:"program_count"` | ||||||
Name string | ||||||
} | ||||||
|
||||||
type ChannelAudioInfoApi struct { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||||||
Data []AudioInfo | ||||||
Code int | ||||||
Total int | ||||||
} | ||||||
|
||||||
type AudioInfo struct { | ||||||
FilePath string `json:"file_path"` | ||||||
Name string | ||||||
ResId int `json:"res_id"` | ||||||
UpdateTime string `json:"update_time"` | ||||||
Duration int | ||||||
Playcount string | ||||||
Id int | ||||||
Desc string | ||||||
ChannelId string `json:"channel_id"` | ||||||
Type string | ||||||
ImgUrl string `json:"img_url"` | ||||||
} | ||||||
|
||||||
// Extract is the main function for extracting data | ||||||
func Extract(uri string) ([]downloader.Data, error) { | ||||||
channelId := extractChannelId(uri) | ||||||
|
||||||
// get info of the channel | ||||||
channelInfoUrl := getChannelInfoUrl(channelId) | ||||||
channelInfoResponse, err := http.Get(channelInfoUrl) | ||||||
if err != nil { | ||||||
fmt.Println("Error in fetching JSON") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not print logs here, just return the error |
||||||
return nil, extractors.ErrURLParseFailed | ||||||
} | ||||||
defer channelInfoResponse.Body.Close() | ||||||
channelInfoBody, err := ioutil.ReadAll(channelInfoResponse.Body) | ||||||
var parsedChannelJson ChannelInfoApi | ||||||
json.Unmarshal(channelInfoBody, &parsedChannelJson) | ||||||
|
||||||
// request API and parse it | ||||||
audioInfoUrl := getChannelAudioInfoUrl(channelId) | ||||||
response, err := http.Get(audioInfoUrl) | ||||||
if err != nil { | ||||||
fmt.Println("Error in fetching JSON") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||||||
return nil, err | ||||||
} | ||||||
defer response.Body.Close() | ||||||
body, err := ioutil.ReadAll(response.Body) | ||||||
var parsedJson ChannelAudioInfoApi | ||||||
json.Unmarshal(body, &parsedJson) | ||||||
|
||||||
// handle playlist | ||||||
needDownloadItems := utils.NeedDownloadList(len(parsedJson.Data)) | ||||||
extractedData := make([]downloader.Data, len(parsedJson.Data)) | ||||||
wgp := utils.NewWaitGroupPool(config.ThreadNumber) | ||||||
dataIndex := 0 | ||||||
for index, audioInfo := range parsedJson.Data { | ||||||
|
||||||
if !utils.ItemInSlice(index+1, needDownloadItems) { | ||||||
continue | ||||||
} | ||||||
wgp.Add() | ||||||
|
||||||
go func(index int, audioInfo AudioInfo, extractedData []downloader.Data) { | ||||||
defer wgp.Done() | ||||||
extractedData[index] = qingtingDownload(audioInfo, uri) | ||||||
}(dataIndex, audioInfo, extractedData) | ||||||
dataIndex++ | ||||||
|
||||||
} | ||||||
wgp.Wait() | ||||||
return extractedData, nil | ||||||
} | ||||||
|
||||||
func extractChannelId(uri string) string { | ||||||
s := strings.Split(uri, "/") | ||||||
channid := s[len(s)-1] | ||||||
return channid | ||||||
} | ||||||
|
||||||
func getChannelInfoUrl(channelId string) string { | ||||||
return "http://i.qingting.fm/wapi/channels/" + channelId | ||||||
} | ||||||
|
||||||
func getChannelAudioInfoUrl(channelId string) string { | ||||||
return "http://i.qingting.fm/wapi/channels/" + channelId + "/programs/page/1/pagesize/250" | ||||||
} | ||||||
|
||||||
func getAudioFilePath(filePath string) string { | ||||||
return "http://od.qingting.fm/" + filePath | ||||||
} | ||||||
|
||||||
func qingtingDownload(audioInfo AudioInfo, uri string) downloader.Data { | ||||||
title := audioInfo.Name | ||||||
audioPath := getAudioFilePath(audioInfo.FilePath) | ||||||
|
||||||
streams := map[string]downloader.Stream{} | ||||||
|
||||||
size, err := request.Size(audioPath, uri) | ||||||
if err != nil { | ||||||
return downloader.EmptyData(uri, extractors.ErrURLParseFailed) | ||||||
} | ||||||
urlData := downloader.URL{ | ||||||
URL: audioPath, | ||||||
Size: size, | ||||||
Ext: "m4a", | ||||||
} | ||||||
streams["default"] = downloader.Stream{ | ||||||
URLs: []downloader.URL{urlData}, | ||||||
Size: size, | ||||||
} | ||||||
return downloader.Data{ | ||||||
Site: "qingting fm", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
Title: title, | ||||||
Type: "aduio", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
Streams: streams, | ||||||
URL: uri, | ||||||
} | ||||||
|
||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package qingting | ||
|
||
import ( | ||
"github.com/iawia002/annie/config" | ||
"github.com/iawia002/annie/downloader" | ||
"github.com/iawia002/annie/test" | ||
"testing" | ||
) | ||
|
||
func TestExtract(t *testing.T) { | ||
config.InfoOnly = true | ||
config.ThreadNumber = 9 | ||
tests := []struct { | ||
name string | ||
args test.Args | ||
playlist bool | ||
}{ | ||
{ | ||
name: "playlist test", | ||
args: test.Args{ | ||
URL: "https://www.qingting.fm/channels/226572", | ||
Title: "ViliBili | 这个冬天是个恋爱的季节", | ||
Size: 66284484, | ||
}, | ||
playlist: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var ( | ||
data []downloader.Data | ||
err error | ||
) | ||
if tt.playlist { | ||
// playlist mode | ||
config.Playlist = true | ||
_, err = Extract(tt.args.URL) | ||
test.CheckError(t, err) | ||
} else { | ||
config.Playlist = false | ||
data, err = Extract(tt.args.URL) | ||
test.CheckError(t, err) | ||
test.Check(t, tt.args, data[0]) | ||
} | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.