Skip to content

Commit

Permalink
Added 'facebook'.
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomontalbano committed Sep 14, 2018
1 parent 304cf65 commit 502319e
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 4 deletions.
1 change: 1 addition & 0 deletions .env.sample
@@ -1,2 +1,3 @@
NODE_ENV=development
CLOUDINARY_CLOUD_NAME=
FACEBOOK_ACCESS_TOKEN=
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/index.html
Expand Up @@ -85,6 +85,7 @@ <h2>Features</h2>
<p>List of supported video providers:</p>
<div class="providers">
<div class="provider"><i class="fas fa-check"></i><span>Youtube</span></div>
<div class="provider"><i class="fas fa-check"></i><span>Facebook (low-quality)</span></div>
<div class="provider"><i class="fas fa-check"></i><span>Dailymotion</span></div>
<div class="provider"><i class="fas fa-check"></i><span>Vimeo</span></div>
</div>
Expand Down
29 changes: 29 additions & 0 deletions src/lambda/classes/Providers/Facebook.js
@@ -0,0 +1,29 @@
import VideoProvider from '../VideoProvider';
import fetch from 'node-fetch';

const { FACEBOOK_ACCESS_TOKEN } = process.env;

// https://www.facebook.com/backintimetheparty/videos/1588846901182916/

export default class Facebook extends VideoProvider {
static get regex() {
return [
// - //www.facebook.com/backintimetheparty/videos/1588846901182916/
/https?\:\/\/www\.facebook\.com\/[\w]+\/videos\/([0-9]+)/,

// - //www.facebook.com/backintimetheparty/videos/description/1588846901182916/
/https?\:\/\/www\.facebook\.com\/[\w]+\/videos[\w/-]+\/([0-9]+)/,
];
}

getThumbnail_asUrl() {
// unfortunately the FACEBOOK_ACCESS_TOKEN is temporally and there is no way to get a quality thumbnail without a valid token.
if (FACEBOOK_ACCESS_TOKEN) {
return fetch(`https://graph.facebook.com/${this.getId()}?access_token=${FACEBOOK_ACCESS_TOKEN}&fields=title,description,updated_time,id,thumbnails`)
.then(response => response.json())
.then(json => json.thumbnails.data.filter(t => t.is_preferred)[0].uri)
}

return new Promise(resolve => resolve(`https://graph.facebook.com/${this.getId()}/picture`));
}
}
21 changes: 21 additions & 0 deletions src/lambda/classes/Providers/Facebook.test.js
@@ -0,0 +1,21 @@
import Facebook from './Facebook';

describe('Facebook', () => {
it('"regex" must be correct.', () => {
expect(Facebook.getVideoId('https://www.facebook.com/backintimetheparty/videos/1588846901182916/')).toBe('1588846901182916');
expect(Facebook.getVideoId('https://www.facebook.com/backintimetheparty/videos/description/1588846901182916/')).toBe('1588846901182916');
});

it('all methods must work.', () => {
const url = 'https://www.facebook.com/backintimetheparty/videos/1588846901182916/';
const video = new Facebook(url);

// static methods
expect(Facebook.check(url)).toBe(true);

// instance methods
expect(video.getId()).toBe('1588846901182916');
expect(video.providerName).toBe('facebook');
expect(video.url).toBe(url);
});
})
9 changes: 6 additions & 3 deletions src/lambda/classes/VideoProvider.js
Expand Up @@ -43,9 +43,12 @@ export default class VideoProvider {
}

fetchCloudinary(url) {
const cloudinaryUrl = `http://res.cloudinary.com/${CLOUDINARY_CLOUD_NAME}/image/fetch/h_720/l_video_to_markdown:${this.providerName}_play,g_center/${url}`;
this.log('fetchCloudinary', cloudinaryUrl);
return fetch(cloudinaryUrl)
let fetchUrl = url;
if (CLOUDINARY_CLOUD_NAME) {
fetchUrl = `http://res.cloudinary.com/${CLOUDINARY_CLOUD_NAME}/image/fetch/h_720/l_video_to_markdown:${this.providerName}_play,g_center/${encodeURIComponent(url)}`;
}
this.log('fetchCloudinary', fetchUrl);
return fetch(fetchUrl)
.then(response => response.buffer())
}

Expand Down
1 change: 1 addition & 0 deletions src/lambda/classes/VideoWrapper.js
Expand Up @@ -2,6 +2,7 @@ export default class VideoWrapper {
static get videoProviders() {
return [
require('./Providers/Dailymotion'),
require('./Providers/Facebook'),
require('./Providers/Youtube'),
require('./Providers/Vimeo'),
];
Expand Down
1 change: 1 addition & 0 deletions src/lambda/classes/VideoWrapper.test.js
Expand Up @@ -5,5 +5,6 @@ describe('VideoWrapper', () => {
expect(VideoWrapper.create('https://www.dailymotion.com/video/x3ke49').providerName).toBe('dailymotion');
expect(VideoWrapper.create('https://vimeo.com/263856289').providerName).toBe('vimeo');
expect(VideoWrapper.create('https://www.youtube.com/watch?v=oRdzL2DX0yU').providerName).toBe('youtube');
expect(VideoWrapper.create('https://www.facebook.com/backintimetheparty/videos/1588846901182916/').providerName).toBe('facebook');
});
});
2 changes: 1 addition & 1 deletion src/styles/_homepage.scss
Expand Up @@ -74,7 +74,7 @@ form {
.providers {
@include grid-row();
.provider {
@include grid-column((xxsmall: 1 of 1, small: 1 of 2, large: 1 of 3));
@include grid-column((xxsmall: 1 of 1, small: 1 of 2, large: 1 of 3, xlarge: 1 of 4));
cursor: default;
display: block;
.fa-check {
Expand Down

0 comments on commit 502319e

Please sign in to comment.