Skip to content

Commit b193dcd

Browse files
committed
Ensure video is an absolute video url
1 parent b3cac2a commit b193dcd

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

packages/metascraper-video/__snapshots__/index.js.snap-shot

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
exports['video src 1'] = {
22
"image": "https://cdn.vox-cdn.com/thumbor/AtQQMyWrexi6-Xyk73jv6nqTO7s=/0x5:1247x658/fit-in/1200x630/cdn.vox-cdn.com/uploads/chorus_asset/file/10079811/Screen_Shot_2018_01_22_at_3.27.50_PM.png",
3-
"video": "https://cdn.vox-cdn.com/thumbor/4l0C-7uGFtTfc6lWibo1ITiE2YU=/0x0:1280x720/320x213/filters:focal(538x258:742x462):gifv():no_upscale()/cdn.vox-cdn.com/uploads/chorus_image/image/58416873/2018_01_22_14_19_55.0.gif",
3+
"video": null,
44
"author": "Rachel Becker",
55
"date": "2018-01-22T23:38:17.000Z",
66
"description": "The zombies are only released on the weekends, the developers promise",
@@ -26,7 +26,7 @@ exports['source src 1'] = {
2626

2727
exports['og:video 1'] = {
2828
"image": "https://pbs.twimg.com/tweet_video_thumb/DUQODu8VMAASlkj.jpg",
29-
"video": "https://twitter.com/i/videos/955905369242513414?embed_source=facebook",
29+
"video": null,
3030
"author": "_developit",
3131
"date": "2018-02-18T12:00:00.000Z",
3232
"description": "“📣 Announcing... Karmatic!\n🦑 Easy automatic (headless) browser testing\n🛠 No config needed, auto-detects webpack settings\n🤹‍♀️ Only one dependency to juggle\n🏎 Powered by Karma, Webpack, Jasmine & Puppeteer\nhttps://t.co/nguAcGaWQP https://t.co/OgxikFBdQ8”",

packages/metascraper-video/index.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
'use strict'
22

33
const { getUrl, isUrl } = require('@metascraper/helpers')
4+
const videoExtensions = require('video-extensions')
5+
const path = require('path')
6+
7+
const isVideoUrl = url => videoExtensions.includes(path.extname(url).substring(1))
48

59
/**
610
* Wrap a rule with validation and formatting logic.
@@ -9,11 +13,17 @@ const { getUrl, isUrl } = require('@metascraper/helpers')
913
* @return {Function} wrapped
1014
*/
1115

12-
const wrap = rule => ({ htmlDom, url }) => {
16+
const wrapUrl = (url, value) => isUrl(value)
17+
const wrapVideoUrl = (url, value) => wrapUrl(url, value) && isVideoUrl(value)
18+
19+
const createWrapper = validator => rule => ({ htmlDom, url }) => {
1320
const value = rule(htmlDom)
14-
return isUrl(value) && getUrl(url, value)
21+
return validator(url, value) && getUrl(url, value)
1522
}
1623

24+
const wrap = createWrapper(wrapUrl)
25+
const wrapVideo = createWrapper(wrapVideoUrl)
26+
1727
/**
1828
* Rules.
1929
*/
@@ -23,11 +33,11 @@ module.exports = () => ({
2333
wrap($ => $('video').attr('poster'))
2434
],
2535
video: [
26-
wrap($ => $('meta[property="og:video:secure_url"]').attr('content')),
27-
wrap($ => $('meta[property="og:video:url"]').attr('content')),
28-
wrap($ => $('meta[property="og:video"]').attr('content')),
29-
wrap($ => $('meta[property="twitter:player:stream"]').attr('content')),
30-
wrap($ => $('video').attr('src')),
31-
wrap($ => $('source').attr('src'))
36+
wrapVideo($ => $('meta[property="og:video:secure_url"]').attr('content')),
37+
wrapVideo($ => $('meta[property="og:video:url"]').attr('content')),
38+
wrapVideo($ => $('meta[property="og:video"]').attr('content')),
39+
wrapVideo($ => $('meta[property="twitter:player:stream"]').attr('content')),
40+
wrapVideo($ => $('video').attr('src')),
41+
wrapVideo($ => $('source').attr('src'))
3242
]
3343
})

packages/metascraper-video/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"url": "https://github.com/microlinkhq/metascraper/issues"
1717
},
1818
"dependencies": {
19-
"@metascraper/helpers": "^3.6.0"
19+
"@metascraper/helpers": "^3.6.0",
20+
"video-extensions": "~1.1.0"
2021
},
2122
"devDependencies": {
2223
"lodash": "latest",

0 commit comments

Comments
 (0)