🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

text-to-mp3

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

text-to-mp3 - npm Package Compare versions

Comparing version
1.0.1
to
1.0.2
+104
-37
index.js

@@ -5,4 +5,6 @@ "use strict";

var debug = require("debug")(DEBUGNAME);
var fs = require('fs');
var TextToMp3 = function () {
const BASE_URL = "http://translate.google.com/translate_tts?";
var TextToMp3 = function () { }

@@ -21,47 +23,112 @@ /**

TextToMp3.prototype.parse = function (text, callback) {
var _parseURL = function(path, text){
if(typeof text === "undefined" || text === "" || typeof callback === "undefined"){
console.log("missing required params");
return;
var keysAtt = Object.keys(TextToMp3.prototype.attributes);
for(var i = 0; i< keysAtt.length ; i++){
path += keysAtt[i] + "=" + TextToMp3.prototype.attributes[keysAtt[i]]+"&";
}
path += "q="+text+"&";
path += "textLen="+text.length;
return path;
};
var _writeFile = function(fn, data){
if(fn.substring(fn.length-4, fn.length) !== ".mp3"){ // if name is not well formatted, I add the mp3 extention
fn+=".mp3";
}
var file = fs.createWriteStream(fn); // write it down the file
file.write(data);
file.end();
return file.path;
};
TextToMp3.prototype.saveMP3 = function(text, fileName, callback){
if(typeof callback !== 'undefined' && typeof(callback) == 'function'){
TextToMp3.prototype.getMp3(text,function(err,data){
if(err)
return callback(err);
var file = _writeFile(fileName, data);
return callback(null, fs.realpathSync(file));
});
}else{
return new Promise(function(resolve, reject) {
TextToMp3.prototype.getMp3(text).then(function (data) {
var file = _writeFile(fileName, data);
resolve(fs.realpathSync(file));
}).catch(function(err){
reject(err);
});
});
}
};
TextToMp3.prototype.getMp3 = function (text, callback) {
var fs = require('fs'),
request = require('request');
var path = "http://translate.google.com/translate_tts?";
var keysAtt = Object.keys(this.attributes);
for(var i = 0; i< keysAtt.length ; i++){
path += keysAtt[i] + "=" + this.attributes[keysAtt[i]]+"&";
}
path += "q="+text+"&";
path += "textLen="+text.length;
var data = [];
debug("PATH", path);
var data = [];
request
.get({
headers: {
"Accept-Encoding" : "identity;q=1, *;q=0",
"Range" : "bytes=0-"
},
uri: path,
method: 'GET'
})
.on('data',function(chunk){
data.push(chunk);
})
.on('end',function(){
if(data)
if(typeof callback !== 'undefined' && typeof(callback) == 'function'){
if(typeof text === "undefined" || text === ""){
callback("missing required params");
}
var path = _parseURL(BASE_URL, text);
debug("PATH", path);
request
.get({
headers: {
"Accept-Encoding": "identity;q=1, *;q=0",
"Range": "bytes=0-"
},
uri: path,
method: 'GET'
})
.on('data', function (chunk) {
data.push(chunk);
})
.on('end', function () {
callback(null, Buffer.concat(data));
else
callback("no data found");
})
.on('error', function(err) {
// handle error
callback(err, null)
})
})
.on('error', function (err) {
// handle error
callback(err);
});
}else {
return new Promise(function(resolve, reject) {
if(typeof text === "undefined" || text === ""){
reject("missing required params");
}
var path = _parseURL(BASE_URL, text);
debug("PATH", path);
request
.get({
headers: {
"Accept-Encoding": "identity;q=1, *;q=0",
"Range": "bytes=0-"
},
uri: path,
method: 'GET'
})
.on('data', function (chunk) {
data.push(chunk);
})
.on('end', function () {
resolve(Buffer.concat(data));
})
.on('error', function (err) {
// handle error
reject(err);
});
});
}
};
};
module.exports = TextToMp3;
module.exports = new TextToMp3();
{
"name": "text-to-mp3",
"version": "1.0.1",
"version": "1.0.2",
"author": "Enrico Aleandri",

@@ -5,0 +5,0 @@ "license": "MIT",

@@ -1,12 +0,77 @@

## Description
#Text to MP3
This is a Simple script to generate MP3 from text, using public Google Translate API, so it have the limits of public usage
On **textToMp3.js** there is a bash usage of libray, call it with -? for help information
[![npm version](https://badge.fury.io/js/text-to-mp3.svg)](https://badge.fury.io/js/text-to-mp3)
[![Open Source Love](https://badges.frapsoft.com/os/mit/mit.svg?v=102)](https://github.com/ellerbrock/open-source-badge/)
## Installation
Install this module through npm or clone it:
## Run It
```javascript
npm install --save text-to-mp3
```
## Usage
### Get MP3 - callback
```javascript
var txtomp3 = require("text-to-mp3");
txtomp3.getMp3("Ciao Mondo", function(err, binaryStream){
if(err){
console.log(err);
return;
}
var file = fs.createWriteStream("FileName.mp3"); // write it down the file
file.write(binaryStream);
file.end();
});
```
### Get MP3 - Promise
```javascript
var txtomp3 = require("text-to-mp3");
txtomp3.getMp3("Ciao Mondo").then(function(binaryStream){
var file = fs.createWriteStream("FileName.mp3"); // write it down the file
file.write(binaryStream);
file.end();
})
.catch(function(err){
console.log("Error", err);
});
```
### Save MP3 - callback
```javascript
var txtomp3 = require("text-to-mp3");
txtomp3.saveMP3("Ciao Mondo", "FileName.mp3", function(err, absoluteFilePath){
if(err){
console.log(err);
return;
}
console.log("File saved :", absoluteFilePath); //"File saved : /home/enrico/WebstormProjects/textToMp3/FileName.mp3"
});
```
### Save MP3 - Promise
```javascript
var txtomp3 = require("text-to-mp3");
//if you do not provide a mp3 extension, or you provide it wrong, it will automatically append.
txtomp3.saveMP3("Ciao Mondo", "FileName").then(function(absoluteFilePath){
console.log("File saved :", absoluteFilePath); //"File saved : /home/enrico/WebstormProjects/textToMp3/FileName.mp3"
})
.catch(function(err){
console.log("Error", err);
});
```
## Bash it
```
git clone https://github.com/enricoaleandri/text-to-mp3.git
cd text-to-mp3
npm install

@@ -13,0 +78,0 @@ node textToMp3.js -t "Texto to generate speech" -f "filenameSpeech.mp3"

@@ -7,3 +7,3 @@ #!/usr/bin/env nodejs

var TextToMp3 = require("./index");
var texttomp3 = require("./index");
var fs = require('fs');

@@ -45,4 +45,3 @@

//HERE WE GO
var texttomp3 = new TextToMp3();
texttomp3.parse(text, function(err, data){
texttomp3.getMp3(text, function(err, data){
if(err){

@@ -60,2 +59,2 @@ console.log(err);

console.log("MP3 SAVED!");
});
});