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

extractors/vimeo: Add basic support #26

Merged
merged 1 commit into from
Mar 20, 2018
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ YouTube | <https://www.youtube.com> | ✓ | | |
爱奇艺 | <https://www.iqiyi.com> | ✓ | | |
芒果TV | <https://www.mgtv.com> | ✓ | | |
Tumblr | <https://www.tumblr.com> | | ✓ | |
Vimeo | <https://vimeo.com> | ✓ | | |


## Contributing
Expand Down
80 changes: 80 additions & 0 deletions extractors/vimeo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package extractors

import (
"encoding/json"
"strings"

"github.com/iawia002/annie/downloader"
"github.com/iawia002/annie/request"
"github.com/iawia002/annie/utils"
)

type vimeoProgressive struct {
Profile int `json:"profile"`
Width int `json:"width"`
Height int `json:"height"`
Quality string `json:"quality"`
URL string `json:"url"`
}

type vimeoFiles struct {
Progressive []vimeoProgressive `json:"progressive"`
}

type vimeoRequest struct {
Files vimeoFiles `json:"files"`
}

type vimeoVideo struct {
Title string `json:"title"`
}

type vimeo struct {
Request vimeoRequest `json:"request"`
Video vimeoVideo `json:"video"`
}

func bestQuality(progressives []vimeoProgressive) vimeoProgressive {
var highestProfile int
var data vimeoProgressive
for _, progressive := range progressives {
if progressive.Profile > highestProfile {
highestProfile = progressive.Profile
data = progressive
}
}
return data
}

// Vimeo download function
func Vimeo(url string) downloader.VideoData {
var html string
var vid string
if strings.Contains(url, "player.vimeo.com") {
html = request.Get(url)
} else {
vid = utils.MatchOneOf(url, `vimeo\.com/(\d+)`)[1]
html = request.Get("https://player.vimeo.com/video/" + vid)
}
jsonString := utils.MatchOneOf(html, `{var a=(.+?);`)[1]
var vimeoData vimeo
json.Unmarshal([]byte(jsonString), &vimeoData)
video := bestQuality(vimeoData.Request.Files.Progressive)

size := request.Size(video.URL, url)
urlData := downloader.URLData{
URL: video.URL,
Size: size,
Ext: "mp4",
}
data := downloader.VideoData{
Site: "Vimeo vimeo.com",
Title: vimeoData.Video.Title,
Type: "video",
URLs: []downloader.URLData{urlData},
Size: size,
Quality: video.Quality,
}
data.Download(url)
return data
}
41 changes: 41 additions & 0 deletions extractors/vimeo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package extractors

import (
"testing"

"github.com/iawia002/annie/config"
"github.com/iawia002/annie/test"
)

func TestVimeo(t *testing.T) {
config.InfoOnly = true
tests := []struct {
name string
args test.Args
}{
{
name: "normal test",
args: test.Args{
URL: "https://player.vimeo.com/video/259325107",
Title: "prfm 20180309",
Size: 131051118,
Quality: "1080p",
},
},
{
name: "normal test",
args: test.Args{
URL: "https://vimeo.com/254865724",
Title: "MAGIC DINER PT. II",
Size: 138966306,
Quality: "1080p",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data := Vimeo(tt.args.URL)
test.Check(t, tt.args, data)
})
}
}
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ func main() {
extractors.Mgtv(videoURL)
case "tumblr":
extractors.Tumblr(videoURL)
case "vimeo":
extractors.Vimeo(videoURL)
default:
extractors.Universal(videoURL)
}
Expand Down