mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
Merge pull request #1899 from omnivore-app/revert-youtube-api-change
Revert using youtube data api
This commit is contained in:
commit
0e0dac8c30
1 changed files with 2 additions and 36 deletions
|
|
@ -28,7 +28,6 @@ export const escapeTitle = (title: string) => {
|
|||
}
|
||||
|
||||
export class YoutubeHandler extends ContentHandler {
|
||||
static apiKey = process.env.YOUTUBE_API_KEY
|
||||
constructor() {
|
||||
super()
|
||||
this.name = 'Youtube'
|
||||
|
|
@ -41,23 +40,19 @@ 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 = `${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 = `${BaseUrl}/watch?v=${videoId}`
|
||||
src = `${embedBaseUrl}/embed/${videoId}`
|
||||
dataUrl = `${dataApiBaseUrl}/videos?part=snippet&id=${videoId}`
|
||||
src = `${embedBaseUrl}/${videoId}`
|
||||
}
|
||||
|
||||
const oembedUrl =
|
||||
|
|
@ -70,7 +65,6 @@ 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
|
||||
|
|
@ -80,33 +74,6 @@ 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>
|
||||
|
|
@ -115,8 +82,7 @@ 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}" />
|
||||
<meta property="og:site_name" content="YouTube" />
|
||||
</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