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

Fix web throttling because of double deciphering #3759

Merged
merged 1 commit into from
Jul 11, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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