-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support video download for xiaohognshu.com
- Loading branch information
Showing
3 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package xiaohongshu | ||
|
||
import ( | ||
"encoding/json" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/iawia002/lux/config" | ||
"github.com/iawia002/lux/extractors" | ||
"github.com/iawia002/lux/request" | ||
"github.com/iawia002/lux/utils" | ||
) | ||
|
||
func init() { | ||
extractors.Register("xiaohongshu", New()) | ||
} | ||
|
||
type extractor struct{} | ||
|
||
// New returns a xiaohognshu extractor. | ||
func New() extractors.Extractor { | ||
return &extractor{} | ||
} | ||
|
||
const mp4VideoType = "mp4" | ||
|
||
// Extract is the main function to extract the data. | ||
func (e *extractor) Extract(url string, option extractors.Options) ([]*extractors.Data, error) { | ||
html, err := request.Get(url, url, config.FakeHeaders) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
|
||
// title | ||
titles := utils.MatchOneOf(html, `,"title":"(.+?)",`) | ||
if titles == nil || len(titles) != 2 { | ||
return nil, errors.WithStack(extractors.ErrBodyParseFailed) | ||
} | ||
title := titles[1] | ||
|
||
// video url | ||
urlsJSON := utils.MatchOneOf(html, `"backupUrls":(\[.+?\])`) | ||
if urlsJSON == nil || len(urlsJSON) != 2 { | ||
return nil, errors.WithStack(extractors.ErrBodyParseFailed) | ||
} | ||
var urls []string | ||
err = json.Unmarshal([]byte(urlsJSON[1]), &urls) | ||
if err != nil { | ||
return nil, errors.WithStack(extractors.ErrBodyParseFailed) | ||
} | ||
|
||
// streams | ||
streams := make(map[string]*extractors.Stream) | ||
var size int64 | ||
for i, u := range urls { | ||
if !strings.Contains(u, mp4VideoType) { | ||
continue | ||
} | ||
size, err = request.Size(u, u) | ||
if err != nil { | ||
continue | ||
} | ||
streams[strconv.Itoa(i)] = &extractors.Stream{ | ||
Parts: []*extractors.Part{ | ||
{ | ||
URL: u, | ||
Size: size, | ||
Ext: mp4VideoType, | ||
}, | ||
}, | ||
Size: size, | ||
} | ||
} | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
if len(streams) == 0 { | ||
return nil, errors.WithStack(extractors.ErrBodyParseFailed) | ||
} | ||
|
||
return []*extractors.Data{ | ||
{ | ||
Site: "小红书 xiaohongshu.com", | ||
Title: title, | ||
Type: extractors.DataTypeVideo, | ||
Streams: streams, | ||
URL: url, | ||
}, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package xiaohongshu | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/iawia002/lux/extractors" | ||
"github.com/iawia002/lux/test" | ||
) | ||
|
||
func TestDownload(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
args test.Args | ||
}{ | ||
{ | ||
name: "normal test", | ||
args: test.Args{ | ||
URL: "https://www.xiaohongshu.com/explore/64e9f1e50000000003023b3f?m_source=pinpai", | ||
Title: "七星级大厨都不会告诉你的,五花肉的8种做法", | ||
Size: 59410194, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
data, err := New().Extract(tt.args.URL, extractors.Options{}) | ||
test.CheckError(t, err) | ||
test.Check(t, tt.args, data[0]) | ||
}) | ||
} | ||
} |