Skip to content
This repository has been archived by the owner on May 27, 2020. It is now read-only.

Commit

Permalink
Merge pull request #20 from lemonde/HUIT-700-published-time
Browse files Browse the repository at this point in the history
feat(provider) Récupère la date de publication d'une video
  • Loading branch information
larube authored Aug 28, 2019
2 parents 4d43a40 + 6f0f86c commit 713a8da
Show file tree
Hide file tree
Showing 22 changed files with 1,714 additions and 1,392 deletions.
16 changes: 14 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
{
"presets": ["es2015"],
"plugins": ["transform-object-rest-spread"]
"presets": [
[
"@babel/preset-env",
{
"modules": "commonjs",
"loose": true,
"forceAllTransforms": true,
"targets": {
"node": "current"
}
}
]
],
"plugins": ["@babel/plugin-proposal-object-rest-spread"]
}
40 changes: 34 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ videoProvider.extendProvider('digiteka', {
mainCatalog: '5dmpl',
// When playing a video, this indicates that the video is played
// in a back-office (to avoid ads, and not count it as a view)
zoneId: 34
zoneId: 34,
});
```

Expand All @@ -86,6 +86,7 @@ searcher().then(data => console.log(data))
thumbnailUrl: 'https://i.ytimg.com/vi/DKqOp2YHfhI/maxresdefault.jpg',
playerUrl: '//www.youtube.com/watch?v=DKqOp2YHfhI',
duration: '04:06',
pusblishedDate: '2017-09-25T19:05:29.000Z',
metadata: { embedCode: '//www.youtube.com/embed/DKqOp2YHfhI' },
provider: 'youtube',
providerVideoId: 'DKqOp2YHfhI' },
Expand All @@ -94,6 +95,7 @@ searcher().then(data => console.log(data))
thumbnailUrl: 'https://i.ytimg.com/vi/akG2cFldO6I/maxresdefault.jpg',
playerUrl: '//www.youtube.com/watch?v=akG2cFldO6I',
duration: '06:17',
publishedDate: '2017-09-25T19:05:29.000Z',
metadata: { embedCode: '//www.youtube.com/embed/akG2cFldO6I' },
provider: 'youtube',
providerVideoId: 'akG2cFldO6I'
Expand All @@ -111,7 +113,9 @@ Get a specific provider from an url.
`Return:` [provider Object](https://github.com/lemonde/universal-video-provider#provider-api)

```js
const provider = videoProvider.getProviderFromUrl('http://www.youtube.com/v/ky6CRSBcf98');
const provider = videoProvider.getProviderFromUrl(
'http://www.youtube.com/v/ky6CRSBcf98'
);
```

### getVideoFromUrl
Expand All @@ -133,7 +137,9 @@ Get a video from a provider and a video id.
`Return: video object`

```js
const youtubeProvider = videoProvider.getProviderFromUrl('http://www.youtube.com/v/ky6CRSBcf98');
const youtubeProvider = videoProvider.getProviderFromUrl(
'http://www.youtube.com/v/ky6CRSBcf98'
);
provider.getVideoFromId(youtubeProvider, 'ky6CRSBcf98');
then(video => console.log(video)).catch(err => console.error(err));
// video is an object which contains all the extracted data
Expand All @@ -148,8 +154,13 @@ Return: String
```

```js
const provider = videoProvider.getProviderFromUrl('http://www.youtube.com/v/ky6CRSBcf98');
const videoId = videoProvider.extractVideoId(provider, 'http://www.youtube.com/v/ky6CRSBcf98');
const provider = videoProvider.getProviderFromUrl(
'http://www.youtube.com/v/ky6CRSBcf98'
);
const videoId = videoProvider.extractVideoId(
provider,
'http://www.youtube.com/v/ky6CRSBcf98'
);
// 'ky6CRSBcf98'
```

Expand Down Expand Up @@ -187,7 +198,8 @@ Params: provider name (String), new fields (Object)
```js
videoProvider.extendProvider('vimeo', {
apiKey: 'myKey',
search: terms => fetch(`vimeoUrl/search/?terms=${terms}?apiKey=${this.apiKey}`)
search: terms =>
fetch(`vimeoUrl/search/?terms=${terms}?apiKey=${this.apiKey}`),
});
```

Expand Down Expand Up @@ -269,6 +281,22 @@ provider
.catch(err => console.error(err));
```

### getPublishedDate

Get the publication date of the video.

```
Params: video id
Returns: date
```

