forked from JohannesKaufmann/html-to-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvimeo.go
156 lines (137 loc) · 4.3 KB
/
vimeo.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package plugin
import (
"encoding/json"
"fmt"
"net/http"
"regexp"
"strings"
"time"
"unicode/utf8"
md "github.com/JohannesKaufmann/html-to-markdown"
"github.com/PuerkitoBio/goquery"
)
// Timeout for the http client
var Timeout = time.Second * 10
var netClient = &http.Client{
Timeout: Timeout,
}
type vimeoVideo struct {
Type string `json:"type"`
Version string `json:"version"`
ProviderName string `json:"provider_name"`
ProviderURL string `json:"provider_url"`
Title string `json:"title"`
AuthorName string `json:"author_name"`
AuthorURL string `json:"author_url"`
IsPlus string `json:"is_plus"`
AccountType string `json:"account_type"`
HTML string `json:"html"`
Width int `json:"width"`
Height int `json:"height"`
Duration int `json:"duration"`
Description string `json:"description"`
ThumbnailURL string `json:"thumbnail_url"`
ThumbnailWidth int `json:"thumbnail_width"`
ThumbnailHeight int `json:"thumbnail_height"`
ThumbnailURLWithPlayButton string `json:"thumbnail_url_with_play_button"`
UploadDate string `json:"upload_date"`
VideoID int `json:"video_id"`
URI string `json:"uri"`
}
var vimeoID = regexp.MustCompile(`video\/(\d*)`)
type vimeoVariation int
// Configure how the Vimeo Plugin should display the video in markdown.
const (
VimeoOnlyThumbnail vimeoVariation = iota
VimeoWithTitle
VimeoWithDescription
)
// EXPERIMENTALVimeoEmbed registers a rule (for iframes) and
// returns a markdown compatible representation (link to video, ...).
func EXPERIMENTALVimeoEmbed(variation vimeoVariation) md.Plugin {
return func(c *md.Converter) []md.Rule {
getVimeoData := func(id string) (*vimeoVideo, error) {
u := fmt.Sprintf("http://vimeo.com/api/oembed.json?url=https://vimeo.com/%s", id)
resp, err := netClient.Get(u)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var res vimeoVideo
err = json.NewDecoder(resp.Body).Decode(&res)
if err != nil {
return nil, err
}
return &res, nil
}
cleanDescription := func(html string) (string, error) {
text, err := c.ConvertString(html)
if err != nil {
return "", err
}
text = strings.Replace(text, "\n", " ", -1)
text = strings.Replace(text, "\t", " ", -1)
before := utf8.RuneCountInString(text)
text = summary(text, 70)
after := utf8.RuneCountInString(text)
if after != before {
text += "..."
}
return text, nil
}
return []md.Rule{
{
Filter: []string{"iframe"},
Replacement: func(content string, selec *goquery.Selection, opt *md.Options) *string {
src := selec.AttrOr("src", "")
if !strings.Contains(src, "vimeo.com") {
return nil
}
parts := vimeoID.FindStringSubmatch(src)
if len(parts) != 2 {
return nil
}
id := parts[1]
video, err := getVimeoData(id)
if err != nil {
panic(err)
}
// desc, err := cleanDescription(video.Description)
// if err != nil {
// panic(err)
// }
// [![Little red ridning hood](http://i.imgur.com/7YTMFQp.png)](https://vimeo.com/3514904 "Little red riding hood - Click to Watch!")
// text := fmt.Sprintf("[![%s](%s) ](%s)", desc, video.ThumbnailURLWithPlayButton, "https://vimeo.com/"+video.URI)
text := fmt.Sprintf(`[![](%s)](https://vimeo.com/%d)`, video.ThumbnailURLWithPlayButton, video.VideoID)
switch variation {
case VimeoOnlyThumbnail:
// do nothing
case VimeoWithTitle:
duration := time.Duration(video.Duration) * time.Second
text += fmt.Sprintf("\n\n'%s' by ['%s'](%s) (%s)", video.Title, video.AuthorName, video.AuthorURL, duration.String())
case VimeoWithDescription:
desc, err := cleanDescription(video.Description)
if err != nil {
panic(err)
}
text += "\n\n" + desc
}
return &text
},
},
}
}
}
// truncate
func summary(text string, limit int) string {
result := text
chars := 0
for i := range text {
if chars >= limit {
result = text[:i]
break
}
chars++
}
return result
}