mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
Call youtube data api to fetch published date
This commit is contained in:
parent
a3ab0fc35b
commit
98d5f92ad4
2 changed files with 46 additions and 10 deletions
|
|
@ -69,7 +69,7 @@ const pageToReadwiseHighlight = (page: Page): ReadwiseHighlight[] => {
|
|||
return (
|
||||
page.highlights
|
||||
// filter out highlights with no quote
|
||||
.filter((highlight) => highlight.quote.length === 0)
|
||||
.filter((highlight) => highlight.quote.length > 0)
|
||||
.map((highlight) => {
|
||||
return {
|
||||
text: highlight.quote,
|
||||
|
|
@ -149,18 +149,18 @@ export const syncWithReadwise = async (
|
|||
}
|
||||
// The request was made and the server responded with a status code
|
||||
// that falls out of the range of 2xx
|
||||
console.log('Readwise error, response data', error.response.data)
|
||||
console.error('Readwise error, response data', error.response.data)
|
||||
} else if (error.request) {
|
||||
// The request was made but no response was received
|
||||
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
|
||||
// http.ClientRequest in node.js
|
||||
console.log('Readwise error, request', error.request)
|
||||
console.error('Readwise error, request', error.request)
|
||||
} else {
|
||||
// Something happened in setting up the request that triggered an Error
|
||||
console.log('Error', error.message)
|
||||
console.error('Error', error.message)
|
||||
}
|
||||
} else {
|
||||
console.log('Error syncing with readwise', error)
|
||||
console.error('Error syncing with readwise', error)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ export const escapeTitle = (title: string) => {
|
|||
}
|
||||
|
||||
export class YoutubeHandler extends ContentHandler {
|
||||
static apiKey = process.env.YOUTUBE_API_KEY
|
||||
constructor() {
|
||||
super()
|
||||
this.name = 'Youtube'
|
||||
|
|
@ -38,19 +39,25 @@ export class YoutubeHandler extends ContentHandler {
|
|||
}
|
||||
|
||||
async preHandle(url: string): Promise<PreHandleResult> {
|
||||
const BaseUrl = 'https://www.youtube.com'
|
||||
const embedBaseUrl = 'https://www.youtube.com/embed'
|
||||
const dataApiBaseUrl = 'https://www.googleapis.com/youtube/v3'
|
||||
let urlToEncode: string
|
||||
let src: string
|
||||
let dataUrl: string
|
||||
const playlistId = getYoutubePlaylistId(url)
|
||||
if (playlistId) {
|
||||
urlToEncode = `https://www.youtube.com/playlist?list=${playlistId}`
|
||||
src = `https://www.youtube.com/embed/videoseries?list=${playlistId}`
|
||||
urlToEncode = `${BaseUrl}/playlist?list=${playlistId}`
|
||||
src = `${embedBaseUrl}/videoseries?list=${playlistId}`
|
||||
dataUrl = `${dataApiBaseUrl}/playlists?part=snippet&id=${playlistId}`
|
||||
} else {
|
||||
const videoId = getYoutubeVideoId(url)
|
||||
if (!videoId) {
|
||||
return {}
|
||||
}
|
||||
urlToEncode = `https://www.youtube.com/watch?v=${videoId}`
|
||||
src = `https://www.youtube.com/embed/${videoId}`
|
||||
urlToEncode = `${BaseUrl}/watch?v=${videoId}`
|
||||
src = `${embedBaseUrl}/embed/${videoId}`
|
||||
dataUrl = `${dataApiBaseUrl}/videos?part=snippet&id=${videoId}`
|
||||
}
|
||||
|
||||
const oembedUrl =
|
||||
|
|
@ -63,6 +70,7 @@ export class YoutubeHandler extends ContentHandler {
|
|||
thumbnail_url: string
|
||||
author_name: string
|
||||
author_url: string
|
||||
provider_name: string
|
||||
}
|
||||
// escape html entities in title
|
||||
const title = oembed.title
|
||||
|
|
@ -72,7 +80,33 @@ export class YoutubeHandler extends ContentHandler {
|
|||
const height = 350
|
||||
const width = height * ratio
|
||||
const authorName = _.escape(oembed.author_name)
|
||||
|
||||
let publishedAt = ''
|
||||
if (YoutubeHandler.apiKey) {
|
||||
// Make a GET request to the YouTube Data API and parse the response
|
||||
try {
|
||||
const response = (
|
||||
await axios.get(`${dataUrl}&key=${YoutubeHandler.apiKey}`)
|
||||
).data as {
|
||||
items: {
|
||||
snippet: {
|
||||
publishedAt: string
|
||||
}
|
||||
}[]
|
||||
}
|
||||
if (response.items.length > 0) {
|
||||
publishedAt = response.items[0].snippet.publishedAt
|
||||
}
|
||||
} catch (error) {
|
||||
if (axios.isAxiosError(error)) {
|
||||
console.error(
|
||||
'Error getting video/playlist publishedAt',
|
||||
error.response?.data
|
||||
)
|
||||
} else {
|
||||
console.error('Error getting video/playlist publishedAt', error)
|
||||
}
|
||||
}
|
||||
}
|
||||
const content = `
|
||||
<html>
|
||||
<head><title>${escapedTitle}</title>
|
||||
|
|
@ -81,6 +115,8 @@ export class YoutubeHandler extends ContentHandler {
|
|||
<meta property="og:title" content="${escapedTitle}" />
|
||||
<meta property="og:description" content="" />
|
||||
<meta property="og:article:author" content="${authorName}" />
|
||||
<meta property="og:article:published_time" content="${publishedAt}" />
|
||||
<meta property="og:site_name" content="${oembed.provider_name}" />
|
||||
</head>
|
||||
<body>
|
||||
<iframe width="${width}" height="${height}" src="${src}" title="${escapedTitle}" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
|
||||
|
|
|
|||
Loading…
Reference in a new issue