Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate channel related functionality to YouTube.js #3143

Merged
merged 18 commits into from
Mar 1, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 1 addition & 7 deletions _scripts/webpack.renderer.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,7 @@ const config = {
alias: {
vue$: 'vue/dist/vue.common.js',

// use the web version of linkedom
linkedom$: 'linkedom/worker',

// defaults to the prebundled browser version which causes webpack to error with:
// "Critical dependency: require function is used in a way in which dependencies cannot be statically extracted"
// webpack likes to bundle the dependencies itself, could really have a better error message though
'youtubei.js$': 'youtubei.js/dist/browser.js',
'youtubei.js$': 'youtubei.js/web',
},
extensions: ['.js', '.vue']
},
Expand Down
15 changes: 4 additions & 11 deletions _scripts/webpack.web.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,10 @@ const config = {
path: path.join(__dirname, '../dist/web'),
filename: '[name].js',
},
externals: [
{
electron: '{}'
},
({ request }, callback) => {
if (request.startsWith('youtubei.js')) {
return callback(null, '{}')
}
callback()
}
],
externals: {
electron: '{}',
'youtubei.js': '{}'
},
module: {
rules: [
{
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@
"vue-observe-visibility": "^1.0.0",
"vue-router": "^3.6.5",
"vuex": "^3.6.2",
"youtubei.js": "^2.9.0",
"yt-channel-info": "^3.2.1"
"youtubei.js": "^3.1.0"
},
"devDependencies": {
"@babel/core": "^7.20.12",
Expand Down
7 changes: 5 additions & 2 deletions src/renderer/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,11 +430,14 @@ export default defineComponent({
}

case 'channel': {
const { channelId, subPath } = result
const { channelId, subPath, url } = result

openInternalPath({
path: `/channel/${channelId}/${subPath}`,
doCreateNewWindow
doCreateNewWindow,
query: {
url
}
})
break
}
Expand Down
43 changes: 25 additions & 18 deletions src/renderer/components/data-settings/data-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import FtFlexBox from '../ft-flex-box/ft-flex-box.vue'
import FtPrompt from '../ft-prompt/ft-prompt.vue'
import { MAIN_PROFILE_ID } from '../../../constants'

import ytch from 'yt-channel-info'
import { calculateColorLuminance, getRandomColor } from '../../helpers/colors'
import {
copyToClipboard,
Expand All @@ -17,6 +16,7 @@ import {
writeFileFromDialog
} from '../../helpers/utils'
import { invidiousAPICall } from '../../helpers/api/invidious'
import { getLocalChannel } from '../../helpers/api/local'

export default defineComponent({
name: 'DataSettings',
Expand Down Expand Up @@ -967,25 +967,32 @@ export default defineComponent({
})
},

getChannelInfoLocal: function (channelId) {
return new Promise((resolve, reject) => {
ytch.getChannelInfo({ channelId: channelId }).then(async (response) => {
resolve(response)
}).catch((err) => {
console.error(err)
const errorMessage = this.$t('Local API Error (Click to copy)')
showToast(`${errorMessage}: ${err}`, 10000, () => {
copyToClipboard(err)
})
getChannelInfoLocal: async function (channelId) {
try {
const channel = await getLocalChannel(channelId)

if (this.backendFallback && this.backendPreference === 'local') {
showToast(this.$t('Falling back to the Invidious API'))
resolve(this.getChannelInfoInvidious(channelId))
} else {
resolve([])
}
if (channel.alert) {
return undefined
}

return {
author: channel.header.author.name,
authorThumbnails: channel.header.author.thumbnails
}
} catch (err) {
console.error(err)
const errorMessage = this.$t('Local API Error (Click to copy)')
showToast(`${errorMessage}: ${err}`, 10000, () => {
copyToClipboard(err)
})
})

if (this.backendFallback && this.backendPreference === 'local') {
showToast(this.$t('Falling back to the Invidious API'))
return await this.getChannelInfoInvidious(channelId)
} else {
return []
}
}
},

/*
Expand Down
8 changes: 5 additions & 3 deletions src/renderer/components/top-nav/top-nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,14 @@ export default defineComponent({
}

case 'channel': {
const { channelId, idType, subPath } = result
const { channelId, subPath, url } = result

openInternalPath({
path: `/channel/${channelId}/${subPath}`,
query: { idType },
doCreateNewWindow
doCreateNewWindow,
query: {
url
}
})
break
}
Expand Down
31 changes: 29 additions & 2 deletions src/renderer/helpers/api/invidious.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function getCurrentInstance() {
return store.getters.getCurrentInvidiousInstance
}

export function invidiousAPICall({ resource, id = '', params = {} }) {
export function invidiousAPICall({ resource, id = '', params = {}, doLogError = true }) {
return new Promise((resolve, reject) => {
const requestUrl = getCurrentInstance() + '/api/v1/' + resource + '/' + id + '?' + new URLSearchParams(params).toString()

Expand All @@ -19,12 +19,39 @@ export function invidiousAPICall({ resource, id = '', params = {} }) {
resolve(json)
})
.catch((error) => {
console.error('Invidious API error', requestUrl, error)
if (doLogError) {
console.error('Invidious API error', requestUrl, error)
}
reject(error)
})
})
}

/**
* Gets the channel ID for a channel URL
* used to get the ID for channel usernames and handles
* @param {string} url
*/
export async function invidiousGetChannelId(url) {
try {
const response = await invidiousAPICall({
resource: 'resolveurl',
params: {
url
},
doLogError: false
})

if (response.pageType === 'WEB_PAGE_TYPE_CHANNEL') {
return response.ucid
} else {
return null
}
} catch {
return null
}
}

export async function invidiousGetChannelInfo(channelId) {
return await invidiousAPICall({
resource: 'channels',
Expand Down