Skip to content
Draft
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
87 changes: 59 additions & 28 deletions commands/render.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const { execFileSync } = require('child_process');
const URL = require('url');
const path = require('path');
const os = require('os');
const { fork } = require('child_process');

const osu = require('../osu.js');
const helper = require('../helper.js');
Expand All @@ -12,7 +10,7 @@ const config = require('../config.json');
module.exports = {
command: ['render', 'frame', 'fail'],
description: "Render picture or gif of a beatmap at a specific time. Videos 10 seconds or longer are automatically rendered as mp4 video with audio and beatmap background.",
usage: '[beatmap url] [+mods] [AR8] [CS6] [preview/strains/aim/speed/fail] [HD] [20%] [mp4] [plain] [120fps] [mm:ss] [353x] [4s]',
usage: '[beatmap url] [+mods] [AR8] [CS6] [preview/strains/aim/speed/fail] [HD] [20%] [mp4] [plain] [120fps] [mm:ss] [353x] [4s] [highres]',
example: [
{
run: "render strains",
Expand Down Expand Up @@ -47,9 +45,9 @@ module.exports = {

let score_id;

if(argv[0].toLowerCase() == 'fail'){
if(argv[0].toLowerCase() === 'fail'){
if(msg.channel.id in last_beatmap){
if(last_beatmap[msg.channel.id].rank != 'F'){
if(last_beatmap[msg.channel.id].rank !== 'F'){
reject("Last play is not a failed score");
return false;
}
Expand All @@ -65,45 +63,46 @@ module.exports = {
let hidden = false;
let flashlight = false;
let analyze = false;
let toS3 = false;

argv.map(arg => arg.toLowerCase());

argv.slice(1).forEach(arg => {
if(arg.startsWith('+'))
mods = arg.substr(1).toUpperCase().match(/.{1,2}/g);
else if(/^([0-9]+)\:([0-9]+)\:([0-9]+)$/g.test(arg)){
else if(/^([0-9]+):([0-9]+):([0-9]+)$/g.test(arg)){
let parts = arg.split(':');
if(parts.length > 2){
time += parseInt(parts[2]);
time += parseInt(parts[1]) * 1000;
time += parseInt(parts[0]) * 1000 * 60;
}
}else if(/^([0-9]+)\:([0-9]+)$/g.test(arg)){
}else if(/^([0-9]+):([0-9]+)$/g.test(arg)){
let parts = arg.split(':');
if(parts.length > 1){
time += parseInt(parts[1]) * 1000;
time += parseInt(parts[0]) * 1000 * 60;
}
}else if(arg.endsWith('.osr')){
osr = arg;
}else if(arg == 'strains' || arg == 'aim' || arg == 'speed'){
}else if(arg === 'strains' || arg === 'aim' || arg === 'speed'){
type = arg;
length = 4;
}else if(arg == 'preview'){
}else if(arg === 'preview'){
type = arg
length = 9;
video_type = 'mp4';
audio = true;
}else if(arg == 'hd' || arg == 'hidden'){
}else if(arg === 'hd' || arg === 'hidden'){
hidden = true;
}else if(arg == 'fl' || arg == 'flashlight'){
}else if(arg === 'fl' || arg === 'flashlight'){
flashlight = true;
}else if(arg == 'mp4'){
}else if(arg === 'mp4'){
video_type = 'mp4';
}else if(arg == 'audio'){
}else if(arg === 'audio'){
audio = true;
video_type = 'mp4';
}else if(arg == 'plain'){
}else if(arg === 'plain'){
audio = false;
}else if(arg.endsWith('%')){
speed = parseInt(arg) / 100;
Expand All @@ -114,9 +113,10 @@ module.exports = {
fps = Math.max(1, Math.min(240, _fps));
video_type = 'mp4';
}
}else if(arg == 'analyze'){
}else if(arg === 'analyze'){
analyze = true;
}else if(arg.endsWith('s')){
// }else if(arg.endsWith('s')){
}else if(/^[0-9]+(\.[0-9]+)?s$/g.test(arg)){
length = parseFloat(arg);
}else if(arg.endsWith('x')){
combo = parseInt(arg);
Expand All @@ -130,18 +130,23 @@ module.exports = {
od = parseFloat(arg.substr(2));
}else if(arg.startsWith('(') && arg.endsWith(')')){
objects = arg.substr(1, arg.length - 1).split(',').length;
}else if(arg == 'fail'){
}else if(arg === 'fail'){
if(msg.channel.id in last_beatmap){
if(last_beatmap[msg.channel.id].fail_percent == 1){
if(last_beatmap[msg.channel.id].fail_percent === 1){
reject("Last play is not a failed score");
return false;
}

percent = last_beatmap[msg.channel.id].fail_percent;
length = 4;
}
}else{
if(arg.startsWith('http://') || arg.startsWith('https://')){
}else if(arg === 'toS3'){
toS3 = true;
}else if(arg === 'highres'){
size = [1280, 960];
}else{
// noinspection HttpUrlsUsage
if(arg.startsWith('http://') || arg.startsWith('https://')){
beatmap_url = arg;
beatmap_promise = osu.parse_beatmap_url(beatmap_url);
beatmap_promise.then(response => {
Expand All @@ -163,10 +168,10 @@ module.exports = {
beatmap_id = _last_beatmap.beatmap_id;
download_promise = helper.downloadBeatmap(beatmap_id).catch(helper.error);

if(last_beatmap[msg.channel.id].score_id && mods.length == 0)
if(last_beatmap[msg.channel.id].score_id && mods.length === 0)
({ score_id } = last_beatmap[msg.channel.id]);

if(mods.length == 0)
if(mods.length === 0)
mods = last_beatmap[msg.channel.id].mods;
}

Expand All @@ -181,7 +186,8 @@ module.exports = {
if(config.debug)
helper.log('specified ar', ar);

if(!beatmap_id || custom_url){
// noinspection DuplicatedCode,JSDeprecatedSymbols
if(!beatmap_id || custom_url){
let download_url = URL.parse(beatmap_url);
download_path = path.resolve(os.tmpdir(), `${Math.floor(Math.random() * 1000000) + 1}.osu`);

Expand All @@ -192,12 +198,12 @@ module.exports = {
let preview_promise;

Promise.resolve(download_promise).then(async () => {
if(type == 'strains' || type == 'aim' || type == 'speed'){
if(type === 'strains' || type === 'aim' || type === 'speed'){
if(config.debug)
helper.log('getting strains for mods', mods);

time = (await osu.get_strains(download_path, mods.join(''), type)).max_strain_time_real - 2000;
}else if(type == 'preview'){
}else if(type === 'preview'){
preview_promise = osu.get_preview_point(download_path);
}

Expand All @@ -210,13 +216,38 @@ module.exports = {

frame.get_frames(download_path, time, length * 1000, mods, size, {
combo,
type: video_type, cs, ar, od, analyze, hidden, flashlight, black: false, osr, score_id, audio, fps, speed,
fill: video_type == 'mp4', noshadow: true, percent, border: false, objects, msg
type: video_type,
cs,
ar,
od,
analyze,
hidden,
flashlight,
black: false,
osr,
score_id,
audio,
fps,
speed,
fill: video_type === 'mp4',
noshadow: true,
percent,
border: false,
objects,
msg,
toS3: toS3,
});
}else{
frame.get_frame(download_path, time, mods, [800, 600], {
combo,
cs: cs, ar: ar, score_id, black: true, fill: true, analyze, hidden, percent: percent
cs: cs,
ar: ar,
score_id,
black: true,
fill: true,
analyze,
hidden,
percent: percent,
}, (err, buf) => {
if(err)
reject(err);
Expand Down
69 changes: 69 additions & 0 deletions generate-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const os = require('os');
const path = require('path');
const chalk = require('chalk');
const crypto = require('crypto');

const Discord = require('discord.js');
const axios = require('axios');
Expand Down Expand Up @@ -273,6 +274,74 @@

config.credentials.last_fm_key = value == 'none' ? "" : value;


default_value = {
client_id: 'none',
client_secret: 'none',
bucket_name: 'none',
bucket_endpoint: 'none'
};
let temporary_credentials = {
client_id: 'none',
client_secret: 'none',
bucket_name: 'none',
bucket_endpoint: 'none'
}
const S3_credential_fields = ['client_id', 'client_secret', 'bucket_name', 'bucket_endpoint'];

if (config.credentials.S3){
// console.log("existing S3 credentials found, assigning to default_value");
// console.log(config.credentials.S3);
default_value = config.credentials.S3;
}

do {
console.log('');
console.log(`(Optional) An S3 Bucket and access credentials are needed for the \`toS3\` parameter of the ${config.prefix}render command to work.`);
valid_key = true;

for (const field of S3_credential_fields){
value = readline.question(`${field} [${chalk.green(default_value[field])}]: `);
if(!value){
temporary_credentials[field] = default_value[field];
}
else{
temporary_credentials[field] = value;
}
}

if (temporary_credentials.client_id !== 'none'){
try{
const aws = require('aws-sdk');
const S3Endpoint = new aws.Endpoint(temporary_credentials.bucket_endpoint);
let s3 = new aws.S3({
endpoint: S3Endpoint,
accessKeyId: temporary_credentials.client_id,
secretAccessKey: temporary_credentials.client_secret
});

let test_upload_params = {
Bucket: temporary_credentials.bucket_name,
Key: crypto.randomBytes(16).toString('hex'),
Body: 'test'
};
await s3.upload(test_upload_params).promise();
// console.log(upload_result)
} catch(e){
console.log(chalk.redBright("Invalid S3 Bucket Credentials!"));
valid_key = false;
temporary_credentials = default_value;
}
}

} while (!valid_key &&
temporary_credentials.client_id !== 'none' &&
temporary_credentials.client_secret !== 'none' &&
temporary_credentials.bucket_name !== 'none' &&
temporary_credentials.bucket_endpoint !== 'none');

config.credentials.S3 = temporary_credentials;

console.log('');

try{
Expand Down
Loading