Skip to content

Commit 19405c2

Browse files
committed
Video is a collection
1 parent a807b70 commit 19405c2

File tree

10 files changed

+95
-69
lines changed

10 files changed

+95
-69
lines changed

packages/metascraper-helpers/index.js

+22-22
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
'use strict'
22

3-
const { toLower, replace, includes, isString, trim, flow, isEmpty } = require('lodash')
3+
const {
4+
toLower,
5+
replace,
6+
includes,
7+
isString,
8+
trim,
9+
flow,
10+
isEmpty
11+
} = require('lodash')
412
const condenseWhitespace = require('condense-whitespace')
5-
const videoExtensions = require('video-extensions')
13+
const videoExtensions = require('video-extensions').concat(['gif'])
614
const audioExtensions = require('audio-extensions')
715
const isRelativeUrl = require('is-relative-url')
816
const fileExtension = require('file-extension')
@@ -21,34 +29,27 @@ const REGEX_LOCATION = /^[A-Z\s]+\s+[-—–]\s+/
2129

2230
const removeLocation = value => replace(value, REGEX_LOCATION, '')
2331

24-
const urlTest = (url, { relative = true }) => relative
25-
? isRelativeUrl(url) || urlRegex().test(url)
26-
: urlRegex().test(url)
32+
const urlTest = (url, { relative = true }) =>
33+
relative ? isRelativeUrl(url) || urlRegex().test(url) : urlRegex().test(url)
2734

2835
const isUrl = (url, opts = {}) => !isEmpty(url) && urlTest(url, opts)
2936

30-
const absoluteUrl = (baseUrl, relativePath = '') => (
37+
const absoluteUrl = (baseUrl, relativePath = '') =>
3138
resolveUrl(baseUrl, relativePath)
32-
)
3339

34-
const sanetizeUrl = (url, opts) => (
40+
const sanetizeUrl = (url, opts) =>
3541
_normalizeUrl(url, {
3642
normalizeHttp: false,
3743
stripWWW: false,
3844
sortQueryParameters: false,
3945
removeTrailingSlash: false,
4046
...opts
4147
})
42-
)
4348

44-
const normalizeUrl = (baseUrl, relativePath, opts) => (
49+
const normalizeUrl = (baseUrl, relativePath, opts) =>
4550
sanetizeUrl(absoluteUrl(baseUrl, relativePath), opts)
46-
)
4751

48-
const removeByPrefix = flow([
49-
value => value.replace(REGEX_BY, ''),
50-
trim
51-
])
52+
const removeByPrefix = flow([value => value.replace(REGEX_BY, ''), trim])
5253

5354
const createTitle = flow([condenseWhitespace, smartquotes])
5455

