so I was trying to get the metainfo from a youtube URL using ytdl in Node.js
here's the code I'm currently trying to get working :
ytdl.getInfo('https://www.youtube.com/watch?v=YQHsXMglC9A',{downloadURL: true},
function(err, info) {
if (err) throw err;
console.log(info.title);
}
);
So, this works fine and I get the title of the song. Now i'm trying to actually GET the info as in stocking it in a var for me to use later. And I can't get to manage it, I tried to return something from the callback methode or catching whatever getInfo() is giving but it keep saying it's 'undefined'.
If any of you have some ideas i'll be glad to try these.
Thanks, Lucas
maybe do something like this
ytdl.getInfo('https://www.youtube.com/watch?v=YQHsXMglC9A',{downloadURL: true},
function(err, info) {
if (err) throw err;
var songTitle = info.title //you can store it here
console.log(songTitle);
}
);
When downloading a video/audio using ytdl-core use a event handler instead!
The information will going to be fetched first, then audio/video is going to be downloaded.
ytdl(youtube_url)
.on('info', (info) => {
console.log(info.title); // the video title
});
Related
I have codes like
app.post('/zamansecimi',(req,res)=>{
{
zamansecimim(req.body.baslangic,req.body.bitis)
res.render('./dist/rapor.ejs')
}});
function zamansecimim (bas,bit) {
app.get("/zamansecimi2", function (req, res) {
data2="select * from raporlama where oncekizaman between $1 and $2"
var query = connection2.query(data2,[bas,bit] ,
function(err, result) {
if(err) throw err;
res.send(httpResponse(result.rows));
});
})
}
In these codes I want to change get result every post request but I have a problem with get request these values never change when I send to 2 or 3 ...request. First request is working.
Where is my mistake?
thank you for your help
Each request sent to the server can only have one response, and you can't trigger a GET request inside a POST request on the server and expect the response to that request to go back to the browser. Without the whole app, I can't tell you the best way to do what you want, but I'd suggest you have one response on the server like this:
app.post('/zamansecimi',(req, res)=>{
const bas = req.body.baslangic;
const bit = req.body.bitis;
const query="select * from raporlama where oncekizaman between $1 and $2";
connection2.query(query,[bas,bit], function(err, result) {
if(err) throw err;
res.send(httpResponse(result.rows));
});
});
That will at least ensure you get the correct data back to the browser. It does not seem to me like rapor.ejs needs re-rendering since nothing has changed on the server with the POST request. But honestly I can't tell what's going on from the code provided.
I am coding a Discord bot that can write to a local text file on glitch.com.
When I use the ready event handler my program is able to write to the file just fine:
client.on('ready', () => {
fs.appendFile('./log.txt', 'Hello\n', (err) => {
if(err) throw err;
});
});
//Writes to file
However, when I try to write to the same file using the message event handler, nothing happens:
client.on('message', (message) => {
fs.appendFile('./log.txt', 'Hello\n', (err) => {
if(err) throw err;
});
});
//Does not write to file
Do I need to change permissions on this file? Or change it from a local file? Any help would be greatly appreciated.
I think something like this might work, I don't have time to test it but it's using the writeFile() method as opposed to the appendFile() method
client.on('message', (message) => {
fs.writeFile('./log.txt', message, (err) => {
if(err) throw err;
});
});
Assuming you already have your local text file you wouldn't need to make a new one. So you may as well use writeFile(), only thing I can see going wrong with this is it may refresh the file every message, but you could probably fix that with a message collector that updates the file every say 100 messages.
Using appendFileSync instead of appendFile should fix your issue, as appendFileSync is more reliable and has less of a chance to create errors. Good luck!
I'm trying to explore something about Facebook-chat-api provided on git and node.js. I know what I want but I can't find proper literature so please help me out.
I have a Facebook page and I have front-end html page with sockets + back-end node.js. On front-end I have text box. The idea is that I want to send whatever is in that text box to my page as a private message.
I know You can send message to a friend via this node plugin but I'm nut sure what to put as ID if You want to send a message to Yourself or page.
Using facebook-chat-api.
socket.on("REQ fmsgsend", function(mailData){
facebook({email: "FB_EMAIL", password: "FB_PASSWORD"}, (err, api) => {
if(err) return console.error(err);
api.sendMessage(message.body, message.threadID);
});
});
I would put text box string in message.body but I'm not sure how to detect thread ID. Am I going wrong way?
Here is the solution:
var facbook = require("facebook-chat-api");
facbook({email: EMAIL#EMAIL.com, password: PASSWORD}, function callback (err, api) {
if(err) return console.error(err);
api.getUserID(FriendName, (err, dataF) => {
if(err) return console.error(err);
// Send the message to the best match (best by Facebook's criteria)
var msg = "Hello my friend!";
var threadID = dataF[0].userID;
api.sendMessage(msg, threadID);
api.logout(function(err){
if(err) return console.error(err);
console.log("logged out");
});
});
});
This code on node.js server sends a message to friend by name.
This code and it's functionalities are not approved by Facebook or Facebook-API.
There seems to be lack of documentation on this topic. I'm trying to upload an image and set it to avatar: { type: Types.CloudinaryImage } in my Keystone model.
I'm posting content as multipart form data with the following structure: avatar: <raw_data>. Here is how I handle this in my API:
exports.upload_avatar = function(req, res) {
if (!req.files.avatar) {
console.info('Request body missing');
return res.status(400).json({ message: 'Request body missing', code: 20 });
}
req.current_user.avatar = req.files.avatar;
req.current_user.save();
}
where current_user is a mongoose model. What I find confusing is how to set my CloudinaryImage type field to the data I receive in the API.
So, rather than just setting the avatar to the raw data (which would work fine for e.g. a string field), you'll need to go through the update handler, which calls to the {path}_upload special path in cloudinary image.
You should then be able to do avatar.getUpdateHandler, perhaps following this example.
I would like to share what worked for me. The process is kind of strange but by adding in this code, all of the model validation works just fine and cloudinary uploads are set.
post(req, res, next) {
const newBundle = new Bundle(); //A mongoose model
newBundle.getUpdateHandler(req).process(req.body, (err) => {
if (err) {
return res.status(500).json({
error: err.message,
});
}
return res.json(newBundle);
});
}
When posting to the endpoint, all you need to do is make sure you set your file fields to be {databaseFieldName}_upload.
Ok after some digging through the source code, I figured out a way to do that:
exports.upload_avatar = function(req, res) {
req.current_user.getUpdateHandler(req).process(req.files, {fields: 'avatar'}, function(err) {
if (err) {
return res.status(500).json({ message: err.message || '', code: 10 });
}
res.send('');
});
}
I had the following gotchas:
use getUpdateHandler to update CloudinaryImage field.
use "magic" naming for multipart form data fields you POST to your API: {field_name}_upload, which in my case would be avatar_upload.
process req.files, which is a dictionary with your field names as keys and your file data as values. req.body is empty due to some post-processing with multer.
invoke update handler on your keystone model (you need to fetch it with find first) rather than on a specific field. Then specify {fields: <>} to limit its scope, otherwise you could have some issues like validation errors trying to update the whole object.
Hi i am developing nodejs application. I am inserting data to mongodb but my page always in 'loading' mode. But strange thing is my data inserted to mongodb immediately but page load not stopping. My code is shown below:
app.post('/Management/Post/New',function(req, res){
new Post({
title:req.body.post.title,
body:req.body.post.body,
keywords:req.body.post.keywords
}).save(function (err, docs){
if(err) {
return res.render(__dirname + "/views/createpost", {
title: 'Yeni Gönderi Oluştur',
stylesheet: 'postcreate',
error: 'Gönderi oluşturulurken bir hata ile karşılaşıldı'
});
}
console.log('Gönderi oluşturuldu');
});
});
Have no idea.
You only send a response when there is an error. If there's no error, you server never sends anything back: that's why the page seems to always be loading.
You need to send a response when you have no error, like this:
.save(function (err, docs){
if(err) { // Executed when there was an error with Mongo
return res.render(...);
} else { // Executed when everything is fine
return res.render(...);
}
});
You aren't handling the success scenario except for a console.log. You need a res.render() or res.redirect() on success, not just error