diff --git a/packages/api/src/services/integrations.ts b/packages/api/src/services/integrations.ts index 9cff4567b..acde28e20 100644 --- a/packages/api/src/services/integrations.ts +++ b/packages/api/src/services/integrations.ts @@ -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 } diff --git a/packages/content-handler/src/websites/youtube-handler.ts b/packages/content-handler/src/websites/youtube-handler.ts index 8021f467e..9def4c30c 100644 --- a/packages/content-handler/src/websites/youtube-handler.ts +++ b/packages/content-handler/src/websites/youtube-handler.ts @@ -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 { + 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 = ` ${escapedTitle} @@ -81,6 +115,8 @@ export class YoutubeHandler extends ContentHandler { + +