How to retrieve all feeds of a facebook page - node.js

When retrieving feeds of a facebook page, we can at most get 100 feeds.
How can we parse all available feeds with graph API and node.js?
If it was recursive, we could test on paging and next elements when getting the response.
For example, this code retrieves only two last feed pages, but I want to retrieve all feeds.
// note: you might want to prevent the callback hell :)
graph.get('likes', {limit: 2, access_token: "foobar"}, function(err, res) {
if(res.paging && res.paging.next) {
graph.get(res.paging.next, function(err, res) {
// page 2
});
}
});

if you want to get a data. you must be set user_post permission on graph explorer.
Code.
FB.api(
'/mosaiquefm/feed',
'GET',
{},
function(response)
{
// here print response. this response according to screenshot.
}
);

Related

How do I get data from the Google Analytics API by filtering on a specific URL

Good afternoon, I'm developing an application in Node.js, this application returns the total views of my projects in JSON.
My question is: how do I get the total views of a specific page or URL. Bearing in mind that this will become an attribute for my function later.
async function getTopPosts(customUrl) { /* I need the Custom URL to be used as a filter */
try {
await jwt.authorize();
const response = await google.analytics("v3").data.ga.get({
auth: jwt,
ids: "ga:" + VIEW_ID,
"start-date": "2019-01-01",
"end-date": "today",
dimensions: "ga:pagePath,ga:pageTitle",
metrics: "ga:pageviews",
sort: "-ga:pageviews",
"max-results": "10",
filters: "ga:medium==organic",
});
let data = response.data
console.log(data)
} catch (err) {
console.log(err);
}
};
I tried to see the GA-API documentations GA-API but I was not successful.
I hope it returns a JSON containing only the total views of a specific page or URL of my Google Analytics project.

How to follow a link in a get request header in Node.js Express

I am trying to retrieve paginated results from a 3rd party API after making an API call from my Node.js/Express server. I then want to send the data through to the client. I can retrieve the first page of results using the Request package and the following code:
var options = {
url: `https://theURL.com`,
headers: {
'authorization': `bearer ${user_token}`,
'user-agent': '***my details***'
}
}
function callback(error, response, body) {
if (!error) {
res.json({
data: body
});
} else {
res.send("an error occured")
}
}
Request(options, callback);
I understand that the response will contain a Link header which I should follow to get the next page's data and to retrieve the link header for the page after that. I repeat this process until I reach a blank link header, at which point all the pages of data have been retrieved.
Firstly, I don't know how to approach this task, should I be following all the link headers and compiling all the results on my server before transferring them to the client? Or should I send each pages worth of data to the client as I get it and then deal with it there?
Secondly, how can an appropriate solution be achieved in code?

Send variable from mongoose query to page without reload on click

I have a link on my site. When clicked it'll call a function that does a mongoose query.
I'd like the results of that query to be sent to the same page in a variable without reloading the page. How do I do that? Right now it is just rendering the page again with new query result data.
// List comments for specified chapter id.
commentController.list = function (req, res) {
var chapterId = req.params.chapterId;
var query = { chapterId: chapterId };
Chapter.find({ _id: chapterId }).then(function (chapter) {
var chapter = chapter;
Comment.find(query).then(function (data) {
console.log(chapter);
Chapter.find().then(function(chapters){
return res.render({'chapterlinks', commentList: data, user: req.user, chapterq: chapter, chapters:chapters });
})
});
})
};
You just need to make that request from your browser via AJAX:
https://www.w3schools.com/xml/ajax_intro.asp
This would be in the code for your client (browser), not the code for your server (nodejs).
UPDATE:
Here's a simple example, which uses jQuery to make things easier:
(1) create a function that performs and handles the ajax request
function getChapterLinks(chapterId) {
$.ajax({
url: "/chapterLinks/"+chapterId,
}).done(function(data) {
//here you should do something with data
console.log(data);
});
}
(2) bind that function to a DOM element's click event
$( "a#chapterLinks1" ).click(function() {
getChapterLinks(1);
});
(3) make sure that DOM element is somewhere in you html
<a id="chapterLinks1">Get ChapterLinks 1</a>
Now when this a#chapterLinks1 element is clicked, it will use AJAX to fetch the response of /chaptersLink/1 from your server without reloading the page.
references:
http://api.jquery.com/jquery.ajax/
http://api.jquery.com/jquery.click/

