From bffad9867ad44d2826026eecfbf607edbff14567 Mon Sep 17 00:00:00 2001 From: "Matheus Cesar C. - Matsukii" Date: Mon, 7 Oct 2019 22:08:07 -0300 Subject: [PATCH] thumbnail for video metadata and dedicated url --- README.md | 124 ++++++++++++++++++++++++++++-------------- package-lock.json | 9 ++- package.json | 2 +- src/router.js | 28 +++++++++- src/urlVideoParser.js | 53 +++++++++++++++++- utils.md | 4 ++ 6 files changed, 172 insertions(+), 48 deletions(-) create mode 100644 utils.md diff --git a/README.md b/README.md index ed2dbce..eb64be0 100644 --- a/README.md +++ b/README.md @@ -1,86 +1,113 @@ # PolarPod -A set of useful apis +A set of useful apis + +## this API uses -# this API uses: * open-graph-crapper -* word filter [only english support up to now] -* atob and btoa base64 +* word filter [only english support up to now] +* atob and btoa base64 * express * js-video-url-parser - * qrcode-svg -## Inside but not in use: +## Inside but not in use + * metafetch [unused, secondary] * nano-memoize [unused] * mysql - # General usage ## Og tag getter -``` +``` http https://polarpod.herokuapp.com/apis/ogtags?u=[URL] -``` +``` + +parameters: -parameters:
-* u: url
+* u: url + +response: -response:
* json ### see samples file or below - ## SVG QR Code generator -``` +``` http https://polarpod.herokuapp.com/apis/qr?u=[URL]&d=[true]/[false]&w=[width]&c=[color]&bg=[background] -``` +``` + +parameters: -parameters:
-* u: data
-* d: dark/light [true/false] (if true generates a lighet qr)
-* w: width
-* c: HEX color **(without the #)** _default colors: for dark themes=#CCCCCC and light=#222222_
+* u: data +* d: dark/light [true/false] (if true generates a lighet qr) +* w: width +* c: HEX color **(without the #)** _default colors: for dark themes=#CCCCCC and light=#222222_ * bg: background color in HEX **(without the #)** _default is transparent_ *if any param was send, a message will be showed* -response:
-* SVG document +response: +* SVG document ## Video URL parser -``` -http://polarpod.herokuapp.com/apis/vidurl?u=[URL] -``` +``` http +http://polarpod.herokuapp.com/apis/video/meta?u=[URL] +``` + +params: -params:
* u: video url -response:
+response: + +* json + +## Video thumbnail + +**same as video url parser, but with different response and url** + +``` http +http://polarpod.herokuapp.com/apis/video/thumb?u=[URL] +``` + +supported platforms + +* youtube +* vimeo + +params: + +* u: video url + +response: + * json ## Bad-word message filter -``` +``` http https://polarpod.herokuapp.com/apis/filter?msg=[message_to_filter] -``` +``` + +params: -params:
* msg: message -response:
-* json +response: +* json # responses -## og tags response sample:
-``` +## og tags response sample + +``` json { "name": "GitHub", "title": "Build software better, together", @@ -91,16 +118,32 @@ response:
} ``` -## video url parser:
-``` +## video url parser + +_thumbnails is currently avaliable for youtube and vimeo only_ +_for youtube, maxresdefault will be retuned if avaliable, else will return '0.jpg', the same as hqdefault.jpg_ +_for vimeo thumbails the image size '_640' is replaced by '_1920'_ + +``` json { "id": "jYCGSuNDy4M", "mediaType": "video", - "provider": "youtube" - } -``` -## bad-word filter:
+ "provider": "youtube", + "thumb":"https://img.youtube.com/vi/jYCGSuNDy4M/maxresdefault.jpg" + }, + + { + "id": "76979871", + "mediaType": "video", + "provider": "vimeo", + "thumb":"https://i.vimeocdn.com/video/452001751_1920.jpg", + "thumbOriginal":"https://i.vimeocdn.com/video/452001751_640.jpg" + }, ``` + +## bad-word filter + +``` json { "msg":"****" } @@ -121,4 +164,3 @@ response:
## LICENSE * MIT - diff --git a/package-lock.json b/package-lock.json index 963e2d5..545359f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { - "name": "ogtagscrapper", - "version": "1.0.1", + "name": "polarpod", + "version": "1.0.4", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -752,6 +752,11 @@ "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz", "integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw==" }, + "node-fetch": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz", + "integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==" + }, "nth-check": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/nth-check/-/nth-check-1.0.2.tgz", diff --git a/package.json b/package.json index 4c24a03..764f65c 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,6 @@ "og tags api", "video url parser api", "bad word filter api" - ], "author": "Matsukii", "license": "MIT", @@ -27,6 +26,7 @@ "metafetch": "^1.9.2", "mysql": "^2.17.1", "nano-memoize": "^1.1.5", + "node-fetch": "^2.6.0", "object-hash": "^1.3.1", "open-graph-scraper": "^3.6.2", "qrcode-svg": "^1.0.0" diff --git a/src/router.js b/src/router.js index 18bac16..8cc81c2 100644 --- a/src/router.js +++ b/src/router.js @@ -132,7 +132,7 @@ module.exports = (app, dir) => { * @returns {Object} video metadata (mediaType, provider, type, chanel, id...) */ app.get('/apis/video/meta', function(req, res){ - params = { + let params = { url: req.query.u } @@ -140,7 +140,31 @@ module.exports = (app, dir) => { return res.status(400).send('no URL informed'); } else{ - return res.status(200).send(urlp(params)) + urlp(params).then(r => { + return res.status(200).send(r) + }) + } + }) + + + /** + * + * @description get video thumbnail (is part of .../video/meta but returns only the image(s)) + * @returns {Object} video metadata (mediaType, provider, type, chanel, id...) + */ + app.get('/apis/video/thumb', function(req, res){ + let params = { + url: req.query.u, + thumb: true + } + + if(params.url == undefined){ + return res.status(400).send('no URL informed'); + } + else{ + urlp(params).then(r => { + return res.status(200).send(r) + }) } }) diff --git a/src/urlVideoParser.js b/src/urlVideoParser.js index 2801236..4fbec3d 100644 --- a/src/urlVideoParser.js +++ b/src/urlVideoParser.js @@ -1,5 +1,54 @@ -module.exports = (params) => { +module.exports = async(params) => { const parser = require('js-video-url-parser'); + const fetch = require('node-fetch'); // console.log(parser.parse(params.url)); - return parser.parse(params.url) + + let metadata = parser.parse(params.url); + + let onlyThumb = params.thumb; + + /** + * @descrioption try get thumbnail for youtube videos + * + * ! other platforms may be added later + * + * current avaliable: + * * youtube (url completation based) + * * vimeo (vimeo api based) (replaces largest thumbnail resolution value with '1920') + * + * in progress: + * + */ + if(metadata.provider == 'youtube'){ + let turl = `https://img.youtube.com/vi/${metadata.id}`; + + await fetch(`${turl}/maxresdefault.jpg`, { + method: 'GET' + }).then(r => { + if(r.status == 200){ + metadata.thumb = `${turl}/maxresdefault.jpg`; + } + else{ + metadata.thumb = `${turl}/0.jpg`; + } + }) + } + else if(metadata.provider == 'vimeo'){ + let apiURL = `https://vimeo.com/api/v2/video/${metadata.id}.json` + await fetch(apiURL, { + method: 'GET' + }).then(r => r.json()).then(r => { + let thum = r[0].thumbnail_large.replace(/_[0-9]{1,4}/gi, '_1920'); + metadata.thumb = thum; + metadata.thumbOriginal = r[0].thumbnail_large; + }); + } + + if(onlyThumb){ + return {thumb: metadata.thumb, originalSize: metadata.thumbOriginal}; + } + else{ + return metadata; + } + }; diff --git a/utils.md b/utils.md new file mode 100644 index 0000000..e3934a0 --- /dev/null +++ b/utils.md @@ -0,0 +1,4 @@ +# youtube thumbnail URLS + +* +*