Skip to content

Commit

Permalink
Changes to youtube downloads to prioritize MP4 files
Browse files Browse the repository at this point in the history
- Also includes a download view page
  • Loading branch information
dmf444 committed May 31, 2021
1 parent cdc9d36 commit f97aa27
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "archives_manager",
"version": "2.0.5",
"version": "2.0.6",
"description": "The file & metadata management app for archives.",
"main": "./dist/main.bundle.js",
"scripts": {
Expand Down Expand Up @@ -33,7 +33,7 @@
"installerIcon": "./dist/public/archivesLogo.ico",
"uninstallerIcon": "./dist/public/archivesLogo.ico"
},
"buildVersion": "2.0.5"
"buildVersion": "2.0.6"
},
"author": "David Fernandes @dmf444",
"license": "GPLv3",
Expand Down
21 changes: 20 additions & 1 deletion src/main/database/LocalDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class FileDatabase {
let adapter = new FileSync(filePath + '/appdb.json');
//log.info("FileDB:" + filePath + '/appdb.json');
this.database = low(adapter);
this.database.defaults({ settings: {}, uploadhist: [], files: []}).write();
this.database.defaults({ settings: {}, uploadhist: [], downloadHistory: {}, files: []}).write();
}

public addFile(file: FileModel) {
Expand Down Expand Up @@ -105,6 +105,25 @@ export class FileDatabase {
return this.database.get('uploadhist').value();
}

public addNewDownload(url: string, data) {
let dataSpot = this.database.get('downloadHistory');
if(dataSpot == null){
this.database.push({downloadHistory: {}}).write();
}
dataSpot = this.database.get('downloadHistory').get(url);
if(dataSpot.value() == null) {
this.database.get('downloadHistory').set(url, data).write();
} else {
dataSpot.assign(data).write();
}
}

public getAllDownloads() {
let history = this.database.get('downloadHistory').value();
if(history == null) return false;
return history;
}

public getDiscordConfig() : ISettings {
let fss = new DiscordSettings();
return this.getSettingsOrDefault(fss);
Expand Down
2 changes: 1 addition & 1 deletion src/main/downloader/YouTubeDownloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class YouTubeDownloader implements IDownloader {
log.info(`Now downloading video from ${url}`)
let youtubeBuilder: YtdlBuilder = new YtdlBuilder(url);
let responseCode: number = await youtubeBuilder.setFilePath(initalDirectory).setOutputTemplate("%(title)s_%(id)s.%(ext)s")
.downloadThumbnail().downloadJsonInfo().downloadDescription().downloadAnnotations().normalizeFileNames().executeCommand();
.downloadThumbnail().downloadJsonInfo().downloadDescription().downloadAnnotations().normalizeFileNames().rencodeToMp4().executeCommand();

let downloadPromise: downloadPromise;
if (responseCode == 0) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,8 @@ ipcMain.on('file_upload', function (event, id:number) {

ipcMain.on('upload_list_get', function (event, args) {
event.sender.send('upload_list_reply', getFileDatabase().getAllUploads());
});

ipcMain.on('download_list_get', function (event, args) {
event.sender.send('download_list_reply', getFileDatabase().getAllDownloads());
});
15 changes: 13 additions & 2 deletions src/main/youtubedl/YtdlBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {getYoutubeDlManager} from "@main/main";
import {getFileDatabase, getYoutubeDlManager} from '@main/main';

const path = require('path');
const { spawn } = require('child_process');
const log = require('electron-log');

export class YtdlBuilder {

Expand Down Expand Up @@ -83,7 +84,7 @@ export class YtdlBuilder {
}

public rencodeToMp4(): YtdlBuilder {
this.executeArgs.push('--recode-video mp4');
this.executeArgs.push('-f bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best');
return this;
}

Expand All @@ -105,9 +106,19 @@ export class YtdlBuilder {
} else {
this.executeArgs.push("-o", this.outputFormat);
}
this.executeArgs.push("--newline");
this.executeArgs.push(this.downloadUrl);

const youtubedl = spawn(getYoutubeDlManager().getFullApplicationPath(), this.executeArgs);
youtubedl.stdout.setEncoding('utf8');
youtubedl.stdout.on('data', (data: string) => {
log.info('stdout: ' + data.replace("\n", ""));

let matches = ("" + data).match(/\d+[.]?\d%/gm);
if(matches !== null) {
getFileDatabase().addNewDownload(this.downloadUrl.replace(/\./g, ","), {percent: matches[matches.length - 1]});
}
});

return await new Promise((resolve, reject) => {
youtubedl.on('close', resolve);
Expand Down
4 changes: 4 additions & 0 deletions src/renderer/components/Info.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from "react";
import {Button, Descriptions, PageHeader, Row, Table, Tabs, Tag} from 'antd';
import {CheckCircleOutlined, CheckCircleTwoTone, CloseCircleTwoTone, CloudSyncOutlined} from "@ant-design/icons/lib";
import {ipcRenderer} from "electron";
import {DownloadTable} from '@/renderer/components/info/DownloadTable';
const log = require('electron-log');
const { TabPane } = Tabs;

Expand Down Expand Up @@ -116,6 +117,9 @@ export class Info extends React.Component {
<TabPane tab={"Upload History"} key={"2"} style={{textAlign: "left"}}>
<Table columns={this.columns} dataSource={this.state.uploadedFiles} />
</TabPane>
<TabPane tab={"Downloads"} key={"3"} style={{textAlign: "left"}}>
<DownloadTable/>
</TabPane>
</Tabs>
</div>
);
Expand Down
80 changes: 80 additions & 0 deletions src/renderer/components/info/DownloadTable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import * as React from "react";
import {Progress, Table} from 'antd';
import {ipcRenderer} from 'electron';
const log = require('electron-log');

export class DownloadTable extends React.Component<any, any> {

state = {
downloadValues: []
}

constructor(props) {
super(props);

this.parseData = this.parseData.bind(this);
ipcRenderer.on('download_list_reply', this.parseData);
}

componentDidMount(): void {
ipcRenderer.send('download_list_get', '');
this.startTimer();
}

componentWillUnmount(): void {
ipcRenderer.removeListener('download_list_reply', this.parseData);
}

private updateTimer = null;
private startTimer() {
//log.info(name, e.target.value);
clearTimeout(this.updateTimer);
this.updateTimer = setTimeout(() => { ipcRenderer.send('download_list_get', ''); this.startTimer(); }, 3000);
}


parseData = (event, data) => {
let newValues = [];
Object.entries(data).map((listThing) => {
let key = listThing[0];
let value: any = listThing[1];
newValues.push({downurl: key, percent: value.percent});
});
this.setState({downloadValues: newValues});
}

private columns = [
{
title: 'Download URL',
dataIndex: 'downurl',
key: 'downurl',
width: "45%",
render: text => {
return text.replace(/,/g, ".");
}
},
{
title: 'Completion',
dataIndex: 'percent',
key: 'percent',
width: "55%",
render: text => {
let percent = parseFloat(text.slice(0, -1));
let status = true;
if (percent > 99.99){
status = false;
}
return <Progress percent={percent} status={status ? "active" : "success"} />
}
}
];

public render() {
return (
<>
<Table columns={this.columns} dataSource={this.state.downloadValues}/>
</>
);
}

}

0 comments on commit f97aa27

Please sign in to comment.