```js
provider
.getPublishedDate(videoId)
.then(publishedDate => console.log(publishedDate))
.catch(err => console.error(err));
```

### getPlayerUrl

Get the player url of the video.
Expand Down
5 changes: 4 additions & 1 deletion lib/fetch.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
'use strict';
"use strict";

/* eslint global-require: "off" */

/* eslint no-unused-expressions: "off" */

/* eslint no-undef: "off" */
try {
if (fetch) true;
} catch (err) {
require('es6-promise').polyfill();

require('isomorphic-fetch');
}

Expand Down
17 changes: 12 additions & 5 deletions lib/formatter.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
'use strict';
"use strict";

var moment = require('moment');

var formatDuration = function formatDuration(duration) {
var pattern = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'HH:mm:ss';
var formatDuration = function formatDuration(duration, pattern) {
if (pattern === void 0) {
pattern = 'HH:mm:ss';
}

return moment.unix(duration).utc().format(pattern).replace(/^00:/, '');
};

Expand All @@ -13,14 +16,18 @@ var formatVideo = function formatVideo(providerName, videoId, _ref) {
duration = _ref.duration,
thumbnailUrl = _ref.thumbnailUrl,
playerUrl = _ref.playerUrl,
embedCode = _ref.embedCode;
embedCode = _ref.embedCode,
publishedDate = _ref.publishedDate;
return {
title: title,
description: description,
thumbnailUrl: thumbnailUrl,
playerUrl: playerUrl,
duration: formatDuration(duration),
metadata: { embedCode: embedCode },
publishedDate: publishedDate,
metadata: {
embedCode: embedCode
},
provider: providerName,
providerVideoId: videoId
};
Expand Down
45 changes: 27 additions & 18 deletions lib/provider.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
'use strict';

var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
"use strict";

var _ = require('lodash');

var formatter = require('./formatter').video;

var dailymotion = require('./providers/dailymotion');

var ina = require('./providers/ina');

var youtube = require('./providers/youtube');

var digiteka = require('./providers/digiteka');

var facebook = require('./providers/facebook');

var providers = [dailymotion, ina, youtube, digiteka, facebook];
Expand All @@ -25,48 +29,53 @@ function getProviderFromUrl(url) {
}

function getVideoFromId(provider, videoId) {
return Promise.all([provider.getTitle(videoId), provider.getDescription(videoId), provider.getDuration(videoId), provider.getThumbnailUrl(videoId), provider.getPlayerUrl(videoId), provider.getEmbedCode(videoId)]).then(function (_ref) {
var _ref2 = _slicedToArray(_ref, 6),
title = _ref2[0],
description = _ref2[1],
duration = _ref2[2],
thumbnailUrl = _ref2[3],
playerUrl = _ref2[4],
embedCode = _ref2[5];

return Promise.all([provider.getTitle(videoId), provider.getDescription(videoId), provider.getDuration(videoId), provider.getThumbnailUrl(videoId), provider.getPlayerUrl(videoId), provider.getEmbedCode(videoId), provider.getPublishedDate(videoId)]).then(function (_ref) {
var title = _ref[0],
description = _ref[1],
duration = _ref[2],
thumbnailUrl = _ref[3],
playerUrl = _ref[4],
embedCode = _ref[5],
publishedDate = _ref[6];
return formatter(provider.name, videoId, {
title: title,
description: description,
duration: duration,
thumbnailUrl: thumbnailUrl,
playerUrl: playerUrl,
embedCode: embedCode
embedCode: embedCode,
publishedDate: publishedDate
});
});
}

module.exports.getVideoFromUrl = function (url) {
var provider = getProviderFromUrl(url);

if (!provider) return Promise.reject(new Error('Url pattern is not recognized'));

var videoId = extractVideoId(provider, url);

return getVideoFromId(provider, videoId);
};

module.exports.getProviderFromUrl = getProviderFromUrl;
module.exports.extractVideoId = extractVideoId;
module.exports.getVideoFromId = getVideoFromId;

module.exports.getSupportedProviders = function () {
return _.map(providers, 'name');
};

module.exports.getProviderFromName = function (name) {
return _.find(providers, { name: name });
return _.find(providers, {
name: name
});
};

module.exports.extendProvider = function (name, obj) {
return _.extend(_.find(providers, { name: name }), obj);
return _.extend(_.find(providers, {
name: name
}), obj);
};

module.exports.extendProviders = function (obj) {
return exports.getSupportedProviders().map(function (name) {
return exports.extendProvider(name, obj);
Expand Down
42 changes: 20 additions & 22 deletions lib/providers/dailymotion.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,71 @@
'use strict';
"use strict";

var _ = require('lodash');
var fetch = require('../fetch');

var fetch = require('../fetch');
/**
* Dailymotion api endpoints
*/


var fetchVideo = _.memoize(function (url) {
return fetch(url, { headers: provider.headers }).then(function (res) {
return fetch(url, {
headers: provider.headers
}).then(function (res) {
return res.json();
});
});

var getUrl = function getUrl(videoId) {
return '//www.dailymotion.com/embed/video/' + videoId;
return "//www.dailymotion.com/embed/video/" + videoId;
};

var provider = {
name: 'dailymotion',
label: 'Dailymotion',
headers: {},
videoIdExtractRegExps: [
// NOTE : behind the _ in the id, everything is discarded by dailymotion
videoIdExtractRegExps: [// NOTE : behind the _ in the id, everything is discarded by dailymotion
// standard url
// ex. http://www.dailymotion.com/video/x22i6cm_la-guerre-a-gaza-en-5-minutes_news
/^(?:https?:)?\/\/(?:www\.)?dailymotion\.com\/video\/([^?_&#]+)/i,
// standard url with extra path
/^(?:https?:)?\/\/(?:www\.)?dailymotion\.com\/video\/([^?_&#]+)/i, // standard url with extra path
// ex. http://www.dailymotion.com/toto/video/x22i6cm_la-guerre-a-gaza-en-5-minutes_news
/^(?:https?:)?\/\/(?:www\.)?dailymotion\.com\/(?:[^_&#]+)\/video\/([^?_&#]+)/i,
// short url
/^(?:https?:)?\/\/(?:www\.)?dailymotion\.com\/(?:[^_&#]+)\/video\/([^?_&#]+)/i, // short url
// ex. http://dai.ly/x22i6cm
/^(?:https?:)?\/\/dai\.ly\/([^?_&#]+)/i],

getThumbnailUrl: function getThumbnailUrl(videoId) {
return new Promise(function (resolve) {
return resolve('//www.dailymotion.com/thumbnail/video/' + videoId);
return resolve("//www.dailymotion.com/thumbnail/video/" + videoId);
});
},

getTitle: function getTitle(videoId) {
return fetchVideo('https://api.dailymotion.com/video/' + videoId + '?fields=title').then(function (result) {
return fetchVideo("https://api.dailymotion.com/video/" + videoId + "?fields=title").then(function (result) {
return _.get(result, 'title');
});
},

getDescription: function getDescription(videoId) {
return fetchVideo('https://api.dailymotion.com/video/' + videoId + '?fields=description').then(function (result) {
return fetchVideo("https://api.dailymotion.com/video/" + videoId + "?fields=description").then(function (result) {
return _.get(result, 'description');
});
},

getDuration: function getDuration(videoId) {
return fetchVideo('https://api.dailymotion.com/video/' + videoId + '?fields=duration').then(function (result) {
return fetchVideo("https://api.dailymotion.com/video/" + videoId + "?fields=duration").then(function (result) {
return _.get(result, 'duration');
});
},

getPublishedDate: function getPublishedDate(videoId) {
return fetchVideo("https://api.dailymotion.com/video/" + videoId + "?fields=created_time").then(function (result) {
return _.get(result, 'created_time');
});
},
getPlayerUrl: function getPlayerUrl(videoId) {
return new Promise(function (resolve) {
return resolve(getUrl(videoId));
});
},

getEmbedCode: function getEmbedCode(videoId) {
return new Promise(function (resolve) {
return resolve(_.compact(['<iframe src="' + getUrl(videoId) + '"', 'frameborder="0"', _.get(provider, 'embed.width') ? 'width="' + provider.embed.width + '"' : null, _.get(provider, 'embed.height') ? 'height="' + provider.embed.height + '"' : null, '></iframe>']).join(' '));
return resolve(_.compact(["<iframe src=\"" + getUrl(videoId) + "\"", 'frameborder="0"', _.get(provider, 'embed.width') ? "width=\"" + provider.embed.width + "\"" : null, _.get(provider, 'embed.height') ? "height=\"" + provider.embed.height + "\"" : null, '></iframe>']).join(' '));
});
}
};

module.exports = provider;
Loading

0 comments on commit 713a8da

Please sign in to comment.