Skip to content

Commit 0dff396

Browse files
committed
feat(vimeo): Support for fetching vimeo data added
1 parent c2c1f9f commit 0dff396

4 files changed

Lines changed: 135 additions & 52 deletions

File tree

src/embed.es6

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,62 +19,62 @@
1919
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2020
//SOFTWARE.
2121

22-
import utils from './modules/utils.es6';
23-
import Emoji from './modules/emoticons/emoji.es6';
24-
import Smiley from './modules/emoticons/smiley.es6';
25-
import Url from './modules/url.es6';
26-
import CodeEmbed from './modules/code/codeEmbed.es6';
22+
import utils from './modules/utils.es6';
23+
import Emoji from './modules/emoticons/emoji.es6';
24+
import Smiley from './modules/emoticons/smiley.es6';
25+
import Url from './modules/url.es6';
26+
import CodeEmbed from './modules/code/codeEmbed.es6';
2727
import VideoEmbed from './modules/video/videoEmbed.es6';
28-
import Twitter from './modules/twitter.es6';
28+
import Twitter from './modules/twitter.es6';
2929

3030
(function() {
3131

3232
var defaultOptions = {
33-
link: true,
34-
linkOptions: {
35-
target: 'self',
36-
exclude: ['pdf']
33+
link : true,
34+
linkOptions : {
35+
target : 'self',
36+
exclude : ['pdf']
3737
},
38-
emoji: true,
39-
fontIcons: true,
40-
highlightCode: true,
41-
tweetsEmbed: true,
42-
tweetOptions: {
43-
maxWidth: 550,
44-
hideMedia: false,
45-
hideThread: false,
46-
align: 'none',
47-
lang: 'en'
38+
emoji : true,
39+
fontIcons : true,
40+
highlightCode : true,
41+
tweetsEmbed : true,
42+
tweetOptions : {
43+
maxWidth : 550,
44+
hideMedia : false,
45+
hideThread : false,
46+
align : 'none',
47+
lang : 'en'
4848
},
49-
excludeEmbed: [],
50-
codeEmbedHeight: 500,
51-
videoHeight: null,
52-
videoWidth: null,
53-
vineOptions: {
54-
maxWidth: null,
55-
type: 'postcard', //'postcard' or 'simple' embedding
56-
responsive: true,
57-
width : 350,
58-
height : 460
49+
excludeEmbed : [],
50+
codeEmbedHeight : 500,
51+
videoHeight : null,
52+
videoWidth : null,
53+
vineOptions : {
54+
maxWidth : null,
55+
type : 'postcard', //'postcard' or 'simple' embedding
56+
responsive : true,
57+
width : 350,
58+
height : 460
5959
},
60-
googleAuthKey:'AIzaSyCqFouT8h5DKAbxlrTZmjXEmNBjC69f0ts'
60+
googleAuthKey : 'AIzaSyCqFouT8h5DKAbxlrTZmjXEmNBjC69f0ts'
6161
};
6262

6363
class EmbedJS {
6464
constructor(options) {
6565
this.options = utils.deepExtend(defaultOptions, options);
6666
this.element = this.options.element;
67-
this.input = this.element.innerHTML;
67+
this.input = this.element.innerHTML;
6868
this.process();
6969
}
7070

7171
async process() {
72-
let input = this.input;
73-
let options = this.options;
74-
let embeds = [];
75-
let output = options.link ? await (new Url(input, options).process()) : output;
76-
output = options.emoji ? await (new Emoji(output, options).process()) : output;
77-
output = options.fontIcons ? await (new Smiley(output, options).process()) : output;
72+
let input = this.input;
73+
let options = this.options;
74+
let embeds = [];
75+
let output = options.link ? await (new Url(input, options).process()) : output;
76+
output = options.emoji ? await (new Emoji(output, options).process()) : output;
77+
output = options.fontIcons ? await (new Smiley(output, options).process()) : output;
7878
[output, embeds] = await (new CodeEmbed(input, output, options, embeds).process());
7979
[output, embeds] = await (new VideoEmbed(input, output, options, embeds).process());
8080

src/modules/video/videoEmbed.es6

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Ustream from './ustream.es6';
66
import LiveLeak from './liveleak.es6';
77
import Vine from './vine.es6';
88
import Youtube from './youtube.es6';
9+
import Vimeo from './vimeo.es6';
910

1011
class VideoEmbed {
1112
constructor(input, output, options, embeds) {
@@ -26,6 +27,8 @@ class VideoEmbed {
2627
embeds = utils.ifEmbed(this.options, 'liveleak') ? await (new LiveLeak(input, this.options, embeds).process()) : output;
2728
embeds = utils.ifEmbed(this.options, 'vine') ? await (new Vine(input, this.options, embeds).process()) : output;
2829
embeds = utils.ifEmbed(this.options, 'youtube') ? await (new Youtube(input, this.options, embeds).process()) : output;
30+
embeds = utils.ifEmbed(this.options, 'vimeo') ? await (new Vimeo(input, this.options, embeds).process()) : output;
31+
2932

3033
return [output, embeds];
3134
} catch (error) {

src/modules/video/vimeo.es6

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import utils from '../utils.es6';
2+
3+
class Vimeo {
4+
constructor(input, options, embeds) {
5+
this.input = input;
6+
this.options = options;
7+
this.embeds = embeds;
8+
9+
this.regex = /https?:\/\/(?:www\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|)(\d+)(?:$|\/|\?)*/gi;
10+
}
11+
12+
formatData(data) {
13+
return {
14+
title : data.title,
15+
thumbnail : data.thumbnail_medium,
16+
rawDescription : data.description.replace(/\n/g,'<br/>').replace(/&#10;/g, '<br/>'),
17+
views : data.stats_number_of_plays,
18+
likes : data.stats_number_of_likes,
19+
description : utils.truncate(data.description.replace(/((<|&lt;)br\s*\/*(>|&gt;)\r\n)/g, ' '), 150),
20+
url : data.url,
21+
id : data.id,
22+
host : 'vimeo'
23+
}
24+
}
25+
26+
template(data) {
27+
var template =
28+
`<div class="ejs-video">
29+
<div class="ejs-video-preview">
30+
<div class="ejs-video-thumb">
31+
<img src="${data.thumbnail}" alt="${data.host}/${data.id}"/>
32+
<i class="fa fa-play-circle-o"></i>
33+
</div>
34+
<div class="ejs-video-detail">
35+
<div class="ejs-video-title">
36+
<a href="${data.url}">${data.title}</a>
37+
</div>
38+
<div class="ejs-video-desc">${data.description}</div>
39+
<div class="ejs-video-stats">
40+
<span><i class="fa fa-eye"></i>${data.views}</span>
41+
<span><i class="fa fa-heart"></i>${data.likes}</span>
42+
</div>
43+
</div>
44+
</div>
45+
</div>`;
46+
return template;
47+
}
48+
49+
async data(id) {
50+
try {
51+
let url = `https://vimeo.com/api/v2/video/${id}.json`;
52+
let response = await fetch(url);
53+
let data = await response.json();
54+
return data[0];
55+
} catch (error) {
56+
console.log(error);
57+
}
58+
59+
}
60+
61+
async process() {
62+
try {
63+
let match;
64+
while ((match = utils.matches(this.regex, this.input)) !== null) {
65+
let data = await this.data(match[3]);
66+
let text = this.template(this.formatData(data));
67+
this.embeds.push({
68+
text: text,
69+
index: match.index
70+
})
71+
}
72+
return this.embeds;
73+
} catch (error) {
74+
console.log(error);
75+
}
76+
}
77+
}
78+
79+
module.exports = Vimeo;
80+

src/modules/video/youtube.es6

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@ import utils from '../utils.es6';
22

33
class Youtube {
44
constructor(input, options, embeds) {
5-
this.input = input;
5+
this.input = input;
66
this.options = options;
7-
this.embeds = embeds;
7+
this.embeds = embeds;
88

9-
this.regex = /https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=|\/ytscreeningroom\?v=|\/feeds\/api\/videos\/|\/user\S*[^\w\-\s]|\S*[^\w\-\s]))([\w\-]{11})[?=&+%\w-]*/gi;
9+
this.regex = /https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=|\/ytscreeningroom\?v=|\/feeds\/api\/videos\/|\/user\S*[^\w\-\s]|\S*[^\w\-\s]))([\w\-]{11})[?=&+%\w-]*/gi;
1010
}
1111

1212
formatData(data) {
1313
return {
14-
title: data.snippet.title,
15-
thumbnail: data.snippet.thumbnails.medium.url,
16-
rawDescription: data.snippet.description,
17-
views: data.statistics.viewCount,
18-
likes: data.statistics.likeCount,
19-
description: utils.truncate(data.snippet.description, 150),
20-
url: `https://www.youtube.com/watch?v=${data.id}`,
21-
id: data.id,
22-
host: 'youtube'
14+
title : data.snippet.title,
15+
thumbnail : data.snippet.thumbnails.medium.url,
16+
rawDescription : data.snippet.description,
17+
views : data.statistics.viewCount,
18+
likes : data.statistics.likeCount,
19+
description : utils.truncate(data.snippet.description, 150),
20+
url : `https://www.youtube.com/watch?v=${data.id}`,
21+
id : data.id,
22+
host : 'youtube'
2323
}
2424
}
2525

@@ -48,9 +48,9 @@ class Youtube {
4848

4949
async data(id) {
5050
try {
51-
let url = `https://www.googleapis.com/youtube/v3/videos?id=${id}&key=${this.options.googleAuthKey}&part=snippet,statistics`;
51+
let url = `https://www.googleapis.com/youtube/v3/videos?id=${id}&key=${this.options.googleAuthKey}&part=snippet,statistics`;
5252
let response = await fetch(url);
53-
let data = await response.json();
53+
let data = await response.json();
5454
return data.items[0];
5555
} catch (error) {
5656
console.log(error);

0 commit comments

Comments
 (0)