0

I'm trying to use x-ray on Meteor but so far with no luck.

Here's the example I'm testing (it works fine on a basic node app)

import Xray from 'x-ray';

var xray = new Xray();

xray('http://reddit.com/r/meteor/', '.title',
[{
  title: '',
  href: '@href'
}]
)
  .write('./result.json');

2 Answers2

3

I hope you figured it out since it's 5 months ago, I had my head around this question and figured out that way.

Don't use the atmosphere package since it's not maintained anymore.

$meteor npm install --save x-ray (https://github.com/lapwinglabs/x-ray)

Then just create a Meteor.method on the server side and call it on the client side.

(https://docs.meteor.com/api/methods.html)

// Server Side

import Xray from 'x-ray'

Meteor.methods({
  scrap:function(){
    var x = Xray();
    console.log('Is scrapping');
    x('http://google.com', 'title')(function(err, title) {
      console.log(title) // Google
    })
   }
});

Then

// Client Side

Meteor.apply('scrap', function(error, result) {
  console.log(error, result);
  console.log('Scrap is bussy');
})

Cheers

TheBilTheory
  • 408
  • 2
  • 12
1

The code from the previous post indeed invokes the x-ray function on the server side but doesn't return the result to the client.

Using async/wait and promises (ES7) you can return the result from the server to the client:

method.js (server):

import { Meteor } from 'meteor/meteor';
import Xray from 'x-ray';

Meteor.methods({
  async 'scrape.test'() {
    let x = Xray(),
      scraper;
    function scrap() {
      return new Promise((r, e) => {
        x('http://google.com', 'title')(function(err, title) {
          if (err) e(err);
          if (title) r(title);
        });
      });
    }
    try {
      return await scrap();
    } catch (error) {
      throw new Meteor.Error('500', error);
    }
  }
});

client.js:

Meteor.call('scrape.test', (error, result) => {
  if (error) console.log('error', error);
  console.log('result', result);
});

Cheers

Willy
  • 81
  • 5