Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run patch monkey in async mode #575

Open
olragon opened this issue Jan 3, 2013 · 0 comments
Open

Run patch monkey in async mode #575

olragon opened this issue Jan 3, 2013 · 0 comments

Comments

@olragon
Copy link

olragon commented Jan 3, 2013

Follow up this discussion https://groups.google.com/forum/?fromgroups=#!topic/qlio/2whvdxaTFiQ

I want to implement simple html scraper into ql.io. My idea is when you write query like code below, ql.io will fetch the page, create browser like environment (JSDom), include jQuery, execute query and fetch result. But my problem is JSDom run in async mode but ql.io 's patch monkey run in sync mode, so ql.io return result before JSDom finish running. In example below, result always is blank array. Any chance to fix that?

I think because of this code:

respJson = resource.monkeyPatch['patch response']({

query

select * from www.scraper
  where url = "http://abcdef.com/xyz"
      and title = '[{"selector":"ul li a.title"}]'
      and url = '[{"selector":"ul li a.title", "prop": "href"}]';

www.scraper.ql

-- definition of www.scraper table
create table www.scraper
  on select get from "{url}"
  using patch 'www.scraper.js'
  resultset 'results'

www.scraper.js

var jsdom   = require('jsdom');
exports['parse response'] = function(args) {
    var result = [];
    // get body
    var body = '';
    _.each(args.body, function(buf) {
         body += buf.toString('UTF-8');
    });

    // create JSDom environment, include jQuery
    jsdom.env({
         html: body,
         scripts: ['http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js'],
         done: function(err, window) {
            // run CSS query using jQuery
            // jQuery(selector).each(function() { result.push($(element).text()) });
         }
    });

    // return result as new response
    return {
       type: 'application/json',
       content: JSON.stringify(result)
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant