Skip to content

Commit

Permalink
Merge pull request #450 from guanw/judew-support-porn
Browse files Browse the repository at this point in the history
Pornhub Support
  • Loading branch information
iawia002 authored Jun 5, 2019
2 parents 87b68d9 + 570d3ce commit 14c7ce2
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
74 changes: 74 additions & 0 deletions extractors/pornhub/pornhub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package pornhub

import (
"encoding/json"

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

type pornhubData struct {
DefaultQuality bool `json:"defaultQuality"`
Format string `json:"format"`
Quality string `json:"quality"`
VideoURL string `json:"videoUrl"`
}

// Extract is the main function for extracting data
func Extract(url string) ([]downloader.Data, error) {
var err error
html, err := request.Get(url, url, nil)
if err != nil {
return downloader.EmptyList, err
}
var title string
desc := utils.MatchOneOf(html, `<span class="inlineFree">(.+?)</span>`)
if desc != nil {
title = desc[1]
} else {
title = "pornhub video"
}

realURLs := utils.MatchOneOf(html, `"mediaDefinitions":(.+?),"isVertical"`)

var pornhubs []pornhubData
err = json.Unmarshal([]byte(realURLs[1]), &pornhubs)
if err != nil {
return downloader.EmptyList, err
}

//TODO add support for different quality
var realURL string
for _, downloadlink := range(pornhubs) {
if downloadlink.VideoURL != "" {
realURL = downloadlink.VideoURL
break
}
}

size, err := request.Size(realURL, url)
if err != nil {
return downloader.EmptyList, err
}
urlData := downloader.URL{
URL: realURL,
Size: size,
Ext: "mp4",
}
streams := map[string]downloader.Stream{
"default": {
URLs: []downloader.URL{urlData},
Size: size,
},
}
return []downloader.Data{
{
Site: "Pornhub",
Title: title,
Type: "video",
Streams: streams,
URL: url,
},
}, nil
}
34 changes: 34 additions & 0 deletions extractors/pornhub/pornhub_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package pornhub

import (
"testing"

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


func TestYoutube(t *testing.T) {
config.InfoOnly = true
config.RetryTimes = 10
tests := []struct {
name string
args test.Args
}{
{
name: "normal test",
args: test.Args{
URL: "https://www.pornhub.com/view_video.php?viewkey=ph5cb5fc41c6ebd",
Title: "Must Watch MILF Drilled by the Fireplace",
Size: 158868371,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data, err := Extract(tt.args.URL)
test.CheckError(t, err)
test.Check(t, tt.args, data[0])
})
}
}
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/iawia002/annie/extractors/miaopai"
"github.com/iawia002/annie/extractors/netease"
"github.com/iawia002/annie/extractors/pixivision"
"github.com/iawia002/annie/extractors/pornhub"
"github.com/iawia002/annie/extractors/qq"
"github.com/iawia002/annie/extractors/tumblr"
"github.com/iawia002/annie/extractors/twitter"
Expand Down Expand Up @@ -152,6 +153,8 @@ func download(videoURL string) {
data, err = yinyuetai.Extract(videoURL)
case "geekbang":
data, err = geekbang.Extract(videoURL)
case "pornhub":
data, err = pornhub.Extract(videoURL)
default:
data, err = universal.Extract(videoURL)
}
Expand Down

0 comments on commit 14c7ce2

Please sign in to comment.