Skip to content

Commit

Permalink
Fixed invidious API!
Browse files Browse the repository at this point in the history
src\renderer\helpers\api\invidious.js synced with main source

src\renderer\store\modules\invidious.js synced with main source
  • Loading branch information
CelularBat committed Jul 24, 2024
1 parent 90b8835 commit aab4090
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "freetube",
"productName": "FreeTube",
"description": "A private YouTube client",
"version": "0.20.1",
"version": "0.20.2",
"license": "AGPL-3.0-or-later",
"main": "./dist/main.js",
"private": true,
Expand Down
12 changes: 7 additions & 5 deletions src/renderer/helpers/api/invidious.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,16 @@ export function invidiousImageUrlToInvidious(url, currentInstance = null) {

function parseInvidiousCommentData(response) {
return response.comments.map((comment) => {
comment.id = comment.commentId
comment.showReplies = false
comment.authorLink = comment.authorId
comment.authorThumb = youtubeImageUrlToInvidious(comment.authorThumbnails[1].url)
comment.authorThumb = youtubeImageUrlToInvidious(comment.authorThumbnails.at(-1).url)
comment.likes = comment.likeCount
comment.text = autolinker.link(stripHTML(invidiousImageUrlToInvidious(comment.contentHtml, getCurrentInstance())))
comment.dataType = 'invidious'
comment.isOwner = comment.authorIsChannelOwner
comment.numReplies = comment.replies?.replyCount ?? 0
comment.hasReplyToken = !!comment.replies?.continuation
comment.replyToken = comment.replies?.continuation ?? ''
comment.isHearted = comment.creatorHeart !== undefined
comment.isMember = comment.isSponsor
Expand Down Expand Up @@ -321,10 +323,10 @@ export function filterInvidiousFormats(formats, allowAv1 = false) {
// Which is caused by Invidious API limitation on AV1 formats (see related issues)
// Commented code to be restored after Invidious issue fixed
//
// As we generate our own DASH manifest (using YouTube.js) for multiple audio track support in Electron,
// we can allow AV1 in that situation. If we aren't in electron,
// As we generate our own DASH manifest (using YouTube.js) for multiple audio track support when the local API is supported,
// we can allow AV1 in that situation. When the local API isn't supported,
// we still can't use them until Invidious fixes the issue on their side
if (process.env.IS_ELECTRON && allowAv1 && av1Formats.length > 0) {
if (process.env.SUPPORTS_LOCAL_API && allowAv1 && av1Formats.length > 0) {
return [...audioFormats, ...av1Formats]
}

Expand Down Expand Up @@ -408,7 +410,7 @@ export function convertInvidiousToLocalFormat(format) {
: {
fps: format.fps,
qualityLabel: format.qualityLabel,
colorInfo: format.colorInfo
colorInfo: format.colorInfo ?? {}
})
})

Expand Down
44 changes: 22 additions & 22 deletions src/renderer/store/modules/invidious.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fs from 'fs/promises'
import { createWebURL, fetchWithTimeout } from '../../helpers/utils'

const state = {
Expand All @@ -17,45 +16,46 @@ const getters = {
}

const actions = {
async fetchInvidiousInstancesFromFile({ commit }) {
const url = createWebURL('/static/invidious-instances.json')

const fileData = await (await fetch(url)).json()
const instances = fileData.filter(e => {
return process.env.SUPPORTS_LOCAL_API || e.cors
}).map(e => {
return e.url
})

commit('setInvidiousInstancesList', instances)
},

/// fetch invidious instances from site and overwrite static file.
async fetchInvidiousInstances({ commit }) {
const requestUrl = 'https://api.invidious.io/instances.json'

let instances = []
try {
const response = await fetchWithTimeout(15_000, requestUrl)
const json = await response.json()
instances = json.filter((instance) => {
const instances = json.filter((instance) => {
return !(instance[0].includes('.onion') ||
instance[0].includes('.i2p') ||
!instance[1].api ||
(!process.env.IS_ELECTRON && !instance[1].cors))
(!process.env.SUPPORTS_LOCAL_API && !instance[1].cors))
}).map((instance) => {
return instance[1].uri.replace(/\/$/, '')
})

if (instances.length !== 0) {
commit('setInvidiousInstancesList', instances)
} else {
console.warn('using static file for invidious instances')
}
} catch (err) {
if (err.name === 'TimeoutError') {
console.error('Fetching the Invidious instance list timed out after 15 seconds. Falling back to local copy.')
} else {
console.error(err)
}
}

// If the invidious instance fetch isn't returning anything interpretable
if (instances.length === 0) {
// Fallback: read from static file
const fileName = 'invidious-instances.json'
/* eslint-disable-next-line n/no-path-concat */
const fileLocation = process.env.NODE_ENV === 'development' ? './static/' : `${__dirname}/static/`
const filePath = `${fileLocation}${fileName}`
console.warn('reading static file for invidious instances')
const fileData = process.env.IS_ELECTRON ? await fs.readFile(filePath, 'utf8') : await (await fetch(createWebURL(filePath))).text()
instances = JSON.parse(fileData).filter(e => {
return process.env.IS_ELECTRON || e.cors
}).map(e => {
return e.url
})
}
commit('setInvidiousInstancesList', instances)
},

setRandomCurrentInvidiousInstance({ commit, state }) {
Expand Down

0 comments on commit aab4090

Please sign in to comment.