Google search module for node.js not working? - node.js

I am trying to use the following node module https://github.com/jprichardson/node-google in a web app I am building. The ReadMe file says to use it as follows. However res.links is an empty list for me. I am using it exactly as the example shows. Is the module broken or is it just not working for me?
/*This prints out the first 50 search results of the query `node.js best practices`. */
var google = require('google')
google.resultsPerPage = 25
var nextCounter = 0
google('node.js best practices', function (err, res){
if (err) console.error(err)
for (var i = 0; i < res.links.length; ++i) {
var link = res.links[i];
console.log(link.title + ' - ' + link.href)
console.log(link.description + "\n")
}
if (nextCounter < 4) {
nextCounter += 1
if (res.next) res.next()
}
})
I expect res.links to show an array of links. However it returns the empty list.

Related

Parsing JSON from Wikipedia API throws an error, but only a quarter of the time

I'm trying to get the summary of a random article using the Wikipedia API, but for some reason when parsing the JSON, I run into this error about a quarter of the time:
Unexpected token < in JSON at position 0. Why is this happening? I've pasted the link into my browser manually multiple times and haven't seen anything wrong with the JSON string.
const articleCount = 10;
const fetch = require('sync-fetch');
function article()
{
return fetch('https://en.wikipedia.org/api/rest_v1/page/random/summary').json().extract;
}
var source = '';
for(let i = 0; i < articleCount; i++)
{
source += article() + ' ';
console.log(parseInt(i/articleCount * 100) + '%');
}
console.log(source);
I've switched out the .json() method with JSON.parse(string), but it has the same problem.

Nodejs Webdav read multiple files (Problem with Array)

I am starting using NodeJs and webdav, I am using websockets to receive a list with the names of the images that I need to send to my app and then I have a for loop to send them to my app, but for some reason some of the images are undefined.
I am starting using NodeJs and webdav so I have no idea what's wrong.
for(let i = 0; i < ImagemList.length; i++){
wfs.readFile(ImagemList[i], "binary", function(err, data) {
Imagens[i] = data;
if(Imagens.length == ImagemList.length){
socket.emit("ImagemPost", Imagens);
}
});
}
For some reason I can't acess the variable "i" and for some reason the data was going to a random place leaving empty spots.
I've updated the code and it still goes to random places (don't know why) but doesn't leave any empty spot.
My array is still random if someone can help me.
Imagem = Imagem.replace("[", "");
Imagem = Imagem.replace("]", "");
let ImagemList = Imagem.split(", ");
let Imagens = [];
let contador = 0;
for(let o = 0; o < ImagemList.length; o++){
wfs.readFile(ImagemList[o], "binary", function(err, data) {
Imagens[contador] = (data);
contador++;
if(Imagens.length == ImagemList.length){
socket.emit("ImagemPost", Imagens);
}
});
}

PDF.js - split pdf into pages and re-build multiple files

I am currently working on a Node.js project. One of the actions required is to read the text of a pdf document and then split the document into separate files.
As I have been using pdf.js for all other pdf parsing in this project, I was hoping to complete the above requirement using it as well.
Reading the PDF and its text content is relatively straightforward.
For example -
function GetWords(pdfUrl){
var pdf = PDFJS.getDocument(pdfUrl);
return pdf.then(function(pdf) { // calculate total count for document
var maxPages = pdf.pdfInfo.numPages;
var countPromises = []; // collecting all page promises
for (var j = 1; j <= maxPages; j++) {
var page = pdf.getPage(j);
var txt = "";
countPromises.push(page.then(function(page) { // add page promise
var textContent = page.getTextContent();
return textContent.then
(
function(page)
{ // return content promise
for(var i=0;i<page.items.length;i++)
{
var txtadd = page.items[i].str
txt += txtadd.replace(/[^a-zA-Z0-9:;,.?!-() ]/g,'');
}
return txt.split(" ").length; // value for page words
});
}));
}
// Wait for all pages and sum counts
return Promise.all(countPromises).then(function (counts) {
var count = 0;
//counts.forEach(function (c) { count += c; });
return count;
});
});
}
However, I can't seem to find any examples of building a PDF from one / or more of its pages. Ideally, I would want to use the pdf.GetPage(j) to get an array of the pages required. Then push these into a new document and save this new document to disk.
Any help would be appreciated.
I ended up using a separate library to perform the splitting. http://pdfhummus.com/. So in combination with the PDF.js i was able to get the desired result.

