ashi-smule
Version:
AshiTEAM Smule Song Downloader
310 lines • 14.4 kB
JavaScript
;
/// <reference path="SmuleUserData.ts"/>
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [0, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
var SmuleRecording = (function () {
function SmuleRecording() {
this.AlbumName = "Smule";
this.SmuleURL = "https://www.smule.com/";
}
SmuleRecording.prototype.Init1 = function (pRecData, sFolderPath) {
var i;
this.Singers = new Array(0);
this.Title = pRecData.title.replace(/\b\w/g, function (l) { return l.toUpperCase(); });
this.Singers.push(this.FixName(pRecData.performed_by));
if (pRecData.other_performers !== undefined && pRecData.other_performers.length > 0) {
for (i = 0; i < pRecData.other_performers.length; ++i) {
this.Singers.push(this.FixName(pRecData.other_performers[i].handle));
}
}
this.SongURL = this.SmuleURL + pRecData.web_url;
this.ImageURL = pRecData.cover_url;
this.ChildCount = pRecData.child_count;
this.FileType = pRecData.type;
this.InitFileNames(sFolderPath);
};
SmuleRecording.prototype.Init2 = function (title, songURL, imageURL, singers, type, folderPath) {
this.Title = title.replace(/\b\w/g, function (l) { return l.toUpperCase(); });
this.Singers = [];
this.SongURL = songURL;
this.ImageURL = imageURL;
this.ChildCount = 0;
this.FileType = type;
if (singers !== undefined && singers !== null) {
singers.forEach(function myFunction(item, index, pThis) {
this.Singers.push(this.FixName(item.trim()));
}, this);
}
this.InitFileNames(folderPath);
};
SmuleRecording.prototype.InitFileNames = function (sFolderPath) {
var sFileName = this.Title + " - ";
var sSingers = this.Singers.join(" - ");
sFileName += sSingers;
sFileName = this.FixForFileSystem(sFileName);
if (this.FileType == "audio") {
this.AudioFile = sFolderPath + (sFileName + ".m4a");
this.VideoFile = null;
}
else if (this.FileType == "video") {
this.VideoFile = sFolderPath + (sFileName + ".mp4");
this.AudioFile = sFolderPath + (sFileName + ".mp3");
}
this.ImageFile = sFolderPath + (sFileName + ".jpg");
};
SmuleRecording.prototype.ShowMessage = function (text) {
this.Spinner.text = this.Chalk.dim(text);
};
SmuleRecording.prototype.Download = function () {
return __awaiter(this, void 0, void 0, function () {
var chalk, ora, spinner, mp3Path, fs, err_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
chalk = require('chalk');
ora = require('ora');
spinner = ora();
_a.label = 1;
case 1:
_a.trys.push([1, 5, , 6]);
this.Spinner = spinner;
this.Chalk = chalk;
spinner.text = "" + chalk.dim('Downloading ' + this.Title);
spinner.start();
mp3Path = this.GetMP3Path();
fs = require('fs');
if (fs.existsSync(mp3Path)) {
spinner.succeed(chalk.green(this.Title + " already downloaded."));
return [2 /*return*/, true];
}
if (!(this.ChildCount == 0)) return [3 /*break*/, 3];
return [4 /*yield*/, this.DownloadImpl()];
case 2:
_a.sent();
return [3 /*break*/, 4];
case 3:
this.Spinner.text = this.Chalk.keyword('orange')('Warning: This looks like a collab.');
_a.label = 4;
case 4:
spinner.succeed(chalk.green("Downloaded " + this.Title));
return [2 /*return*/, true];
case 5:
err_1 = _a.sent();
spinner.fail("Failed to downloaded " + this.Title + ": " + err_1);
return [2 /*return*/, false];
case 6: return [2 /*return*/];
}
});
});
};
SmuleRecording.prototype.DownloadImpl = function () {
return __awaiter(this, void 0, void 0, function () {
var mediaURL;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
this.ShowMessage("Downloading " + this.Title);
return [4 /*yield*/, this.GetMediaLink()];
case 1:
mediaURL = _a.sent();
this.ShowMessage('Fetching downloadable link for ' + this.Title);
return [4 /*yield*/, this.DownloadFile(mediaURL)];
case 2:
_a.sent();
return [4 /*yield*/, this.ExtractAudio()];
case 3:
_a.sent();
return [4 /*yield*/, this.TagAudio()];
case 4:
_a.sent();
return [2 /*return*/];
}
});
});
};
SmuleRecording.prototype.GetMediaLink = function () {
var pThis = this;
return new Promise(function (resolve, reject) {
var https = require('follow-redirects').https;
https.get(pThis.SongURL, function (res, cb) {
pThis.ShowMessage("Downloading media links for " + pThis.Title + ". Please wait...");
var sHtml = '';
res.on('data', function (chunk) {
sHtml += chunk;
});
res.on('end', function () {
var $ = sHtml;
var mediaType = $.split("twitter:player:stream:content_type\" content=\"")[1].split('">')[0].replace('/mp4', '');
var mediaSource = $.split("twitter:player:stream\" content=\"")[1].split('">')[0];
var mediaURL = mediaSource.replace(/amp;/g, '');
resolve(mediaURL);
});
}).on("error", function (err) {
pThis.ShowMessage("Downloading Song Links Failed. " + err);
reject(err);
});
});
};
SmuleRecording.prototype.DownloadFile = function (fetchMainSource) {
var pThis = this;
return new Promise(function (resolve, reject) {
var _this = this;
var https = require('follow-redirects').https;
var fs = require('fs');
var saveMedia;
if (pThis.FileType === 'audio') {
saveMedia = fs.createWriteStream(pThis.AudioFile);
}
else {
saveMedia = fs.createWriteStream(pThis.VideoFile);
}
https.get(fetchMainSource, function (res, cb) {
pThis.ShowMessage("Downloading " + pThis.FileType + " for " + pThis.Title + ". Please wait...");
res.pipe(saveMedia);
saveMedia.on('finish', function () {
saveMedia.close(cb);
resolve();
});
}).on("error", function (err) {
_this.ShowMessage("Downloading " + pThis.FileType + " for " + pThis.Title + " Failed. " + err);
reject(err);
});
});
};
SmuleRecording.prototype.ExtractAudio = function () {
var pThis = this;
return new Promise(function (resolve, reject) {
pThis.ShowMessage("Creating mp3 for " + pThis.Title + ". Please wait...");
var FFmpeg = require('fluent-ffmpeg');
var fs = require('fs');
var sInFile;
if (pThis.FileType == "video")
sInFile = pThis.VideoFile;
else
sInFile = pThis.AudioFile;
pThis.ShowMessage("In File " + sInFile);
var pOutStream = fs.createWriteStream(pThis.GetMP3Path());
var conversion_process = new FFmpeg({
source: sInFile,
timeout: 0
}).withAudioCodec('libmp3lame')
.withAudioBitrate(128)
.withAudioChannels(2)
.withAudioFrequency(44100)
.withAudioQuality(5)
.toFormat('mp3')
.on('start', function (commandLine) {
pThis.ShowMessage("Audio Extraction Started for " + pThis.Title);
})
.on('error', function (err, stdout, stderr) {
pThis.ShowMessage("Audio Extraction Failed. " + err);
reject(err);
})
.on('progress', function (progress) {
pThis.ShowMessage("Creating mp3... " + progress.percent);
})
.on('end', function () {
if (pThis.AudioFile.endsWith(".m4a"))
fs.unlinkSync(pThis.AudioFile);
resolve();
})
.pipe(pOutStream, { end: true });
});
};
SmuleRecording.prototype.TagAudio = function () {
var pThis = this;
return new Promise(function (resolve, reject) {
pThis.ShowMessage("Tagging song " + pThis.Title);
var sSingers = pThis.Singers.join(" & ");
var sArtists = pThis.Singers.join("; ");
var nodeID3 = require('node-id3');
var tags = {
title: pThis.Title,
artist: sArtists,
album: "Smule",
year: 2018,
comment: {
language: "eng",
text: pThis.Title + " by " + sSingers + " on Smule"
}
};
var fs = require('fs');
var sFileName = pThis.GetMP3Path();
var filemp3 = fs.readFileSync(sFileName);
fs.writeFileSync(sFileName, nodeID3.update(tags, filemp3));
resolve();
});
};
SmuleRecording.prototype.FixName = function (sName) {
switch (sName) {
case "asangawee":
return "Asanga Weerapura";
case "darshismule":
return "Darshani Weerapura";
case "NirodhaThushari":
return "Nirodha Thushari";
case "Krik49":
return "Keerthi Weerasooriya";
case "nalakaed":
return "Nalaka Edirisinghe";
case "Pramoda102":
return "Pramoda Fatehchand";
}
return sName;
};
SmuleRecording.prototype.FixForFileSystem = function (text) {
return text.replace(/[\|&;\$%@"<>\(\)\+,/]/g, "_");
};
SmuleRecording.prototype.GetMP3Path = function () {
var path;
if (this.VideoFile !== undefined && this.VideoFile !== null) {
path = this.VideoFile;
}
else if (this.AudioFile !== undefined && this.AudioFile !== null) {
path = this.AudioFile;
}
return this.GetMP3PathFromPath(path);
};
SmuleRecording.prototype.GetMP3PathFromPath = function (path) {
if (path !== undefined && path !== null)
return path.replace(".m4a", ".mp3");
return "";
};
return SmuleRecording;
}());
exports.SmuleRecording = SmuleRecording;
//# sourceMappingURL=SmuleRecording.js.map