-
Notifications
You must be signed in to change notification settings - Fork 2
/
pornhub.go
76 lines (59 loc) · 1.47 KB
/
pornhub.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//go:generate mockgen -source=$GOFILE -destination=mock_$GOFILE -package=$GOPACKAGE
package pornhub
import (
"errors"
"fmt"
"net/http"
"strings"
"github.com/PuerkitoBio/goquery"
)
type Client interface {
GetThumbnailURLs(videoURL string) ([]string, error)
}
type clientImpl struct{}
func NewClient() Client {
return &clientImpl{}
}
func (c *clientImpl) GetThumbnailURLs(videoURL string) ([]string, error) {
const imageNum = 16
ret := make([]string, 0, imageNum)
u, err := c.getThumbnailURL(videoURL)
if err != nil {
return nil, err
}
spl := strings.Split(u, ".")
if len(spl) < 2 {
return nil, errors.New("unexpected URL format")
}
ext := spl[len(spl)-1]
spl = strings.Split(u, ")")
if len(spl) < 2 {
return nil, errors.New("unexpected URL format")
}
spl = spl[:len(spl)-1]
for i := 1; i <= imageNum; i++ {
ret = append(ret, fmt.Sprintf("%s)%d.%s", strings.Join(spl, ")"), i, ext))
}
return ret, nil
}
func (c *clientImpl) getThumbnailURL(videoURL string) (string, error) {
res, err := http.Get(videoURL)
if err != nil {
return "", err
}
defer func() {
_ = res.Body.Close()
}()
if res.StatusCode != http.StatusOK {
return "", fmt.Errorf("returned %d from videoURL", res.StatusCode)
}
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return "", err
}
imageURL, exists := doc.Find(`meta[property="og:image"]`).First().Attr("content")
if !exists {
return "", errors.New("could not find image URL")
}
return imageURL, nil
}