Skip to content

Commit

Permalink
Added Pinterest video downloading feature (#1253)
Browse files Browse the repository at this point in the history
* Added Pinterest video downloading feature

* Update pinterest.go

Fixed title extractor
  • Loading branch information
GopherGhaznix authored Jul 5, 2023
1 parent c17006e commit 3da4af3
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 0 deletions.
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])
})
}
}

0 comments on commit 3da4af3

Please sign in to comment.