-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a1f7d5f
Showing
4 changed files
with
259 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2017 Nunki | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
## Installation | ||
you install it with npm : | ||
``` | ||
npm i @sa/link-preview | ||
``` | ||
## Usage | ||
You can include it easly : | ||
### ES6 | ||
```javascript | ||
import linPreview from "@sa/link-preview"; | ||
``` | ||
### ES5 | ||
```javascript | ||
var linkPreview = require("@sa/link-preview"); | ||
``` | ||
### Parameters | ||
#### url | ||
Type: `String` | ||
#### timeout | ||
Type: `Number` | ||
### Value Returned | ||
Type: `Promise` | ||
|
||
Resolved value: | ||
```javascript | ||
{ | ||
description:"GitHub is where people build software. More than 24 million people use GitHub to discover, fork, and contribute to over 67 million projects.", | ||
image:"https://assets-cdn.github.com/images/modules/open_graph/github-logo.png", | ||
imageHeight:"1200", | ||
imageType:"image/png", | ||
imageWidth:"1200", | ||
siteName:"GitHub", | ||
title:"Build software better, together", | ||
url:"http://github.com" | ||
} | ||
``` | ||
Reject if url is `undefined` | ||
|
||
Rejected value: | ||
```javascript | ||
{ message: "You must add a valid url" } | ||
``` | ||
|
||
### Syntaxe | ||
|
||
```javascript | ||
linkPreview("https://github.com").then(response => { | ||
/* response = { | ||
description:"GitHub is where people build software. More than 24 million people use GitHub to discover, fork, and contribute to over 67 million projects.", | ||
image:"https://assets-cdn.github.com/images/modules/open_graph/github-logo.png", | ||
imageHeight:"1200", | ||
imageType:"image/png", | ||
imageWidth:"1200", | ||
siteName:"GitHub", | ||
title:"Build software better, together", | ||
url:"http://github.com" | ||
} | ||
*/ | ||
}); | ||
linkPreview("https://github.com",1000).then(response => { | ||
/* if https://github.com answer before 1000ms | ||
response = { | ||
description:"GitHub is where people build software. More than 24 million people use GitHub to discover, fork, and contribute to over 67 million projects.", | ||
image:"https://assets-cdn.github.com/images/modules/open_graph/github-logo.png", | ||
imageHeight:"1200", | ||
imageType:"image/png", | ||
imageWidth:"1200", | ||
siteName:"GitHub", | ||
title:"Build software better, together", | ||
url:"http://github.com" | ||
} | ||
else | ||
response = { | ||
description: null, | ||
image:null, | ||
imageHeight:null, | ||
imageType:null, | ||
imageWidth:null, | ||
siteName:null, | ||
title:null, | ||
url:"https://ozbezpicybeu.com" | ||
} | ||
*/ | ||
}); | ||
linkPreview("https://ozbezpicybeu.com")then(response => { | ||
/* response = { | ||
description: null, | ||
image:null, | ||
imageHeight:null, | ||
imageType:null, | ||
imageWidth:null, | ||
siteName:null, | ||
title:null, | ||
url:"https://ozbezpicybeu.com" | ||
} | ||
*/ | ||
}); | ||
linkPreview("test")then(response => { | ||
/* response = { | ||
description: null, | ||
image:null, | ||
imageHeight:null, | ||
imageType:null, | ||
imageWidth:null, | ||
siteName:null, | ||
title:null, | ||
url:"test" | ||
} | ||
*/ | ||
}); | ||
linkPreview()then(() => {}).catch(err => { | ||
/* | ||
err = { message: "You must add a valid url" } | ||
*/ | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
const cheerio = require("cheerio"); | ||
const request = require("request"); | ||
|
||
const getByProp = ($, property) => | ||
$(`meta[property='${property}']`) | ||
.first() | ||
.attr("content") || null; | ||
|
||
function collectMeta($, url, deepVideo) { | ||
const ogUrl = getByProp($, "og:url"); | ||
const ogVideoUrl = getByProp($, "og:video:secure_url") || getByProp($, "og:video:url"); | ||
const res = { | ||
url, | ||
image: getByProp($, "og:image"), | ||
imageWidth: getByProp($, "og:image:width"), | ||
imageHeight: getByProp($, "og:image:height"), | ||
imageType: getByProp($, "og:image:type"), | ||
title: getByProp($, "og:title"), | ||
description: getByProp($, "og:description"), | ||
siteName: getByProp($, "og:site_name"), | ||
ogVideoUrl: (ogVideoUrl || "").indexOf("youtube.com") >= 0 ? null : ogVideoUrl, | ||
ogUrl, | ||
youtube: | ||
(ogVideoUrl || "").indexOf("youtube.com") >= 0 | ||
? ogVideoUrl | ||
: !ogUrl ? null : ogUrl.indexOf("youtube.com") >= 0 ? `https://youtube.com/embed/${getValue(ogUrl, "v")}` : null | ||
}; | ||
if (deepVideo && !res.ogVideoUrl && !res.youtube) | ||
return getDeepVideo( | ||
(getByProp($, "og:description") || "").match(/(http(s)?:\/\/([a-z0-9]+\.)+[a-z0-9]+(\/[a-z0-9]+)?)/gi) | ||
).then(ogVideoUrl => { | ||
videoUrl = ogVideoUrl.filter(url => !!url)[0] || ""; | ||
return Object.assign(res, { | ||
[videoUrl.indexOf("youtube.com") >= 0 ? "youtube" : "ogVideoUrl"]: videoUrl || null | ||
}); | ||
}); | ||
return Promise.resolve(res); | ||
} | ||
|
||
const getDeepVideo = urls => { | ||
if (!urls) return Promise.resolve([null]); | ||
return Promise.all( | ||
urls.map(url => | ||
linkPreview | ||
.makeRequest(url, 10000) | ||
.then( | ||
({ response, body }) => | ||
!response || response.statusCode !== 200 | ||
? null | ||
: getByProp(cheerio.load(body), "og:video:secure_url") || getByProp(cheerio.load(body), "og:video:url") | ||
) | ||
) | ||
); | ||
}; | ||
|
||
const getValue = (url, name) => { | ||
const i = url.indexOf(`${name}=`) + name.length + 1; | ||
const j = url.indexOf("&", i); | ||
const end = j < 0 ? url.length : j; | ||
return i < 0 ? "" : url.slice(i, end); | ||
}; | ||
|
||
const getError = url => ({ | ||
url, | ||
image: null, | ||
imageWidth: null, | ||
imageHeight: null, | ||
imageType: null, | ||
title: undefined, | ||
description: null, | ||
siteName: null | ||
}); | ||
|
||
function linkPreview(url, deepVideo = false, timeout = 100000) { | ||
if (!url || url === "") return Promise.reject({ message: "You must add a valid url" }); | ||
if (!url.match(/^http(s)?:\/\/[a-z]+\.[a-z]+(.)+/i)) return Promise.resolve(getError(url)); | ||
return linkPreview.makeRequest(url, timeout).then(({ response, body }) => { | ||
if (!response) return getError(url); | ||
if (response.statusCode === 200) return collectMeta(cheerio.load(body), url, deepVideo); | ||
return getError(url); | ||
}); | ||
} | ||
|
||
linkPreview.makeRequest = (url, timeout) => | ||
new Promise((resolve, reject) => { | ||
request(url, { timeout: timeout }, (error, response, body) => resolve({ body, response })); | ||
}); | ||
|
||
module.exports = linkPreview; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"dependencies": { | ||
"cheerio": "1.0.0-rc.2", | ||
"request": "2.82.0" | ||
}, | ||
"devDependencies": { | ||
"codecov": "2.3.0", | ||
"commitizen": "^2.9.6", | ||
"cz-conventional-changelog": "^2.0.0", | ||
"ghooks": "^2.0.0", | ||
"jest": "21.1.0", | ||
"semantic-release": "^7.0.0", | ||
"sinon": "^4.1.3", | ||
"surge": "^0.19.0" | ||
}, | ||
"homepage": "https://github.com/teamSolutionAnalysts/link-preview#readme", | ||
"keywords": [ | ||
"link", | ||
"preview", | ||
"nunki", | ||
"node.js" | ||
], | ||
"license": "MIT", | ||
"main": "index.js", | ||
"name": "@sa/link-preview", | ||
"private": false, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/teamSolutionAnalysts/link-preview.git" | ||
}, | ||
"scripts": {}, | ||
"version": "1.0.0" | ||
} |