Getting list of messages from Twilio

I am trying to call Twilio services into my node application.
According to docs I am calling list of messages service like bellow
var accountSid = 'ACe622fda3d3cd03b3b975d8d92f7c794b';
var authToken = "your_auth_token";
var client = require('twilio')(accountSid, authToken);
client.messages.list(function(err, data) {
data.messages.forEach(function(message) {
console.log(message.body);
});
});
As a result I am getting first 50 messages with complete details.
Now my issue is how to get previous messages(pagination), conversations between two numbers and using further filters like date.
Twilio developer evangelist here.
List resources return pagination information, including the next and previous pages' URLs. You can also set the page size.
So, for a first pass you can get more than 50 messages by setting the PageSize to the maximum 1000.
client.messages.list({ PageSize: 1000 }, function(err, data) {
data.messages.forEach(function(message) {
console.log(message.body);
});
});
If you need to go beyond that, then you can use the next page url to get the next page:
var url = require("url");
client.messages.list(function(err, data) {
if (data.next_page_uri) {
// deal with page 1
var query = url.parse(data.next_page_uri, true).query;
client.messages.list(query, function(err, data) {
// and so on
}
}
});
Adam Varga shared a solution he was using on GitHub (it's for phone numbers, but lists all act the same on Twilio). Also, look out for the release of version 3 of the Node.js library, which will include pagination helpers.

how to publish a page using node.js

I have just begun to learn node.js. Over the last two days, I've been working on a project that accepts userinput and publishes a ICS file. I have all of that working. Now consider when I have to show this data. I get a router.get to see if I am at the /cal page and..
router.get('/cal', function(req, res, next)
{
var db = req.db;
var ical = new icalendar.iCalendar();
db.find({
evauthor: 'mykey'
}, function(err, docs) {
docs.forEach(function(obj) {
var event2 = ical.addComponent('VEVENT');
event2.setSummary(obj.evics.evtitle);
event2.setDate(new Date(obj.evics.evdatestart), new Date(obj.evics.evdateend));
event2.setLocation(obj.evics.evlocation)
//console.log(ical.toString());
});
});
res.send(ical.toString());
// res.render('index', {
// title: 'Cal View'
// })
})
So when /cal is requested, it loops through my db and creates an ICS calendar ical. If I do console.log(ical.toString) within the loop, it gives me a properly formatted calendar following the protocol.
However, I'd like to END the response with this. At the end I do a res.send just to see what gets published on the page. This is what gets published
BEGIN:VCALENDAR VERSION:2.0
PRODID:calendar//EN
END:VCALENDAR
Now the reason is pretty obvious. Its the nature of node.js. The response gets sent to the browser before the callback function finishes adding each individual VEVENT to the calendar object.
I have two related questions:
1) Whats the proper way to "wait" till the callback is done.
2) How
do I use res to send out a .ics dynamic link with
ical.toString() as the content. Do I need to create a new view for
this ?
edit: I guess for number 2 I'd have to set the HTTP headers like so
//set correct content-type-header
header('Content-type: text/calendar; charset=utf-8');
header('Content-Disposition: inline; filename=calendar.ics');
but how do I do this when using views.
Simply send the response, once you got the neccessary data! You are not required to end or send directly in your route but can do it in a nested callback as well:
router.get('/cal', function(req, res, next) {
var db = req.db;
var ical = new icalendar.iCalendar();
db.find({
evauthor: 'mykey'
}, function(err, docs) {
docs.forEach(function(obj) {
var event2 = ical.addComponent('VEVENT');
event2.setSummary(obj.evics.evtitle);
event2.setDate(new Date(obj.evics.evdatestart), new Date(obj.evics.evdateend));
event2.setLocation(obj.evics.evlocation)
});
res.type('ics');
res.send(ical.toString());
});
});
I also included sending the proper Content-Type by using res.type.
Also: Don't forget to add proper error handling. You can for example use res.sendStatus(500) if an error occured while retrieving the documents.

Resources