Skip to content

Commit

Permalink
implement function to auto-select most relevant quality
Browse files Browse the repository at this point in the history
  • Loading branch information
Pseudorizer committed Jul 23, 2021
1 parent cef427d commit 27161ed
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 14 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ jobs:
if: ${{ steps.previoustag.outputs.tag }} != ${{ env.PACKAGE_VERSION }}
with:
repo_token: "${{ secrets.GITHUB_TOKEN }}"
automatic_release_tag: "${{ env.PACKAGE_VERSION }}"
automatic_release_tag: "v${{ env.PACKAGE_VERSION }}"
prerelease: true
title: "${{ env.PACKAGE_VERSION }}"
title: "v${{ env.PACKAGE_VERSION }}"
files: |
deb/**/*.deb
flatpak/**/*.flatpak
Expand Down
2 changes: 1 addition & 1 deletion settings.json.template
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sessionCookie": "",
"defaultResolution": {
"resolution": {
"width": 1920,
"height": 1080
}
Expand Down
4 changes: 2 additions & 2 deletions src/app/types/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export type Settings = {
sessionCookie: string,
defaultResolution: DefaultResolution
resolution: Resolution
}

export type DefaultResolution = {
export type Resolution = {
width: number,
height: number
}
Expand Down
5 changes: 0 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {app, BrowserWindow} from 'electron';
import loadIpcHandlers from 'Main/ipcHandlers';
import {Settings} from 'Types/types';
import defineExtensions from 'Main/extensions';
import {loadDownloadWatcherHandlers} from 'Main/downloadWatcher';
import {loadSettings} from 'Main/loadSettings';
Expand All @@ -16,8 +15,6 @@ if (require('electron-squirrel-startup')) { // eslint-disable-line global-requir
app.quit();
}

let settings: Settings;

const createWindow = (): void => {
// Create the browser window.
/*const mainWindow = new BrowserWindow({
Expand Down Expand Up @@ -49,8 +46,6 @@ const createWindow = (): void => {
// and load the index.html of the app.
mainWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY);

console.log(MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY, MAIN_WINDOW_WEBPACK_ENTRY);

defineExtensions();

(async () => {
Expand Down
4 changes: 3 additions & 1 deletion src/main/downloadWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import AsyncLock from 'async-lock';
import {DownloadQueueItem} from 'Types/types';
import {parsePageFromUrl} from 'Main/pageParser';
import {ParsedItem} from 'MainTypes/types';
import {getMediaOptionsForVideo} from 'Main/utilityFunctions';
import {getClosestQuality, getMediaOptionsForVideo} from 'Main/utilityFunctions';
import {settings} from 'Main/loadSettings';

let downloadActive = false;
const lock = new AsyncLock();
Expand Down Expand Up @@ -48,6 +49,7 @@ async function startNewDownload(downloadItem: DownloadQueueItem) {

for (const video of videoUrls) {
const f = await getMediaOptionsForVideo(video.nextUrl);
const k = getClosestQuality(f, settings.resolution);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/errors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class RethrownError extends Error {
export class RethrownError extends Error {
original_error: Error;
stack_before_rethrow: string;

Expand Down
1 change: 1 addition & 0 deletions src/main/loadSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Settings} from 'Types/types';
import fsAsync from 'fs/promises';
import {setSessionCookie, validateSession} from 'Main/client';
import {dialog} from 'electron';
import {RethrownError} from 'Main/errors';

export let settings: Settings;

Expand Down
3 changes: 2 additions & 1 deletion src/main/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ export type WistiaAsset = {
codec: string,
url: string,
width: number,
height: number
height: number,
type: string
}
39 changes: 38 additions & 1 deletion src/main/utilityFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,48 @@
import {parsePageFromUrl} from 'Main/pageParser';
import {get} from 'Main/client';
import {WistiaMedia} from 'MainTypes/types';
import {WistiaAsset, WistiaMedia} from 'MainTypes/types';
import {Resolution} from 'Types/types';

// TODO think of a better name for this file

export async function getMediaOptionsForVideo(url: string) {
const videoParsed = await parsePageFromUrl(url, 'video');

const mediaJson = await get(videoParsed[0].nextUrl);

const wistiaMedia = JSON.parse(mediaJson) as WistiaMedia;

return wistiaMedia.media.assets.filter(x => x.ext === 'mp4' || x.slug === 'original' || x.type === 'still_image');
}

export function getClosestQuality(assets: WistiaAsset[], resolution: Resolution) {
const exactMatch = assets.find(x => x.height === resolution.height && x.width === resolution.width);

if (exactMatch) {
return exactMatch;
}

const referenceAsset = {
width: resolution.width,
height: resolution.height,
bitrate: 0,
ext: '',
display_name: '',
type: '',
size: 0,
codec: '',
url: '',
slug: ''
};

assets.push(referenceAsset);

assets = assets.sort((a, b) => {
return (a.height * a.width) - (b.height * b.width);
});

const insertedPosition = assets.indexOf(referenceAsset);
const isAtTop = insertedPosition === assets.length - 1;

return isAtTop ? assets[assets.length - 2] : assets[insertedPosition + 1];
}

0 comments on commit 27161ed

Please sign in to comment.