Jimp : how to use new filename as parameter of write function?

I'm using Jimp module of NodeJS to do some image transformation in my (big) nodejs script inside a big loop where my filename is dynamically generated.
When doing my jimp image transformation, the filename provided to write function parameter is already changed. Because the write is inside a callback (so a another thread?) and my loop is already continue to process another loop step.
Here is an example overview:
for (var i = 0; i < 10; ++i) {
var filename = 'test' + i + '.png';
//some script to generate my image : pupperteer screenshot of a webpage
Jimp.read(filename).then(function (image) {
image.greyscale().write(filename);
}).catch(function (err) {
console.error(err);
});
}
In this example, my script create the file test1.png in color, then I can view a test2.png appear which is a copy of test1.png but in greyscale... Then it overwrite by a new color image named test2.png.
So I'm wondering how to solve this?
It's perfectly fine to have it in multithread, so how to use a copy of "filename" string to use it in parameter of write function?
Regards
Alex
use let instead of var
let filename = 'test' + i + '.png';
You have a problem with asynchrony. I think that you could create a method and send filename as param. For example:
for (var i = 0; i < 10; ++i) {
let filename = 'test' + i + '.png';
//some script to generate my image : pupperteer screenshot of a webpage
_saveImg(filename);
}
function _saveImg(name) {
const filename = name;
Jimp.read(filename).then(function (image) {
image.greyscale().write(filename);
}).catch(function (err) {
console.error(err);
});
}
=)

async in for loop in node.js without using async library helper classes [duplicate]

This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 7 years ago.
I am a beginner to node.js. I was trying out the examples from the 'learnyounode' tutorial. I am trying to write a program that takes three url parameters and fetches some data from those urls and displays the returned data in the order in which the urls were provided.
var http = require('http');
var bl = require('bl');
var url = [];
url[0] = process.argv[2];
url[1] = process.argv[3];
url[2] = process.argv[4];
var data = [];
var remaining = url.length;
for(var i = 0; i < url.length; i++){
http.get(url[i], function (response){
response.setEncoding('utf8');
response.pipe(bl(function (err, chunk){
if(err){
console.log(err);
}
else{
data[i] = chunk.toString();
console.log(data[i]);
remaining -= 1;
if(remaining == 0) {
for(var j = 0; j < url.length; j++){
console.log(data[j]);
}
}
}
}));
});
}
I have two console.log statements in the program. The output i get is as follows:
It'll be chunder where lets throw a ford. We're going durry where mad as a cooee
.
Shazza got us some apples with come a strides. Mad as a swag when get a dog up y
a roo. It'll be rapt piece of piss as cunning as a trackie dacks.
As cross as a bogged with watch out for the boardies. As cunning as a digger fla
min lets get some roo bar. As dry as a piker piece of piss he hasn't got a joey.
Lets throw a strides mate we're going digger.
undefined
undefined
undefined
It seems like the data is correctly fetched and stored in the 'data' array but it still displays undefined.
Any idea why this is happening?
Thanks in advance!
This is a very common issue in async programming in node.js or even in the browser. A main issue you have is that the loop variable i will not be what you want it to be some time later when the async callback is called. By then, the for loop will have run to the end of its loop and i will be at the end value for all response callbacks.
There are numerous ways to solve this. You can use a closure to close over the i value and make it uniquely available to each callback.
var http = require('http');
var bl = require('bl');
var url = [];
url[0] = process.argv[2];
url[1] = process.argv[3];
url[2] = process.argv[4];
var data = [];
var remaining = url.length;
for(var i = 0; i < url.length; i++){
// create closure here to uniquely capture the loop index
// for each separate http request
(function(index) {
http.get(url[index], function (response){
response.setEncoding('utf8');
response.pipe(bl(function (err, chunk){
if(err){
console.log(err);
}
else{
data[index] = chunk.toString();
console.log(data[index]);
remaining -= 1;
if(remaining == 0) {
for(var j = 0; j < url.length; j++){
console.log(data[j]);
}
}
}
}));
});
})(i);
}
If you do much node.js programming, you will find that you probably want to learn how to use promises because they are very, very handy for controlling the flow and sequence of async operations.

Resources