@@ -61,14 +62,13 @@ const titleize = (src, { capitalize = false, removeBy = false } = {}) => {
6162

6263
const defaultFn = el => el.text().trim()
6364

64-
const $filter = ($, collection, fn = defaultFn) => {
65-
const el = collection.filter((i, el) => fn($(el))).first()
65+
const $filter = ($, domNodes, fn = defaultFn) => {
66+
const el = domNodes.filter((i, el) => fn($(el))).first()
6667
return fn(el)
6768
}
6869

69-
const isAuthor = (str, opts = { relative: false }) => (
70+
const isAuthor = (str, opts = { relative: false }) =>
7071
isString(str) && !isUrl(str, opts)
71-
)
7272

7373
const getAuthor = (str, opts = { removeBy: true }) => titleize(str, opts)
7474

@@ -77,9 +77,8 @@ const protocol = url => {
7777
return protocol.replace(':', '')
7878
}
7979

80-
const createUrlExtensionValidator = collection => url => (
80+
const createUrlExtensionValidator = collection => url =>
8181
isUrl(url) && includes(collection, extension(url))
82-
)
8382

8483
const isVideoUrl = createUrlExtensionValidator(videoExtensions)
8584

@@ -89,7 +88,8 @@ const extension = url => fileExtension(url).split('?')[0]
8988

9089
const description = value => isString(value) && getDescription(value)
9190

92-
const getDescription = value => titleize(removeLocation(value), { capitalize: false })
91+
const getDescription = value =>
92+
titleize(removeLocation(value), { capitalize: false })
9393

9494
const publisher = value => isString(value) && condenseWhitespace(value)
9595

packages/metascraper-logo/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ const getDomNodeSizes = (domNodes, attr) =>
1616
const getSizes = ($, collection) =>
1717
chain(collection)
1818
.reduce((acc, { tag, attr }) => {
19-
const domNode = $(tag).get()
20-
const selectors = getDomNodeSizes(domNode, attr)
19+
const domNodes = $(tag).get()
20+
const selectors = getDomNodeSizes(domNodes, attr)
2121
return concat(acc, selectors)
2222
}, [])
2323
.sortBy(({ size }) => -size)

packages/metascraper-media-provider/test/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ const { getVideo } = require('..')
2525
describe('metascraper-media-provider', () => {
2626
describe('.getVideo', () => {
2727
it('twitter', () => {
28-
snapshot(getVideo(require('./fixtures/twitter.json')))
28+
snapshot(getVideo(require('./fixtures/video/twitter.json')))
2929
})
3030
it('vimeo', () => {
31-
snapshot(getVideo(require('./fixtures/vimeo.json')))
31+
snapshot(getVideo(require('./fixtures/video/vimeo.json')))
3232
})
3333
it('youtube', () => {
34-
snapshot(getVideo(require('./fixtures/youtube.json')))
34+
snapshot(getVideo(require('./fixtures/video/youtube.json')))
3535
})
3636
})
3737
describe('provider', () => {
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
1-
exports['video src 1'] = {
2-
"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": null,
4-
"author": "Rachel Becker",
5-
"date": "2018-01-22T23:38:17.000Z",
6-
"description": "The zombies are only released on the weekends, the developers promise",
7-
"lang": null,
8-
"logo": "https://cdn.vox-cdn.com/uploads/chorus_asset/file/7395351/android-chrome-192x192.0.png",
9-
"publisher": "The Verge",
10-
"title": "You can visit the Pentagon’s secret nuclear bunker inside Minecraft",
11-
"url": "https://www.theverge.com/2018/1/22/16921092/pentagon-secret-nuclear-bunker-reconstruction-minecraft-cns-miis-model"
12-
}
13-
14-
exports['source src 1'] = {
1+
exports['<source src /> 1'] = {
152
"image": "https://img-9gag-fun.9cache.com/photo/aGjVLDK_460s.jpg",
16-
"video": "https://img-9gag-fun.9cache.com/photo/aGjVLDK_460sv.mp4",
3+
"video": [
4+
"https://img-9gag-fun.9cache.com/photo/aGjVLDK_460sv.mp4"
5+
],
176
"author": null,
187
"date": null,
198
"description": "Watch the video and the fun convo of the 9GAG community",
@@ -37,22 +26,26 @@ exports['og:video 1'] = {
3726
"url": "https://twitter.com/_developit/status/955905369242513414"
3827
}
3928

40-
exports['src:poster 1'] = {
41-
"image": "https://thumbs.gfycat.com/TimelyHealthyArmadillo-mobile.jpg",
42-
"video": "https://thumbs.gfycat.com/TimelyHealthyArmadillo-mobile.mp4",
43-
"author": "Gfycat",
44-
"date": null,
45-
"description": "Watch Backflip GIF on Gfycat. Discover more PUBG GIFs on Gfycat",
46-
"lang": "en",
47-
"logo": "https://gfycat.com/static/apple-touch-icon/apple-touch-icon-180x180.png",
48-
"publisher": "Gfycat",
49-
"title": "Backflip - Create, Discover and Share Awesome GIFs on Gfycat",
50-
"url": "https://thumbs.gfycat.com/TimelyHealthyArmadillo-size_restricted.gif"
29+
exports['single src 1'] = {
30+
"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",
31+
"video": [
32+
"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"
33+
],
34+
"author": "Rachel Becker",
35+
"date": "2018-01-22T23:38:17.000Z",
36+
"description": "The zombies are only released on the weekends, the developers promise",
37+
"lang": null,
38+
"logo": "https://cdn.vox-cdn.com/uploads/chorus_asset/file/7395351/android-chrome-192x192.0.png",
39+
"publisher": "The Verge",
40+
"title": "You can visit the Pentagon’s secret nuclear bunker inside Minecraft",
41+
"url": "https://www.theverge.com/2018/1/22/16921092/pentagon-secret-nuclear-bunker-reconstruction-minecraft-cns-miis-model"
5142
}
5243

5344
exports['clips.twitch.tv 1'] = {
5445
"image": "https://clips-media-assets.twitch.tv/27434665136-offset-2366-preview.jpg",
55-
"video": "https://clips-media-assets.twitch.tv/AT-27434665136-offset-2366-1280x720.mp4",
46+
"video": [
47+
"https://clips-media-assets.twitch.tv/AT-27434665136-offset-2366-1280x720.mp4"
48+
],
5649
"author": null,
5750
"date": null,
5851
"description": "Shroud with the casual coffee sip to kill combo - Clipped by jpan11",
@@ -65,7 +58,9 @@ exports['clips.twitch.tv 1'] = {
6558

6659
exports['play.tv 1'] = {
6760
"image": "https://d1playscdntv-a.akamaihd.net/video/Ha35bprkDYG/processed/720.jpg",
68-
"video": "https://d1playscdntv-a.akamaihd.net/video/Ha35bprkDYG/processed/480.mp4",
61+
"video": [
62+
"https://d1playscdntv-a.akamaihd.net/video/Ha35bprkDYG/processed/480.mp4"
63+
],
6964
"author": "chineseouchie",
7065
"description": "Publicado por chineseouchie",
7166
"lang": "en",
@@ -75,3 +70,18 @@ exports['play.tv 1'] = {
7570
"url": "https://plays.tv/video/5a6f64b1bef69a7fa9/holy-shit"
7671
}
7772

73+
exports['src:poster 1'] = {
74+
"image": "https://thumbs.gfycat.com/TimelyHealthyArmadillo-mobile.jpg",
75+
"video": [
76+
"https://thumbs.gfycat.com/TimelyHealthyArmadillo-mobile.mp4"
77+
],
78+
"author": "Gfycat",
79+
"date": null,
80+
"description": "Watch Backflip GIF on Gfycat. Discover more PUBG GIFs on Gfycat",
81+
"lang": "en",
82+
"logo": "https://gfycat.com/static/apple-touch-icon/apple-touch-icon-180x180.png",
83+
"publisher": "Gfycat",
84+
"title": "Backflip - Create, Discover and Share Awesome GIFs on Gfycat",
85+
"url": "https://thumbs.gfycat.com/TimelyHealthyArmadillo-size_restricted.gif"
86+
}
87+

packages/metascraper-video/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const wrap = createWrapper((value, url) => urlFn(value, { url }))
1818

1919
const wrapVideo = createWrapper((value, url) => {
2020
const urlValue = urlFn(value, { url })
21-
return isVideoUrl(urlValue) && urlValue
21+
return isVideoUrl(urlValue) && [urlValue]
2222
})
2323

2424
/**
@@ -33,6 +33,6 @@ module.exports = () => ({
3333
wrapVideo($ => $('meta[property="og:video"]').attr('content')),
3434
wrapVideo($ => $('meta[property="twitter:player:stream"]').attr('content')),
3535
wrapVideo($ => $('video').attr('src')),
36-
wrapVideo($ => $('source').attr('src'))
36+
wrapVideo($ => $('video > source').attr('src'))
3737
]
3838
})

packages/metascraper-video/test/index.js

+27-10
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,34 @@ const readFile = promisify(fs.readFile)
2323

2424
describe('metascraper-video', () => {
2525
describe('video', () => {
26-
it('video src', async () => {
27-
const html = await readFile(resolve(__dirname, 'fixtures/video-src.html'))
28-
const url =
29-
'https://www.theverge.com/2018/1/22/16921092/pentagon-secret-nuclear-bunker-reconstruction-minecraft-cns-miis-model'
30-
31-
const metadata = await metascraper({ html, url })
32-
snapshot(metadata)
26+
describe('<video />', () => {
27+
it('single src', async () => {
28+
const html = await readFile(
29+
resolve(__dirname, 'fixtures/video-src.html')
30+
)
31+
const url =
32+
'https://www.theverge.com/2018/1/22/16921092/pentagon-secret-nuclear-bunker-reconstruction-minecraft-cns-miis-model'
33+
const metadata = await metascraper({ html, url })
34+
snapshot(metadata)
35+
})
36+
xit('multiple src', async () => {
37+
const html = `
38+
<video controls>
39+
<source src="video-small.mp4" type="video/mp4" media="all and (max-width: 480px)">
40+
<source src="video-small.webm" type="video/webm" media="all and (max-width: 480px)">
41+
<source src="video.mp4" type="video/mp4">
42+
<source src="video.webm" type="video/webm">
43+
</video>
44+
`
45+
const url =
46+
'https://www.theverge.com/2018/1/22/16921092/pentagon-secret-nuclear-bunker-reconstruction-minecraft-cns-miis-model'
47+
const metadata = await metascraper({ html, url })
48+
console.log(metadata)
49+
// snapshot(metadata)
50+
})
3351
})
3452

35-
it('source src', async () => {
53+
it('<source src />', async () => {
3654
const html = await readFile(
3755
resolve(__dirname, 'fixtures/source-src.html')
3856
)
@@ -45,12 +63,11 @@ describe('metascraper-video', () => {
4563
it('og:video', async () => {
4664
const html = await readFile(resolve(__dirname, 'fixtures/tweet.html'))
4765
const url = 'https://twitter.com/_developit/status/955905369242513414'
48-
4966
const metadata = await metascraper({ html, url })
5067
snapshot(metadata)
5168
})
5269

53-
describe('specific providers', () => {
70+
describe('by providers', () => {
5471
it('clips.twitch.tv', async () => {
5572
const html = await readFile(
5673
resolve(__dirname, 'fixtures/providers/clip.twitch.tv.html')

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -973,11 +973,10 @@ exports['wikipedia 1'] = {
973973
"date": "2016-10-13T12:00:00.000Z",
974974
"description": "Bob Dylan (/ˈdɪlən/; born Robert Allen Zimmerman, May 24, 1941) is an American singer-songwriter, author, and painter, who has been an influential figure in popular music and culture for more than five decades. Much of his most celebrated work dates from the 1960s, when he became a reluctant “voice of a generation”[2] with songs such as “Blowin’ in the Wind” and “The Times They Are a-Changin’” that became anthems for the Civil Rights Movement and anti-war movement. In 1965, he controversially abandoned his early fan-base in the American folk music revival, recording a six-minute single, “Like a Rolling Stone”, which enlarged the scope of popular music.",
975975
"image": "https://upload.wikimedia.org/wikipedia/commons/thumb/0/02/Bob_Dylan_-_Azkena_Rock_Festival_2010_2.jpg/1200px-Bob_Dylan_-_Azkena_Rock_Festival_2010_2.jpg",
976-
"video": "https://upload.wikimedia.org/wikipedia/en/e/e7/Bob_Dylan_-_Blowin'_in_the_Wind.ogg",
976+
"video": null,
977977
"lang": "en",
978978
"logo": "https://en.wikipedia.org/static/apple-touch/wikipedia.png",
979979
"publisher": "Wikipedia",
980980
"title": "Bob Dylan - Wikipedia",
981981
"url": "https://en.wikipedia.org/wiki/Bob_Dylan"
982982
}
983-

0 commit comments

Comments
 (0)