Skip to content

Commit

Permalink
FIX: Bilibili - Add quality selection & fix incorrect server data (#5498
Browse files Browse the repository at this point in the history
)

* BilibiliManhua : Add quality selection

* Bilibilicomics : Add quality selection

* Update BilibiliManhua.mjs

* Update BilibiliManhua.mjs

* Update BilibiliManhua.mjs

* Update BilibiliManhua.mjs

* Bilibili : requested changed
  • Loading branch information
MikeZeDev authored Oct 3, 2023
1 parent eb3c211 commit 4e92a37
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 41 deletions.
68 changes: 41 additions & 27 deletions src/web/mjs/connectors/BilibiliComics.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,47 @@ export default class BilibiliComics extends BilibiliManhua {
this.links = {
login: 'https://www.bilibilicomics.com/account'
};
//override BilibiliManhua config as we need lanuage switch. Difference is handled in the main code with (this.config.language) ? : test
this.config = {
quality: {
label: 'Preferred format',
description: 'format of images\nwebp (low)\njpg (medium)\npng (high))',
input: 'select',
options: [
{ value: 'webp', name: 'webp' },
{ value: 'jpg', name: 'jpg' },
{ value: 'png', name: 'png' },
],
value: 'png'
},
language: {
label: 'Language Settings',
description: 'Choose the language to use. This will affect available manga lists',
input: 'select',
options: [
{ value: 'fr', name: 'French' },
{ value: 'en', name: 'English' },
{ value: 'cn', name: 'Chinese' },
{ value: 'id', name: 'Indonesian' },
{ value: 'es', name: 'Spanish' },
],
value: 'en'
},

this.config.language.input = 'select';
this.config.language.options = [
{ value: 'fr', name: 'French' },
{ value: 'en', name: 'English' },
{ value: 'cn', name: 'Chinese' },
{ value: 'id', name: 'Indonesian' },
{ value: 'es', name: 'Spanish' },
];
this.config.language.options.value = 'en';

this.config.picquality.options =
[
{ value: 'raw', name: 'Raw' },
{ value: 'hd', name: 'HD' },
{ value: 'sd', name: 'SD' },
];
this.config.picquality.options.value = 'raw';
}

//override quality formula cause its different for Bilibilicomics than for BilibiliManhua
getImageSizeByQuality(width) {

//quality is an optional url parameter to put when user select lowest quality
let o = {
imgWidth : width,
quality : undefined
};

if (width < 1) return o;

const choosedQuality = this.config.picquality.value;
const WindowWidth = 3000; //r = window.innerWidth, force it to max. Why are they adjusting picture to window width?

choosedQuality === 'raw' ? o.imgWidth = WindowWidth <= 1000 ? 1000 : WindowWidth <= 1200 ? 1200 : WindowWidth <= 1600 ? 1600 : WindowWidth <= 2000 ? 2000 : width : choosedQuality === 'hd' ? o.imgWidth = WindowWidth <= 1200 ? 800 : 1000 : (o.imgWidth = WindowWidth <= 1200 ? 600 : 800, o.quality = 50);

if (this.config.forcepicturesize.value) {
o.imgWidth = Math.max(o.imgWidth, width);
o.quality = undefined;
}
return o;
}
}

}
91 changes: 77 additions & 14 deletions src/web/mjs/connectors/BilibiliManhua.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,83 @@ export default class BilibiliManhua extends Connector {
};
//default config doesnt need language switch, its fixed to CN
this.config = {
quality: {
label: 'Preferred format',
description: 'format of images\nwebp (low)\njpg (medium)\npng (high))',
input: 'select',
options: [
{ value: 'webp', name: 'webp' },
{ value: 'jpg', name: 'jpg' },
{ value: 'png', name: 'png' },
format: {
label:'Preferred format',
description:'Format of images\nwebp(low)\n jpg(medium)\n png(high))',
input:'select',
options:[
{ value:'webp', name:'webp' },
{ value:'jpg', name:'jpg' },
{ value:'png', name:'png' },
],
value: 'png'
},
language: {
label: 'Language Settings',
description: 'Choose the language to use. This will affect available manga lists',
description: 'Choose the language to use. This will affect available manga in list',
input: 'disabled',
value : 'cn'
value: 'cn'
},
picquality: {
label: 'Quality Settings',
description: 'Choose the prefered quality',
input: 'select',
options: [
{ value: 'veryhigh', name:'VeryHigh' },
{ value: 'good', name:'Good' },
{ value: 'normal', name:'Normal' },
{ value: 'poor', name:'Poor' },
{ value: 'verypoor', name:'VeryPoor' },
],
value: 'good'
},
forcepicturesize: {
label: 'Force max quality (Experimental)',
description: 'Force server to send pictures with maxsize. Override "quality settings"',
input: 'checkbox',
value: false
}
};
}

getImageSizeByQuality(width) {
const ratioArray = { "verypoor":0.4, "poor":0.5, "normal":0.7, "good":0.85, "veryhigh":1 };
const widthArray = { verypoor:350, poor:450, normal:800, good:1100, veryhigh:1600 };

let o = {
imgWidth:width,
quality:undefined
};

//sometimes pictures size from JSON are 0 in this case Bibili forcea 0.85 ratio ,"Good"quality.
if (width < 1) {
o.imgWidth = widthArray.good;
return o;
}

const choosedQuality = ratioArray[this.config.picquality.value];
const calcWidth = Math.floor(width * choosedQuality); //0.85
switch(choosedQuality) {
case ratioArray.verypoor:
calcWidth > widthArray.verypoor && (o.imgWidth = widthArray.verypoor);
break;
case ratioArray.poor:
calcWidth > widthArray.poor && (o.imgWidth = widthArray.poor);
break;
case ratioArray.normal:
calcWidth > widthArray.normal && (o.imgWidth = widthArray.normal);
break;
case ratioArray.good:
calcWidth > widthArray.good && (o.imgWidth = widthArray.good);
break;
case ratioArray.veryhigh:
calcWidth > widthArray.veryhigh && (o.imgWidth = widthArray.veryhigh);
}

if (this.config.forcepicturesize.value) o.imgWidth = Math.max(o.imgWidth, width);
return o;
}

async _initializeConnector() {
await this.getToken();
if(this.auth.refreshToken) await this.refreshToken();
Expand Down Expand Up @@ -71,7 +128,7 @@ export default class BilibiliManhua extends Connector {
//if there is no cookie user is disconnected, force cleanup
if (cooki.length == 0) throw new Error('User is not connected.');

//if token is not defined, get if from cookies
//if token is not defined, get it from cookies
if (!this.auth.accessToken) {
const cookie_value = cooki[0].value;
this.auth.area = JSON.parse(decodeURIComponent(cookie_value)).area;
Expand Down Expand Up @@ -170,15 +227,15 @@ export default class BilibiliManhua extends Connector {
let credz = null;
let data = null;

//Using access_token from cookies, try to get token for unlocked chapter
//Using access_token from cookies, try to get token for unlocked chapters
if (this.auth.accessToken) {
credz = await this._fetchWithAccessToken('/GetCredential', {
ep_id: chapter.id,
comic_id : chapter.manga.id,
type : 1
});
}
//if we got chapter credentials, fetch full chapter using it
//if we got chapter credentials, fetch full chapter using them
if (credz && credz.data && credz.data.credential) {
data = await this._fetchTwirp('/GetImageIndex', {
ep_id: chapter.id,
Expand All @@ -191,7 +248,13 @@ export default class BilibiliManhua extends Connector {
});
}

let images = data.images.map(image => image.path + '@' + image.x + 'w.' + this.config.quality.value);
let images = data.images.map(image => {
const qualdata = this.getImageSizeByQuality(image.x);
let suffix = qualdata.imgWidth + 'w';
suffix += qualdata.quality ? '_' + qualdata.quality + 'q' : '';
return image.path + '@' + suffix + '.'+this.config.format.value;
});

images = await this._fetchTwirp('/ImageToken', {
urls: JSON.stringify(images)
});
Expand Down

0 comments on commit 4e92a37

Please sign in to comment.