[READ-ONLY] Mirror of https://github.com/probablykasper/yt-email-notifier. macOS menubar app for YouTube upload notification emails
email menubar notifications tray youtube
0

Configure Feed

Select the types of activity you want to include in your feed.

Improved reliability of parsing video urls

Kasper (Nov 27, 2020, 10:59 PM +0100) 47d205d9 c4166466

+39 -24
+39 -24
src/server.js
··· 398 398 storeUpdate() 399 399 400 400 } else if (type === 'addChannel') { 401 - let channelUrl = data.channel 402 - const isVideoURL = channelUrl.includes('youtube.com/watch') 403 - || channelUrl.includes('youtu.be/') 404 - || channelUrl.includes('youtube.com/v/') 405 - if (isVideoURL) { 406 - const url = 'https://www.youtube.com/oembed?url='+channelUrl+'&format=json' 407 - const res = await fetch(url) 408 - const json = await res.json() 409 - if (json.error) throw json.error 410 - channelUrl = json.author_url 401 + const parseVideoId = function(url) { 402 + if (url.includes('youtube.com/watch') && url.includes('?v=')) { 403 + return url.split('?v=')[1].substring(0, 11) 404 + } else if (url.includes('youtube.com/watch') && url.includes('&v=')) { 405 + return url.split('&v=')[1].substring(0, 11) 406 + } else if (url.includes('youtu.be/')) { 407 + return url.split('youtu.be/')[1].substring(0, 11) 408 + } 411 409 } 412 - const segments = channelUrl.split('/') 413 - const channelIdSegment = segments.indexOf('channel') + 1 414 - const usernameSegment = segments.indexOf('user') + 1 415 - || segments.indexOf('www.youtube.com') + 1 416 - || segments.indexOf('youtube.com') + 1 417 - const query = { key: store.apiKey, part: 'contentDetails,id,snippet' } 418 - if (channelIdSegment > 0) { 419 - const channelId = segments[channelIdSegment] 420 - query.id = channelId 421 - } else if (usernameSegment >= 0) { 422 - const username = segments[usernameSegment] 423 - query.forUsername = username 410 + const getChannelIdFromVideoId = async function(videoId) { 411 + const result = await fetchYT({ 412 + url: 'https://youtube.googleapis.com/youtube/v3/videos', 413 + query: { part: 'snippet', id: videoId }, 414 + }) 415 + if (!result.items || !result.items[0]) { 416 + throw new Error(`No channel found for video ID '${videoId}':`, JSON.stringify(result)) 417 + } 418 + return result.items[0].snippet.channelId 419 + } 420 + let providedUrl = data.channel 421 + const videoId = parseVideoId(providedUrl) 422 + let channelId, username 423 + if (videoId) { 424 + channelId = await getChannelIdFromVideoId(videoId) 424 425 } else { 425 - throw new Error(`URL '${channelUrl}' not recognized.`) 426 + const segments = providedUrl.split('/') 427 + const channelIdSegment = segments.indexOf('channel') + 1 428 + const usernameSegment = segments.indexOf('user') + 1 429 + || segments.indexOf('www.youtube.com') + 1 430 + || segments.indexOf('youtube.com') + 1 431 + if (channelIdSegment > 0) { 432 + channelId = segments[channelIdSegment] 433 + } else if (usernameSegment >= 0) { 434 + username = segments[usernameSegment] 435 + } else { 436 + throw new Error(`URL '${providedUrl}' not recognized.`) 437 + } 426 438 } 439 + const query = { part: 'contentDetails,id,snippet' } 440 + if (channelId) query.id = channelId 441 + else query.forUsername = username 427 442 const result = await fetchYT({ 428 443 url: 'https://www.googleapis.com/youtube/v3/channels', 429 444 query: query, 430 445 }) 431 - if (!result.items) throw new Error(`Channel not found for url '${channelUrl}'.\nYou could try a video URL`) 446 + if (!result.items) throw new Error(`Channel not found for url '${providedUrl}'.\nYou could try a video URL`) 432 447 const channelObject = result.items[0] 433 448 store.instances[data.instance].channels.push({ 434 449 id: channelObject.id,