diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index da919f0..988afb1 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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 diff --git a/settings.json.template b/settings.json.template index 6a079f0..0db1c13 100644 --- a/settings.json.template +++ b/settings.json.template @@ -1,6 +1,6 @@ { "sessionCookie": "", - "defaultResolution": { + "resolution": { "width": 1920, "height": 1080 } diff --git a/src/app/types/types.d.ts b/src/app/types/types.d.ts index 6bc7785..61ea816 100644 --- a/src/app/types/types.d.ts +++ b/src/app/types/types.d.ts @@ -1,9 +1,9 @@ export type Settings = { sessionCookie: string, - defaultResolution: DefaultResolution + resolution: Resolution } -export type DefaultResolution = { +export type Resolution = { width: number, height: number } diff --git a/src/index.ts b/src/index.ts index a2fb1bf..1940093 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; @@ -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({ @@ -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 () => { diff --git a/src/main/downloadWatcher.ts b/src/main/downloadWatcher.ts index 40885ed..f6b435c 100644 --- a/src/main/downloadWatcher.ts +++ b/src/main/downloadWatcher.ts @@ -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(); @@ -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); } } diff --git a/src/main/errors.ts b/src/main/errors.ts index 36dc8d8..ec14aba 100644 --- a/src/main/errors.ts +++ b/src/main/errors.ts @@ -1,4 +1,4 @@ -class RethrownError extends Error { +export class RethrownError extends Error { original_error: Error; stack_before_rethrow: string; diff --git a/src/main/loadSettings.ts b/src/main/loadSettings.ts index 2e832c6..ded8079 100644 --- a/src/main/loadSettings.ts +++ b/src/main/loadSettings.ts @@ -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; diff --git a/src/main/types/types.d.ts b/src/main/types/types.d.ts index 5a3e804..1c35a64 100644 --- a/src/main/types/types.d.ts +++ b/src/main/types/types.d.ts @@ -28,5 +28,6 @@ export type WistiaAsset = { codec: string, url: string, width: number, - height: number + height: number, + type: string } diff --git a/src/main/utilityFunctions.ts b/src/main/utilityFunctions.ts index 38cc3ce..f454583 100644 --- a/src/main/utilityFunctions.ts +++ b/src/main/utilityFunctions.ts @@ -1,6 +1,9 @@ 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'); @@ -8,4 +11,38 @@ export async function getMediaOptionsForVideo(url: string) { 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]; }