Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/metascraper-youtube/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const pLocate = require('p-locate').default
const {
$filter,
author,
date,
memoizeOne,
title,
toRule
Expand All @@ -32,6 +33,8 @@ const getThumbnailUrl = (id, gotOpts) => {

const toAuthor = toRule(author)

const toDate = toRule(date)

const toTitle = toRule(title)

const getVideoInfo = memoizeOne(getVideoId)
Expand All @@ -45,6 +48,7 @@ module.exports = ({ gotOpts } = {}) => {
toAuthor($ => $filter($, $('[class*="user-info" i]'))),
toAuthor($ => $('[itemprop="author"] [itemprop="name"]').attr('content'))
],
date: [toDate($ => $('[itemprop="uploadDate" i]').attr('content'))],
publisher: () => 'YouTube',
image: [
({ url }) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Inside the Mind of a Master Procrastinator | Tim Urban | TED - YouTube</title>
<link rel="canonical" href="https://www.youtube.com/watch?v=arj7oStGLkU">
<meta property="og:title" content="Inside the Mind of a Master Procrastinator | Tim Urban | TED">
<script type="application/ld+json">{
"@context": "https://schema.org",
"@type": "VideoObject",
"@id": "https://www.youtube.com/watch?v=arj7oStGLkU",
"name": "Inside the Mind of a Master Procrastinator | Tim Urban | TED",
"thumbnailUrl": "https://i.ytimg.com/vi/arj7oStGLkU/maxresdefault.jpg",
"uploadDate": "2016-04-06T09:59:35-07:00",
"comment": [
{
"@type": "https://schema.org/Comment",
"dateCreated": "2020-05-11T12:29:04-07:00",
"text": "a comment",
"author": {
"@type": "https://schema.org/Person",
"name": "a commenter"
},
"upvoteCount": 1
}
]
}</script>
<span itemscope itemtype="http://schema.org/VideoObject">
<link itemprop="url" href="https://www.youtube.com/watch?v=arj7oStGLkU">
<meta itemprop="datePublished" content="2016-04-06T09:59:35-07:00">
<meta itemprop="uploadDate" content="2016-04-06T09:59:35-07:00">
<span itemprop="author" itemscope itemtype="http://schema.org/Person">
<link itemprop="url" href="http://www.youtube.com/@TED">
<link itemprop="name" content="TED">
</span>
</span>
</head>
<body></body>
</html>
13 changes: 11 additions & 2 deletions packages/metascraper-youtube/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ const { metascraper } = require('./helpers')
test('get the high image size', async t => {
const html = await readFile(resolve(__dirname, 'fixtures/image-size.html'))
const url = 'https://www.youtube.com/watch?v=A0FZIwabctw'
const { date, ...metadata } = await metascraper({ html, url })
t.is(typeof date, 'string')
const metadata = await metascraper({ html, url })
t.snapshot(metadata)
})

Expand All @@ -22,6 +21,16 @@ test('youtube video', async t => {
t.snapshot(metadata)
})

test('a video is dated by its upload date, not by its comments', async t => {
const html = await readFile(
resolve(__dirname, 'fixtures/youtube-video-comments.html')
)
const url = 'https://www.youtube.com/watch?v=arj7oStGLkU'

const { date } = await metascraper({ html, url })
t.is(date, '2016-04-06T16:59:35.000Z')
})
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

test('youtube video old', async t => {
const html = await readFile(
resolve(__dirname, 'fixtures/youtube-video-old.html')
Expand Down
1 change: 1 addition & 0 deletions packages/metascraper-youtube/test/snapshots/index.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Generated by [AVA](https://avajs.dev).

{
author: 'SpaceX',
date: '2018-03-11T01:30:44.000Z',
description: 'When Falcon Heavy lifted off, it became the most powerful operational rocket in the world by a factor of two. With the ability to lift into orbit nearly 64…',
image: 'https://img.youtube.com/vi/A0FZIwabctw/maxresdefault.jpg',
lang: 'es',
Expand Down
Binary file modified packages/metascraper-youtube/test/snapshots/index.js.snap
Binary file not shown.