Skip to content

Commit

Permalink
Fix web throttling because of double deciphering (FreeTubeApp#3759)
Browse files Browse the repository at this point in the history
  • Loading branch information
absidue authored Jul 11, 2023
1 parent 87af457 commit b0239d1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 31 deletions.
42 changes: 14 additions & 28 deletions src/renderer/helpers/api/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,27 +152,6 @@ export async function getLocalVideoInfo(id, attemptBypass = false) {
player = innertube.actions.session.player

info = await innertube.getInfo(id)

// // the android streaming formats don't seem to be throttled at the moment so we use those if they are availabe
try {
const androidInnertube = await createInnertube({ clientType: ClientType.ANDROID, generateSessionLocally: false })
const androidInfo = await androidInnertube.getBasicInfo(id, 'ANDROID')

// Sometimes when YouTube detects a third party client or has applied an IP-ratelimit,
// they replace the response with a different video id
// https://github.com/TeamNewPipe/NewPipe/issues/8713
// https://github.com/TeamPiped/Piped/issues/2487
if (androidInfo.basic_info.id !== id) {
console.error(`Failed to fetch android formats. Wrong video ID in response: ${androidInfo.basic_info.id}, expected: ${id}`)
} else if (androidInfo.playability_status.status !== 'OK') {
console.error('Failed to fetch android formats', JSON.stringify(androidInfo.playability_status))
} else {
info.streaming_data = androidInfo.streaming_data
}
} catch (error) {
console.error('Failed to fetch android formats')
console.error(error)
}
}

if (info.streaming_data) {
Expand All @@ -188,17 +167,24 @@ export async function getLocalComments(id, sortByNewest = false) {
return innertube.getComments(id, sortByNewest ? 'NEWEST_FIRST' : 'TOP_COMMENTS')
}

// I know `type & type` is typescript syntax and not valid jsdoc but I couldn't get @extends or @augments to work

/**
* @typedef {object} _LocalFormat
* @property {string} freeTubeUrl deciphered streaming URL, stored in a custom property so the DASH manifest generation doesn't break
*
* @typedef {Misc.Format & _LocalFormat} LocalFormat
*/

/**
* @param {Misc.Format[]} formats
* @param {import('youtubei.js').Player} player
*/
function decipherFormats(formats, player) {
for (const format of formats) {
format.url = format.decipher(player)

// set these to undefined so that toDash doesn't try to decipher them again, throwing an error
format.cipher = undefined
format.signature_cipher = undefined
// toDash deciphers the format again, so if we overwrite the original URL,
// it breaks because the n param would get deciphered twice and then be incorrect
format.freeTubeUrl = format.decipher(player)
}
}

Expand Down Expand Up @@ -705,7 +691,7 @@ export function parseLocalTextRuns(runs, emojiSize = 16, options = { looseChanne
}

/**
* @param {Misc.Format} format
* @param {LocalFormat} format
*/
export function mapLocalFormat(format) {
return {
Expand All @@ -716,7 +702,7 @@ export function mapLocalFormat(format) {
mimeType: format.mime_type,
height: format.height,
width: format.width,
url: format.url
url: format.freeTubeUrl
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/renderer/views/Watch/Watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ export default defineComponent({
}
this.adaptiveFormats = this.videoSourceList

/** @type {import('../../helpers/api/local').LocalFormat[]} */
const formats = [...result.streaming_data.formats, ...result.streaming_data.adaptive_formats]
this.downloadLinks = formats.map((format) => {
const qualityLabel = format.quality_label ?? format.bitrate
Expand All @@ -521,7 +522,7 @@ export default defineComponent({
}

return {
url: format.url,
url: format.freeTubeUrl,
label: label
}
})
Expand Down Expand Up @@ -942,7 +943,7 @@ export default defineComponent({
},

/**
* @param {import('youtubei.js').Misc.Format[]} audioFormats
* @param {import('../../helpers/api/local').LocalFormat[]} audioFormats
* @returns {AudioSource[]}
*/
createLocalAudioSourceList: function (audioFormats) {
Expand All @@ -969,7 +970,7 @@ export default defineComponent({
}

return {
url: format.url,
url: format.freeTubeUrl,
type: format.mime_type,
label: 'Audio',
qualityLabel: label
Expand Down

0 comments on commit b0239d1

Please sign in to comment.