Skip to content
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

Added Pinterest video downloading feature #1253

Merged
merged 2 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/stream_pinterest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: pinterest

on:
push:
paths:
- "extractors/pinterest/*.go"
- ".github/workflows/stream_pinterest.yml"
pull_request:
paths:
- "extractors/tiktok/*.go"
- ".github/workflows/stream_pinterest.yml"
schedule:
# run ci weekly
- cron: "0 0 * * 0"

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
go: ["1.20"]
os: [ubuntu-latest]
name: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go }}

- name: Test
run: go test -timeout 5m -race -coverpkg=./... -coverprofile=coverage.txt github.com/iawia002/lux/extractors/pinterest
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ $ lux -j "https://www.bilibili.com/video/av20203945"
| XVIDEOS | <https://xvideos.com> | ✓ | | | | | [![xvideos](https://github.com/iawia002/lux/actions/workflows/stream_xvideos.yml/badge.svg)](https://github.com/iawia002/lux/actions/workflows/stream_xvideos.yml) |
| 聯合新聞網 | <https://udn.com> | ✓ | | | | | [![udn](https://github.com/iawia002/lux/actions/workflows/stream_udn.yml/badge.svg)](https://github.com/iawia002/lux/actions/workflows/stream_udn.yml) |
| TikTok | <https://www.tiktok.com> | ✓ | | | | | [![tiktok](https://github.com/iawia002/lux/actions/workflows/stream_tiktok.yml/badge.svg)](https://github.com/iawia002/lux/actions/workflows/stream_tiktok.yml) |
| Pinterest | <https://www.pinterest.com> | ✓ | | | | | [![pinterest](https://github.com/iawia002/lux/actions/workflows/stream_pinterest.yml/badge.svg)](https://github.com/iawia002/lux/actions/workflows/stream_pinterest.yml) |
| 好看视频 | <https://haokan.baidu.com> | ✓ | | | | | [![haokan](https://github.com/iawia002/lux/actions/workflows/stream_haokan.yml/badge.svg)](https://github.com/iawia002/lux/actions/workflows/stream_haokan.yml) |
| AcFun | <https://www.acfun.cn> | ✓ | | | ✓ | | [![acfun](https://github.com/iawia002/lux/actions/workflows/stream_acfun.yml/badge.svg)](https://github.com/iawia002/lux/actions/workflows/stream_acfun.yml) |
| Eporner | <https://eporner.com> | ✓ | | | | | [![eporner](https://github.com/iawia002/lux/actions/workflows/stream_eporner.yml/badge.svg)](https://github.com/iawia002/lux/actions/workflows/stream_eporner.yml) |
Expand Down
1 change: 1 addition & 0 deletions app/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
_ "github.com/iawia002/lux/extractors/mgtv"
_ "github.com/iawia002/lux/extractors/miaopai"
_ "github.com/iawia002/lux/extractors/netease"
_ "github.com/iawia002/lux/extractors/pinterest"
_ "github.com/iawia002/lux/extractors/pixivision"
_ "github.com/iawia002/lux/extractors/pornhub"
_ "github.com/iawia002/lux/extractors/qq"
Expand Down
85 changes: 85 additions & 0 deletions extractors/pinterest/pinterest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package pinterest

import (
"regexp"
"strings"

"github.com/pkg/errors"

"github.com/iawia002/lux/extractors"
"github.com/iawia002/lux/request"
)

func init() {
extractors.Register("pinterest", New())
}

type extractor struct{}

// New returns a pinterest extractor.
func New() extractors.Extractor {
return &extractor{}
}

// 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, map[string]string{
// pinterest require a user agent
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:98.0) Gecko/20100101 Firefox/98.0",
})
if err != nil {
return nil, errors.WithStack(err)
}

urlMatcherRegExp := regexp.MustCompile(`"contentUrl":"https:\/\/v1\.pinimg\.com\/videos\/mc\/720p\/[a-zA-Z0-9\/]+\.mp4`)

downloadURLMatcher := urlMatcherRegExp.FindStringSubmatch(html)

if len(downloadURLMatcher) == 0 {
return nil, errors.WithStack(extractors.ErrURLParseFailed)
}

videoURL := strings.ReplaceAll(downloadURLMatcher[0], `"contentUrl":"`, "")

titleMatcherRegExp := regexp.MustCompile(`<title[^>]*>([^<]+)</title>`)

titleMatcher := titleMatcherRegExp.FindStringSubmatch(html)

if len(titleMatcher) == 0 {
return nil, errors.WithStack(extractors.ErrURLParseFailed)
}

title := strings.ReplaceAll(strings.ReplaceAll(titleMatcher[0], "<title>", ""), "</title>", "")

titleArr := strings.Split(title, "|")

if len(titleArr) > 0 {
title = titleArr[0]
}

streams := make(map[string]*extractors.Stream)

size, err := request.Size(videoURL, url)
if err != nil {
return nil, errors.WithStack(err)
}
urlData := &extractors.Part{
URL: videoURL,
Size: size,
Ext: "mp4",
}
streams["default"] = &extractors.Stream{
Parts: []*extractors.Part{urlData},
Size: size,
}

return []*extractors.Data{
{
Site: "Pinterest pinterest.com",
Title: title,
Type: extractors.DataTypeVideo,
Streams: streams,
URL: url,
},
}, nil
}
39 changes: 39 additions & 0 deletions extractors/pinterest/pinterest_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package pinterest

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 1",
args: test.Args{
URL: "https://www.pinterest.com/pin/creamy-cheesy-pretzel-bites-video--368450813272292084/",
Title: "Creamy Cheesy Pretzel Bites [Video] ",
Size: 30247497,
},
},
{
name: "normal test 2",
args: test.Args{
URL: "https://www.pinterest.com/pin/532198880988430823/",
Title: "Pin on TikTok ~ The world of food",
Size: 4676927,
},
},
}
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])
})
}
}